我是VB.NET的新手,我希望这是一个简单的问题。我正在尝试查询数据库,将结果存储到数据集,然后查询相同的数据库,并加入结果以执行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上收到“Record is deleted”错误。它创建/填充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
答案 0 :(得分:0)
我不是肯定的,但是在查看DataAdapter.Fill的函数原型时,我无法想象这会填满数据库中的表,而是可以迭代的客户端数据集。 NET。
不是尝试对客户端结果集执行连接,而是使用SQL创建临时表是否合适?许多RDBMS都支持这样的功能。特别想到的方言是:
SELECT * INTO #tempTable FROM table;
这不会返回结果集,而是在连接上填充临时表(可能在基于数据库系统的事务中)。然后,临时表可以与该连接的其他表连接,并在SQL / DML语句中使用。
除此之外,选择和后续执行删除之间的目标并不完全清楚。 where子句在两个查询中都是相同的,因此如果在select a delete之间没有执行任何处理或数据更改,则加入两者的想法是无偿的。您可以简单地使用WHERE子句而不是连接执行DELETE语句,并实现相同的目标。
希望这有帮助!
答案 1 :(得分:0)
我使用SSMS重新设计了查询格式和可读性。在代码中重新整合它们,看看它们是否有用。
INSERT INTO tmp
(
PRODUCT
,SHIPTO
,TIMEID
,DATATYPE
)
SELECT PRODUCT
,SHIPTO
,TIMEID
,DATATYPE
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')
DELETE tblFactSales
WHERE EXISTS ( SELECT 'True'
FROM tmp T
WHERE tblFactSales.PRODUCT = T.PRODUCT
AND tblFactSales.SHIPTO = T.SHIPTO
AND tblFactSales.TIMEID = T.TIMEID )
我会说我仍然对使用tmp表的原因或者为什么要以这种方式删除数据感到困惑。但请仔细阅读这些查询,看看它们是否有用。
答案 2 :(得分:0)
我有几件事情错了,但我终于开始工作了。这就是我最终的目标。
Private Sub cmdDelete_Click(sender As System.Object, e As System.EventArgs) Handles cmdDelete.Click
Dim Con As SqlConnection = New SqlConnection("Server = OVP-L-R8MXE5M\SQLEXPRESS;" & "Database = dbTest;" & "Trusted_Connection=TRUE")
Con.Open()
Dim Yr, Mnth, fRng As String
Yr = Year(Now)
Mnth = Format(Month(Now), "00")
fRng = Yr + Mnth + "00"
Dim cmdA As SqlCommand = New SqlCommand("SELECT PRODUCT, SHIPTO, TIMEID, DATATYPE INTO tmp " & _
"FROM tblFactSales " & _
"WHERE (BILLTO = 'INPUT_BILLTO') AND (BRANCHPLANT = 'INPUT_BRANCHPLANT') AND (FRTHANDLE = 'INPUT_FRTHANDLE') AND (DATATYPE = 'FORECAST') AND (RPTCURRENCY = 'USD') AND (TIMEID >= '" & fRng & "') " & _
"AND (SIGNEDDATA >= - .01) AND (SIGNEDDATA <= .01) AND (SALESDATA = 'short_tons')", Con)
Dim cmdB As SqlCommand = New SqlCommand("DELETE tblFactSales " & _
"FROM tblFactSales RIGHT JOIN tmp " & _
"ON tblFactSales.PRODUCT=tmp.PRODUCT AND tblFactSales.SHIPTO=tmp.SHIPTO AND tblFactSales.TIMEID=tmp.TIMEID AND tblFactSales.DATATYPE=tmp.DATATYPE", Con)
cmdA.ExecuteNonQuery()
cmdB.ExecuteNonQuery()
MessageBox.Show("Records Removed Successfully.", "Clear Complete", _
MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)
End Sub