1

我对 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 上收到“记录已删除”错误。它创建/填充 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
4

3 回答 3

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
于 2012-08-07T16:17:21.427 回答
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 表或为什么要以这种方式删除数据感到困惑。但是试一试这些查询,看看它们是否有帮助。

于 2012-07-12T19:57:33.053 回答
0

我并不肯定,但在查看 DataAdapter.Fill 的函数原型时,我无法想象这会填充数据库中的表,而是可以在 .NET 中迭代的客户端数据集。

与其尝试对客户端结果集执行连接,不如使用 SQL 创建临时表?许多 RDBMS 都支持这样的特性。特别是想到的方言是:

SELECT * INTO #tempTable FROM table;

这不会返回结果集,而是在连接上填充临时表(可能在基于数据库系统的事务中)。然后可以将临时表与该连接的其他表连接起来,并在 SQL/DML 语句中使用。

除此之外,在选择和稍后执行的删除之间的目标并不完全清楚。两个查询中的 where 子句是相同的,因此如果在 select 和 delete 之间没有执行任何处理或数据更改,那么加入两者的想法是没有必要的。您可能能够简单地使用 WHERE 子句而不是连接执行 DELETE 语句,并实现相同的目标。

希望这可以帮助!

于 2012-07-12T18:30:22.843 回答