我正在写作,因为我在访问数据库中更新记录时遇到问题。我使用select(按id)搜索记录,然后在datagridview中显示它。接下来我想更改它在datagrid视图中并使用更改的数据集更新更改的记录。当我试图这样做时,我有例外:
“OledbCommand.Prepare方法要求所有可变长度参数都有明确设置的非零大小。”
这是我的代码:
private void button2_Click(object sender, EventArgs e)
{
connection = new OleDbConnection(connetionString);
connection.Open();
string sel = "SELECT * from Table where Id=@Id";
oledbAdapter = new OleDbDataAdapter();
OleDbCommand cmd = new OleDbCommand(sel, connection);
cmd.Parameters.Add("@Id", OleDbType.Char).Value = textBox1.Text;
oledbAdapter.SelectCommand = cmd;
oledbAdapter.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
connection.Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
DataTable ndt = new DataTable();
OleDbCommandBuilder scb;
private void button1_Click(object sender, EventArgs e)
{
connection = new OleDbConnection(connetionString);
connection.Open();
DataSet changes;
changes = ds.GetChanges();
ds.AcceptChanges();
if (changes != null)
{
OleDbCommandBuilder builder = new OleDbCommandBuilder(oledbAdapter);
oledbAdapter.UpdateCommand = builder.GetUpdateCommand();
oledbAdapter.Update(changes);
ds.AcceptChanges();
}
ds.AcceptChanges();
MessageBox.Show("Saved");
编辑: 谢谢你,你真的帮了我,但我现在有不同的例外:D
查询表达式中的语法错误(缺少运算符)'((ID =?)AND((?= 1 AND Payment ID IS NULL)OR(Payment ID =?))AND((?= 1 AND CharNo IS NULL)OR(CharNo =?))AND((?= 1 AND CharName IS NULL)OR(CharName =?))AND((?= 1 AND MC IS NULL)OR(MC =?))AND((? = 1 AND姓名是NU'。
当我添加
builder.QuotePrefix = "[";
builder.QuoteSuffix = "]";
我有
标准表达式中的数据类型不匹配。
当我试图更新..
答案 0 :(得分:0)
尝试更改ID字段的数据类型并为其指定长度。
cmd.Parameters.Add("@Id", OleDbType.VarChar, 25).Value = textBox1.Text;