C#如何更新访问表
本文关键字:更新 访问表 何更新 | 更新日期: 2023-09-27 18:01:08
具有多列的UPDATE语句应该如何,必须更新?(因为一行改变了很多(
我在这个板上尝试了一些搜索,但在其他问题中,有时在列周围是"[]",有时不是,但这两个版本在我的代码中都不起作用。
这是代码:
try
{
string name = mtb_Name.Text.ToString();
string altname = mtb_AltName.Text.ToString();
string licht = cb_Licht.SelectedItem.ToString();
string boden = cb_Boden.SelectedItem.ToString();
string haerte = cb_Haerte.SelectedItem.ToString();
string pg = cb_PG.SelectedItem.ToString();
string hoehe = mtb_Hoehe.Text.ToString();
string form = cb_Form.SelectedItem.ToString();
string zuechter = cb_Zuechter.SelectedItem.ToString();
string land = cb_Land.SelectedItem.ToString();
string gruppe = cb_Gruppe.SelectedItem.ToString();
//the connection works, I can add stuff in other tables and delete stuff everywhere
parent.GetDBConnection().Open();
OleDbCommand Query = new OleDbCommand();
Query.Connection = parent.GetDBConnection();
Query.Parameters.Clear();
//I build this string at the moment for testing purposes only and
//converted everything and putted in a string to be sure.
//later it will be replaced with the Parameters.Add(,)
//I also did test it with [AltNameRose] as columname instead of AltNameRose
//or wrote instead of the ' ' a "'"" around the strings,
//but doesn´t seem to be the problem.
Query.CommandText = "UPDATE tb_Rose SET" +
" ,AltNameRose = '" + altname +
"' ,NameZuechter = '" + haerte +
"' ,Boden = '" + boden +
"' ,Wuchshoehe = '" + hoehe +
"' ,Farbe = '" + " " +
"' ,Foto = '" + " " +
"' ,Licht = '" + licht +
"' ,Preisgruppe = '" + pg +
"' ,Gruppe = '" + gruppe +
" WHERE NameRose = '" + name + "'";
//the CommandText seems to be missing something, but I don´t know what.
MessageBox.Show(Query.CommandText.ToString());
Query.ExecuteNonQuery();
parent.GetDBConnection().Close();
MessageBox.Show("Rose successfully edited.");
this.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
parent.GetDBConnection().Close();
}
抛出的异常是
"UPDATE命令中的语法错误
tb_Rose表如下所示:NameRose | AltNameRose | NameZuechter | Frosthalte | Boden | Wuchshoehe | Farbe | Foto | Licht | Preisgruppe | Gruppe
关于另一个问题,我如何传递列也很重要,但这对我来说并不奏效。
有人告诉我错误在哪里吗?
您不需要在第一列之前使用逗号。这就是为什么你;
" ,AltNameRose = '"
应该是
" AltNameRose = '"
但更重要的是,您应该始终使用参数化查询。这种字符串串联对SQL注入攻击是开放的。
也可以使用using
语句自动处理连接和命令,而不是手动调用.Close()
方法。
Update语句的语法为
Update table_name SET
column_name = value,
column_name = value,
....
Where condition
在您的查询中,您在SET关键字后面放了一个,
。
SET关键字后面有一个CCD_ 4。。尝试低于代码
Query.CommandText = "UPDATE tb_Rose SET" +
" AltNameRose = '" + altname +
"' ,NameZuechter = '" + haerte +
"' ,Boden = '" + boden +
"' ,Wuchshoehe = '" + hoehe +
"' ,Farbe = '" + " " +
"' ,Foto = '" + " " +
"' ,Licht = '" + licht +
"' ,Preisgruppe = '" + pg +
"' ,Gruppe = '" + gruppe +
" WHERE NameRose = '" + name + "'";