1. 加try,catch
2. 在insert,update中必须要表的主键
3.
string str=("insert into xxx values("+xxx+")";
//string str=("update xxx set xxx= ...."+")";
//string str=("delete xxx from xxx=...."+")";SqlCommand cmd = new SqlCommand(str,conn);
try {
conn.Open();
cmd.ExecuteNoQuery;
conn.Close()
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
2. 在insert,update中必须要表的主键
3.
string str=("insert into xxx values("+xxx+")";
//string str=("update xxx set xxx= ...."+")";
//string str=("delete xxx from xxx=...."+")";SqlCommand cmd = new SqlCommand(str,conn);
try {
conn.Open();
cmd.ExecuteNoQuery;
conn.Close()
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
sda.InsertCommand = new sqlCommand("Insert ...",scon);
sda.UpdateCommand = new ...
...
是这样的,我是取了一个数据集然后用DataGrid显示出来,进行编辑,然后用自动更新到数据库里,但有时报错。如果是一条记录我觉得用上面二位介绍的方法很好,如果这个DataGrid中有一千行数据,进行了不同的编辑,比如修改,删除,是不是仍旧能像上面那样做呢?