C# 如何更新访问 table
C# How to update an access table
多列的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();
}
抛出的异常是
"Syntaxerror in UPDATE-Command
table tb_Rose 看起来像这样:
NameRose|AltNameRose|NameZuechter|Frosthaerte|Boden|Wuchshoehe|Farbe|Foto||Licht|Preisgruppe|Gruppe
另一个问题是,命令,
我如何通过专栏也很重要,
但它不适合我。
有没有人给个小提示哪里有错误?
您不需要在第一列前逗号 只是。这就是为什么你的;
" ,AltNameRose = '"
应该是
" AltNameRose = '"
但更重要的是,您应该始终使用 parameterized queries. This kind of string concatenations are open for SQL Injection 攻击。
还可以使用 using
statement 自动处理您的连接和命令,而不是手动调用 .Close()
方法。
更新语句的语法是
Update table_name SET
column_name = value,
column_name = value,
....
Where condition
在您的查询中,您在 SET 关键字后放置了一个 ,
。
SET 关键字后有一个 ,
.. 试试下面的代码
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 + "'";
多列的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();
}
抛出的异常是
"Syntaxerror in UPDATE-Command
table tb_Rose 看起来像这样: NameRose|AltNameRose|NameZuechter|Frosthaerte|Boden|Wuchshoehe|Farbe|Foto||Licht|Preisgruppe|Gruppe
另一个问题是,命令, 我如何通过专栏也很重要, 但它不适合我。
有没有人给个小提示哪里有错误?
您不需要在第一列前逗号 只是。这就是为什么你的;
" ,AltNameRose = '"
应该是
" AltNameRose = '"
但更重要的是,您应该始终使用 parameterized queries. This kind of string concatenations are open for SQL Injection 攻击。
还可以使用 using
statement 自动处理您的连接和命令,而不是手动调用 .Close()
方法。
更新语句的语法是
Update table_name SET
column_name = value,
column_name = value,
....
Where condition
在您的查询中,您在 SET 关键字后放置了一个 ,
。
SET 关键字后有一个 ,
.. 试试下面的代码
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 + "'";