SQL命令运行正常,结果不会更新

时间:2014-06-11 18:16:10

标签: sql vb.net

我从这里获取了代码:

http://www.vbforums.com/showthread.php?469872-Retrieving-and-Saving-Data-in-Databases

修改它以使用我的数据库并正确更新表。我可以看到这部分在调试模式下工作。但是,当我尝试修改数据库时,它会突破代码而不会发生任何实际更新或错误。如何调试运行正常的东西?

代码:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim sConnectionString As String = "Data Source=.\SQLExpress;AttachDbFilename=C:\CorrectFileLocation\Database.mdf;Trusted_Connection=Yes;"
    Dim sSelAllCmd As String = "Select * From List_Tbl"

    Dim connection As New SqlConnection(sConnectionString)
    Dim adapter As New SqlDataAdapter(sSelAllCmd, connection)
    Dim indextable As New DataTable

    adapter.Fill(indextable)

    Dim update As New SqlCommand("UPDATE List_Tbl SET List_Index = @List_Index WHERE List_Id = @List_Id" , connection)

    update.Parameters.Add("@List_Index", SqlDbType.SmallInt)
    update.Parameters("@List_Index").Value = 99
    update.Parameters.Add("@List_Id", SqlDbType.BigInt)
    update.Parameters("@List_Id").Value = 2

    adapter.UpdateCommand = update

    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey

    adapter.Update(indextable)

End Sub

结束班

在调试期间查看索引表显示正确的表。该表是名称dbo.List_Tbl。我是否需要更新功能的dbo?

╔═════════╦════════════╦════════╗
║ List_Id ║ List_Index ║ Stuff  ║
╠═════════╬════════════╬════════╣
║       1 ║          4 ║ Name A ║
║       2 ║          5 ║ Name B ║
║       3 ║          6 ║ Name C ║
╚═════════╩════════════╩════════╝

2 个答案:

答案 0 :(得分:1)

修改adapter.Update()对象后,

DataTable可用于更新数据库

如果您只想执行SQL Update,请执行以下操作:

Using connection
    connection.Open()
    update.ExecuteNonQuery()
End Using

所以最终的代码如下:

Imports System
Imports System.Data
Imports System.Data.SqlClient

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim sConnectionString As String = "Data Source=.\SQLExpress;AttachDbFilename=C:\CorrectFileLocation\Database.mdf;Trusted_Connection=Yes;"
    Dim sSelAllCmd As String = "Select * From List_Tbl"

    Dim connection As New SqlConnection(sConnectionString)
    Dim adapter As New SqlDataAdapter(sSelAllCmd, connection)
    Dim indextable As New DataTable

    adapter.Fill(indextable)

    Dim update As New SqlCommand("UPDATE List_Tbl SET List_Index = @List_Index WHERE List_Id = @List_Id" , connection)

    update.Parameters.Add("@List_Index", SqlDbType.SmallInt)
    update.Parameters("@List_Index").Value = 99
    update.Parameters.Add("@List_Id", SqlDbType.BigInt)
    update.Parameters("@List_Id").Value = 2

     Using connection
         connection.Open()
         update.ExecuteNonQuery()
     End Using

End Sub

答案 1 :(得分:1)

仅当传递的表包含RowState属性设置为Modified的行时,DataAdapter类才会执行UpdateCommand。

您刚刚阅读了indexTable,因此所有行的属性RowState都设置为Unchanged。在这种情况下,在adapter.Update调用期间,由于没有任何更新,因此不执行UpdateCommand。 (对于使用相应属性的InsertCommand和DeleteCommand也是如此)

如果您打算直接更改某一行,那么您根本不需要适配器服务。您可以直接在SqlCommand上调用ExecuteNonQuery

Dim update As New SqlCommand(@"UPDATE List_Tbl 
                               SET List_Index = @List_Index 
                               WHERE List_Id = @List_Id" , connection)

update.Parameters.Add("@List_Index", SqlDbType.SmallInt)
update.Parameters("@List_Index").Value = 99
update.Parameters.Add("@List_Id", SqlDbType.BigInt)
update.Parameters("@List_Id").Value = 2
update.ExecuteNonQuery();