在VB.Net中更新记录时出错

时间:2012-09-28 14:31:41

标签: vb.net oledb

在我的表单中,当我修改数据时,我收到错误  '没有为命令对象

设置命令文本

我正在使用oledb连接

关注我们的代码

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click    
Try
        sql = "select * from Employee"
        conn = New OleDbConnection(s)
        adapt = New OleDbDataAdapter(sql, conn)
        ds = New DataSet

        usql = "Update Employee set enp_name=?, emp_addr=?, DOB=?, DOJ=?, emp_mob=?, emp_mail=? where ID=?"
        ucmd = New OleDbCommand(isql, conn)

        Dim date1 As Date
        date1 = DateTimePicker1.Value

        p = ucmd.Parameters.Add("@emp_name", OleDbType.Char)
        p.SourceColumn = "emp_name"
        p.SourceVersion = DataRowVersion.Current

        p = ucmd.Parameters.Add("@emp_addr", OleDbType.Char)
        p.SourceColumn = "emp_addr"
        p.SourceVersion = DataRowVersion.Current

        p = ucmd.Parameters.Add("@DOB", OleDbType.DBDate)
        p.SourceColumn = "DOB"
        p.SourceVersion = DataRowVersion.Current

        p = ucmd.Parameters.Add("@DOJ", OleDbType.DBDate)
        p.SourceColumn = "DOJ"
        p.SourceVersion = DataRowVersion.Current

        p = ucmd.Parameters.Add("@emp_mob", OleDbType.Integer)
        p.SourceColumn = "emp_mob"
        p.SourceVersion = DataRowVersion.Current

        p = ucmd.Parameters.Add("@emp_mail", OleDbType.Char)
        p.SourceColumn = "emp_mail"
        p.SourceVersion = DataRowVersion.Current

        p = ucmd.Parameters.Add("@ID", OleDbType.Integer)
        p.SourceColumn = "ID"
        p.SourceVersion = DataRowVersion.Current

        adapt.UpdateCommand = ucmd

        conn.Open()
        If conn.State = ConnectionState.Open Then
            adapt.MissingSchemaAction = MissingSchemaAction.AddWithKey
            adapt.Fill(ds, "ABC")
            'conn.Close()
        End If

        tables = ds.Tables
        table = tables("ABC")
        rows = table.Rows
        cols = table.Columns

        row = rows.Find(TextBox1.Text)
        row.BeginEdit()
        row("ID") = TextBox1.Text
        row("emp_name") = TextBox2.Text
        row("emp_addr") = TextBox3.Text
        row("DOB") = DateTimePicker1.Text
        row("DOJ") = DateTimePicker2.Text
        row("emp_mob") = TextBox4.Text
        row("emp_mail") = TextBox5.Text
        row.EndEdit()
        'conn.Open()
        adapt.Update(ds, "ABC")** Getting error on this line 
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
  End Sub

但我没有得到我出错的地方......请帮帮我

1 个答案:

答案 0 :(得分:0)

您没有将usql设置为任何地方的ucmd

ucmd.CommandText = usql;

或者更确切地说,在你的构造函数中:

ucmd = New OleDbCommand(isql, conn)

isql应该是usql。我猜你的类中有一个isql字符串字段,如果你没有收到编译错误。