将Datagridview中的命令更新到数据库

时间:2014-02-24 01:58:25

标签: sql vb.net datagridview

到目前为止我一直在处理我的数据库问题,由于无法通过datagridview更新我的数据库,我无法继续使用新模块

有人可以查看我的代码,为什么我无法使用按钮点击在datagridview中更新我的数据库?

这是我的数据网格内容的方式:

Public Sub loaddgvfrm3()
    cmdconn = New SqlConnection
    cmd = New SqlCommand
    cmdconn.ConnectionString = sqlstr
    cmdconn.Open()
    cmd.Connection = cmdconn
    cmd.CommandText = "select  period, VOUCH_AMT, INDIVIDUAL_AMT, check_no, D_MAILED, DIR_NO, who_updated, claim_no,  year_student from tobee.EBD_BILLHISTORY where CLAIM_NO like '" + txtClaimno.Text + "'"
    'cmd.CommandText = "select  a.period, a.VOUCH_AMT, a.INDIVIDUAL_AMT, a.check_no, a.bal_ent, a.D_MAILED, a.DIR_NO from tobee.EBD_BILLHISTORY a left join carlos.claims_waivee b on a.CLAIM_NO = b.claim_no where b.CLAIM_NO like '" + claimno.ToString + "'"
    Dim dt As New DataTable
    'Dim ds As New DataSet
    da = New SqlDataAdapter
    da.SelectCommand = cmd
    da.Fill(dt)
    Me.DataGridView1.DataSource = dt
    Me.DataGridView2.DataSource = dt
    cmdconn.Close()
End Sub

现在我想修改该datagridview中的一些行,并使用buttonclick更新我在数据库中所做的更改

这是我的按钮点击代码:

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click


    Dim connstr As String = "server=midtelephone\sqlexpress; database=testdb; user= sa; password=sa;"
    Dim cmd2 As New SqlCommand

    Try
        Using connection As New SqlConnection(connstr)
            connection.Open()
            cmd2.CommandText = "update tobee.EBD_BILLHISTORY set period=@period, vouch_amt=@vouch_amt, individual_amt=@individual_amt, check_no=@check_no, d_mailed=@d_mailed, dir_no=@dir_no, who_updated=@who_updated where claim_no = '" + txtClaimno.Text + "' "

            cmd2.Parameters.Add("@period", SqlDbType.Char, 25, "period")
            cmd2.Parameters.Add(New SqlParameter("@vouch_amt", SqlDbType.Decimal) With {.Precision = 18, .Scale = 2}).Value = "vouch_amt"
            cmd.Parameters.Add(New SqlParameter("@individual_amt", SqlDbType.Decimal) With {.Precision = 18, .Scale = 2}).Value = "individual_amt"

            cmd2.Parameters.Add("@check_no", SqlDbType.Char, 15, "check_no")
            'cmd2.Parameters.Add("@d_mailed", SqlDbType.DateTime, "d_mailed")
            cmd2.Parameters.Add("@dir_no", SqlDbType.Char, 15, "DIR_NO")
            cmd2.Parameters.Add("@who_updated", SqlDbType.Char, 15, "who_updated")
            cmd2.ExecuteNonQuery()
            connection.Close()
            MsgBox("changes done")
            'MessageBox.Show("Updated Successfully!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information)

        End Using
    Catch ex As Exception
    msgbox(ex.message)
    End Try

End Sub

发生的问题“executiontenonquery:连接属性尚未初始化” 什么可能是我的按钮保存的问题?(我不知道如果我正确的做事,我是否连接这两个“潜艇”正确?)如果你有任何其他建议(更新)我在datagridview中所做的更改。随意修改保存按钮:(

1 个答案:

答案 0 :(得分:1)

您似乎忘了设置Connection的{​​{1}}属性。只需在cmd2之后添加此行:

connection.Open()

更新:

您可以尝试使用cmd2.Connection = connection DataAdapter函数将DataTable作为参数传递,以便将更改从DataTable提交到数据库:

Update()

[MSDN Reference]