私はVB.NETにかなり慣れていないので、簡単な問題だと思っています。DB にクエリを実行し、結果をデータセットに保存してから、同じ DB にクエリを実行し、DELETE タスクの結果を結合しようとしています。誰かが支援する瞬間があれば、これが私がこれまでに持っているものです。前もって感謝します。 また; 誰かに直してもらうよりも、自分が間違っていたことを学ぶ方が好きです。
Private Sub cmdDelete_Click(sender As System.Object, e As System.EventArgs) Handles cmdDelete.Click
'Set/Open Connection
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\youngje\Documents\SQL Server Management Studio\Projects\Nwind.accdb")
con.Open()
'Set TIMEID
Dim Yr, Mnth, fRng As String
Yr = Year(Now)
Mnth = Format(Month(Now), "00")
fRng = Yr + Mnth + "00"
'Query 1/Fill Temp Table (tmp)
Dim cmdA As OleDbCommand = New OleDbCommand("SELECT PRODUCT, SHIPTO, TIMEID " & _
"FROM tblFactSales " & _
"WHERE (BILLTO = 'INPUT_BILLTO') AND (BRANCHPLANT = 'INPUT_BRANCHPLANT') AND (FRTHANDLE = 'INPUT_FRTHANDLE') AND (DATATYPE = 'FORECAST') AND (TIMEID > '" & fRng & "' )" & _
"AND (SIGNEDDATA >= - .01) AND (SIGNEDDATA <= .01) AND (SALESDATA = 'short_tons')", con)
'Query 2, joined with Query 1
Dim cmdB As OleDbCommand = New OleDbCommand("DELETE FS " & _
"FROM tblFactSales as FS INNER JOIN tmp T" & _
"ON FS.PRODUCT=T.PRODUCT AND FS.SHIPTO=T.SHIPTO AND FS.TIMEID=T.TIMEID " & _
"WHERE (FS.DATATYPE = 'FORECAST') AND (FS.TIMEID > '" & fRng & "' )", con)
'Execute Queries
cmdA.ExecuteNonQuery()
cmdB.ExecuteNonQuery()
'Clean Up
cmdA.Dispose()
cmdB.Dispose()
con.Close()
GC.Collect()
'Confirmation
MessageBox.Show("Records Removed Successfully.", "Clear Complete", _
MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
End Sub
完全に修正したわけではありませんが、近づいていると思います。cmdB.ExecuteNonQuery で「レコードが削除されました」というエラーが表示されます。tmp テーブルを作成/移入しますが、何も削除されません。
更新されたコード:
Private Sub cmdDelete_Click(sender As System.Object, e As System.EventArgs) Handles cmdDelete.Click
'Set/Open Connection
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\youngje\Documents\SQL Server Management Studio\Projects\Nwind.accdb")
con.Open()
'Set TIMEID
Dim Yr, Mnth, fRng As String
Yr = Year(Now)
Mnth = Format(Month(Now), "00")
fRng = Yr + Mnth + "00"
'SELECT, to find unique IDs (PRODUCT, SHIPTO, TIMEID, DATATYPE)
Dim cmdA As OleDbCommand = New OleDbCommand("SELECT PRODUCT, SHIPTO, TIMEID, DATATYPE INTO tmp IN 'C:\Users\youngje\Documents\SQL Server Management Studio\Projects\Nwind.accdb'" & _
"FROM tblFactSales " & _
"WHERE (BILLTO = 'INPUT_BILLTO') AND (BRANCHPLANT = 'INPUT_BRANCHPLANT') AND (FRTHANDLE = 'INPUT_FRTHANDLE') AND (DATATYPE = 'FORECAST') AND (TIMEID >= '" & fRng & "' )" & _
"AND (SIGNEDDATA >= - .01) AND (SIGNEDDATA <= .01) AND (SALESDATA = 'short_tons')", con)
'DELETE, joined with cmdA results
Dim cmdB As OleDbCommand = New OleDbCommand("DELETE tblFactSales.* " & _
"FROM tblFactSales INNER JOIN tmp T " & _
"ON tblFactSales.PRODUCT=T.PRODUCT AND tblFactSales.SHIPTO=T.SHIPTO AND tblFactSales.TIMEID=T.TIMEID", con)
'Execute Queries
cmdA.ExecuteNonQuery()
cmdB.ExecuteNonQuery()
'Clean Up
cmdA.Dispose()
cmdB.Dispose()
con.Close()
GC.Collect()
'Confirmation
MessageBox.Show("Records Removed Successfully.", "Clear Complete", _
MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
End Sub