Adapter.UpdateCommand不工作-VB.NET

Adapter.UpdateCommand不工作-VB.NET,vb.net,Vb.net,我是一个初学者,我有一个问题。我不想更改数据库中的数据,当我向数据库传递参数时,我使用适配器。selectCommand和request成功执行。但当我将代码更改为adapter.UpdateCommand时,数据库中并没有数据,所以无法工作。源和数据库为:VB.NET和SQL 这是我的代码: con.Close() Dim com As New SqlCommand Dim adp As New SqlDataAdapter Dim sqlDS As New

我是一个初学者,我有一个问题。我不想更改数据库中的数据,当我向数据库传递参数时,我使用适配器。selectCommand和request成功执行。但当我将代码更改为adapter.UpdateCommand时,数据库中并没有数据,所以无法工作。源和数据库为:VB.NET和SQL

这是我的代码:

    con.Close()

    Dim com As New SqlCommand
    Dim adp As New SqlDataAdapter
    Dim sqlDS As New Data.DataSet

    con.Open()
    Dim startTrasaction = con.BeginTransaction

    Try

        exe = "basicInsert"

        com = New SqlCommand(exe, con)
        com.CommandType = CommandType.StoredProcedure

        com.Parameters.AddWithValue("@ID", Integer.Parse(txtID.Text))
        com.Parameters.AddWithValue("@Name", CStr(txtName.Text))
        com.Parameters.AddWithValue("@LastName", CStr(txtLastName.Text))
        com.Parameters.AddWithValue("@Tel", Integer.Parse(txtTel.Text))
        com.Parameters.AddWithValue("@Emali", (txtMail.Text))
        com.Parameters.AddWithValue("@Status", CStr(txtStatus.Text))
        com.Parameters.AddWithValue("@State", CStr(txtState.Text))
        com.Parameters.AddWithValue("@Town", CStr(txtTown.Text))
        com.Parameters.AddWithValue("@Adress", CStr(txtAdress.Text))
        com.Parameters.AddWithValue("@StateID", (txtStateID.Text))



        com.Transaction = startTrasaction

        adp.UpdateCommand = com   ' NOT WORKING



        'adp.SelectCommand = com   ' WORKING FINE


        adp.Fill(sqlDS)


        adp.Dispose()
        com.Dispose()


        startTrasaction.Commit()


    Catch ex As Exception
        MessageBox.Show(ex.Message)
        startTrasaction.Rollback()

    Finally

        con.Close()

        txtID.ResetText()
        txtName.ResetText()
        txtLastName.ResetText()
        txtTel.ResetText()
        txtMail.ResetText()
        txtStatus.ResetText()
        txtState.ResetText()
        txtTown.ResetText()
        txtAdress.ResetText()

    End Try

这是一个常见的误解。只有在传递给方法的DataTable或DataSet包含更改的行时,才会在服务中调用
SqlDataAdapter.UpdateCommand

在上面的场景中,您只是在数据集中加载数据,这很好,但是数据集中没有任何更改,并且您不调用
SqlDataAdapter.Update
方法

如果要在数据库中插入新行,只需调用SqlCommand的
ExecuteNonQuery
方法

试一试


你能给我举一个例子说明如何使用:SqlDataAdapter.Update和SqlDataAdapter.UpdateCommand方法吗?我很感激上面关于Update方法的MSDN文章的链接是一个很好的起点(请参见更新(数据集)中的示例,在这里你还可以找到关于底层机制的注释)
    exe = "basicInsert"

    com = New SqlCommand(exe, con)
    com.CommandType = CommandType.StoredProcedure

    com.Parameters.AddWithValue("@ID", Integer.Parse(txtID.Text))
    com.Parameters.AddWithValue("@Name", CStr(txtName.Text))
    com.Parameters.AddWithValue("@LastName", CStr(txtLastName.Text))
    com.Parameters.AddWithValue("@Tel", Integer.Parse(txtTel.Text))
    com.Parameters.AddWithValue("@Emali", (txtMail.Text))
    com.Parameters.AddWithValue("@Status", CStr(txtStatus.Text))
    com.Parameters.AddWithValue("@State", CStr(txtState.Text))
    com.Parameters.AddWithValue("@Town", CStr(txtTown.Text))
    com.Parameters.AddWithValue("@Adress", CStr(txtAdress.Text))
    com.Parameters.AddWithValue("@StateID", (txtStateID.Text))

    com.ExecuteNonQuery()

    .... the following adapter.SelectCommand should be able to see the new record