未处理的SQLException 列名无效
在Winform 的修改DATAGRIDVIEW 更新DS 和数据库中的值的函数中,总是出现了如下问题,百思不得其解
public bool update()
{
SqlParameter para = new SqlParameter();
da.UpdateCommand = myConnection.CreateCommand();
da.UpdateCommand.CommandText = "UPDATE pnametable SET met=@met,explain=@explain,pname=@pname,price=@price WHERE aoto_ID=@autoid";
da.UpdateCommand.Parameters.Add("@met", SqlDbType.VarChar, 50, "met");
da.UpdateCommand.Parameters.Add("@explain", SqlDbType.Text, 16,"explain");
da.UpdateCommand.Parameters.Add("@price",SqlDbType.Int,4,"price");
da.UpdateCommand.Parameters.Add("@pname",SqlDbType.VarChar,50,"pname");
para = da.UpdateCommand.Parameters.Add("@autoid", SqlDbType.Int,4, "aoto_ID");
para.SourceVersion = DataRowVersion.Original;
//MessageBox.Show(da.UpdateCommand.CommandText); 如果该行执行结果为正常的SQL语句,语句如下
int count = da.Update(ds.Tables["scores2"]);
//MessageBox.Show(ds.Tables["scores2"].Rows.Count.ToString()); 如果该行执行结果为行数,DS正常
bool result = count > 0 ? true : false;
return result;
//return true;
}
出错症状为
int count = da.Update(ds.Tables["scores2"]); 该行
未处理的SQLException
列名 'met' 无效。
列名 'aoto_ID' 无效。
注释掉的 MESSAGEBOX得到的SQL语句 UPDATE pnametable SET met=@met,explain=@explain,pname=@pname,price=@price WHERE aoto_ID=@autoid
其中数据库中的KEY为 aoto_ID
在ds.Tables["scores2"] 表中,所需要的数据和字段都正常存在
以下是DS的赋值函数
private SqlConnection myConnection = new SqlConnection("server=192.168.0.223;database=Tasaki_zealo;pwd=sa;uid=sa");
private SqlCommand myComd;
private SqlDataAdapter da = null;
private DataSet ds = new DataSet();
......
public void showPicList_all(string did)
{
dataGridView2.Columns.Clear();
string strSel = "SELECT * FROM picdb";
da = new SqlDataAdapter(strSel, myConnection);
ds = new DataSet();
da.Fill(ds, "scores2");
dataGridView2.DataSource = ds.Tables["scores2"].DefaultView;
}