通过vb错误更新sql表

时间:2014-02-01 10:29:57

标签: sql vb.net sql-update

我的老师给了我以下代码来修改数据:

    If Datatable.rows.count<>0 Then
    datatable.rows(rowposition)("company name") = txtcompanyname.text

dataadapter.update(datatable)
Msgbox("Record has been updated")
End if 
End sub

这是我尝试的但是它不起作用:

 Dim companyname As String
    If reader.Read Then
        reader.Close()
        companyname = txtCompanyName.Text
        sqlstring = "UPDATE `client_details` SET `companyname` =  @companyname "
        objcommand = New MySqlCommand(sqlstring, objconnection)
        objcommand.Parameters.AddWithValue("@companyname", companyname)
        objcommand.ExecuteNonQuery()
    Else
        MsgBox("Unable to add details", MsgBoxStyle.Critical, "Updating Failed")
        reader.Close()
        Me.Cursor = Cursors.Arrow

基本上我的表格中有一个数据网格视图,我有一个对应于列的文本框,所以当我点击一条记录时,所有细节都会进入相应的文本框,那么我想要做的就是更改一个文本框的详细信息并按下更新按钮以备记录?我不知道,一直在研究和使用试验和错误,但没有修复?

编辑1:

objconnection.Open()
        Dim companytype As String
        Dim vatregistrationnumber As String
        Dim payeandtaxreference As String
        Dim addressline1 As String
        Dim city As String
        Dim postcode As String
        Dim phonenumber As String
        Dim email As String
        postcode = txtPostcode.Text
        sqlstring = "UPDATE `client_details` SET companytype=@companytype, vatregistrationnumber=@vat, payeandtaxreference=@paye, addressline1=@address, city=@city, postcode=@postcode, phonenumber=@phone, email=@email"
        objcommand = New MySqlCommand(sqlstring, objconnection)
        objcommand.Parameters.AddWithValue("@companytype", CompanyType)
        objcommand.Parameters.AddWithValue("@vat", vatregistrationnumber)
        objcommand.Parameters.AddWithValue("@paye", payeandtaxreference)
        objcommand.Parameters.AddWithValue("@address", addressline1)
        objcommand.Parameters.AddWithValue("@city", city)
        objcommand.Parameters.AddWithValue("@postcode", postcode)
        objcommand.Parameters.AddWithValue("@phone", phonenumber)
        objcommand.Parameters.AddWithValue("@email", email)
        objcommand.ExecuteNonQuery()
        objconnection.Close()
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

这样写:

 objconnection.Open()
        Dim companyname As String
        Dim companytype As String
        Dim vatregistrationnumber As String
        Dim payeandtaxreference As String
        Dim addressline1 As String
        Dim city As String
        Dim postcode As String
        Dim phonenumber As String
        Dim email As String
        postcode = txtPostcode.Text
        companyname = txtCompanyName.Text
        companytype = cbxCompanyType.Text
        payeandtaxreference = txtPAYE.Text
        vatregistrationnumber = txtVAT.Text
        addressline1 = txtAddressLine.Text
        city = txtCity.Text
        phonenumber = txtPhoneNumber.Text
        email = txtEmail.Text
        sqlstring = "UPDATE client_details SET companytype=@companytype, vatregistrationnumber=@vat, payeandtaxreference=@paye, addressline1=@address, city=@city, postcode=@postcode, phonenumber=@phone, email=@email where companyname=  @companyname "
        objcommand = New MySqlCommand(sqlstring, objconnection)
        objcommand.Parameters.AddWithValue("@companyname", companyname)
        objcommand.Parameters.AddWithValue("@companytype", CompanyType)
        objcommand.Parameters.AddWithValue("@vat", vatregistrationnumber)
        objcommand.Parameters.AddWithValue("@paye", payeandtaxreference)
        objcommand.Parameters.AddWithValue("@address", addressline1)
        objcommand.Parameters.AddWithValue("@city", city)
        objcommand.Parameters.AddWithValue("@postcode", postcode)
        objcommand.Parameters.AddWithValue("@phone", phonenumber)
        objcommand.Parameters.AddWithValue("@email", email)
        objcommand.ExecuteNonQuery()
        objconnection.Close()