0

在我的表单中,当我修改数据时出现错误,因为 “没有为命令对象设置命令文本”

我正在使用 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

但我没有找到我要去的地方....请帮助我

4

1 回答 1

0

你没有设置usqlucmd任何地方。

ucmd.CommandText = usql;

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

ucmd = New OleDbCommand(isql, conn)

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

于 2012-09-28T14:35:36.340 回答