更新网格视图时不更新所有表

时间:2014-04-24 09:23:57

标签: c# vb.net winforms gridview

我在应用程序的窗口上工作..我正在填写我的数据网格视图:

 Dim cd As SqlCommandBuilder = New SqlCommandBuilder(adapter)
        adapter = New SqlDataAdapter("select c.cid,c.CompanyName,l.LocName as Location,d.dtId,d.dtName as Department,d.dtPhone as Phone,d.dtEmail as Email,l.Locid from CompanyMaster_tbl c join  DepartmentMaster_tbl d on c.Cid=d.cId  join Location_tbl l on l.Locid=c.locid where d.Deleted =0 and c.Deleted=0 order by cid", con.connect)

        dt1 = New DataTable
        bSource = New BindingSource
        adapter.Fill(dt1) 'Filling dt with the information from the DB
        bSource.DataSource = dt1
        gv.DataSource = bSource
        gv.Columns("cid").Visible = False
        gv.Columns("dtId").Visible = False
        gv.Columns("Locid").Visible = False

在更新按钮中我有这样的代码:

adapter = New SqlDataAdapter()

        Dim cid As Integer
        Dim dtid As Integer
        Dim cmpname As String
        Dim dtname As String
        Dim dtPhone As String
            Dim dtEmail As String
            Dim LocName As String

            Dim Locid As Integer

        For i As Integer = 0 To gv.RowCount - 2

            Dim rv = DirectCast(gv.Rows(i).DataBoundItem, DataRowView)
            cid = rv.Row.Field(Of Integer)("Cid")

            dtid = rv.Row.Field(Of Integer)("dtId")
                cmpname = rv.Row.Field(Of String)("CompanyName")
                LocName = rv.Row.Field(Of String)("Location")
            dtname = rv.Row.Field(Of String)("Department")
            dtPhone = rv.Row.Field(Of String)("Phone")
            dtEmail = rv.Row.Field(Of String)("Email")
                Locid = rv.Row.Field(Of Integer)("Locid")


                adapter.UpdateCommand = New SqlCommand("UPDATE CompanyMaster_tbl SET CompanyName = @CompanyName", con.connect)
                adapter.UpdateCommand = New SqlCommand("Update Location_tbl Set LocName=@LocName where Locid=@Locid ", con.connect)

adapter.UpdateCommand = New SqlCommand("update DepartmentMaster_tbl set dtName = @dtName,dtPhone = @dtPhone,dtEmail = @dtEmail where dtId=@dtid", con.connect)
            adapter.UpdateCommand.Parameters.AddWithValue("@Cid", cid)
                adapter.UpdateCommand.Parameters.AddWithValue("@CompanyName", cmpname)
                adapter.UpdateCommand.Parameters.AddWithValue("@LocName", LocName)
            adapter.UpdateCommand.Parameters.AddWithValue("@dtId", dtid)
            adapter.UpdateCommand.Parameters.AddWithValue("@dtName", dtname)
            adapter.UpdateCommand.Parameters.AddWithValue("@dtPhone", dtPhone)
                adapter.UpdateCommand.Parameters.AddWithValue("@dtEmail", dtEmail)
                adapter.UpdateCommand.Parameters.AddWithValue("@Locid", Locid)

            adapter.UpdateCommand.ExecuteNonQuery()

            Next

点击更新按钮时我没有收到任何错误..我可以更新我的DepartmentMaster_tbl ..但是没有更新我的Location_tbl ..我的代码有什么问题.. 任何帮助都非常明显..谢谢

2 个答案:

答案 0 :(得分:1)

因为你覆盖你的UpdateCommands然后执行ExecuteNonQuery,它将使用当前可用的UpdateCommand(在这种情况下是最后一个)

答案 1 :(得分:0)

对我来说,看起来你在设置后会覆盖 UpdateCommand 两次。 您使用UPDATE CompanyMaster_tbl进行设置,然后使用UPDATE Location_tbl覆盖它,然后使用update DepartmentMaster_tbl再次覆盖它, 正如预期的那样,只执行最后一个。

使用SqlCommand代替SqlAdapter的解决方案:

using (SqlConnection DBConn = new SqlConnection("connection string")
{
    DBConn.Open();
    using (SqlTransaction DBTran = DBConn.BeginTransaction)
    {
        using (SqlCommand DBCmd = new SqlCommand("your first statement here", DBConn))
        {
            DBCmd.Transaction = DBTran;
            // set only the parameters needed for this table
            ...
            // execute statement
            DBCmd.ExecuteNonQuery();
        }
        // repeat the above block for the other tables
        ...
        // commit transaction to database
        DBTran.Commit();
    }
}