因此,我对C#还是很陌生。我的页面上有一个gridview行。编辑数据后,我还希望在链接到它的访问数据库中也更新它。我收到此错误:UPDATE语句中的语法错误。我认为我的约会应该受到指责,但仍然...我无法找出我做错了什么。
这是我的更新行函数的代码:
protected void OnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string id = (row.Cells[0].Controls[0] as TextBox).Text;
string nume = (row.Cells[1].Controls[0] as TextBox).Text;
string prenume = (row.Cells[2].Controls[0] as TextBox).Text;
string data = (row.Cells[3].Controls[0] as TextBox).Text;
DataTable dt = ViewState["dt"] as DataTable;
//dt.Rows[row.RowIndex]["ID"] = id;
dt.Rows[row.RowIndex]["Nume"] = nume;
dt.Rows[row.RowIndex]["Prenume"] = prenume;
dt.Rows[row.RowIndex]["Data Nasterii"] = data;
ViewState["dt"] = dt;
GridView1.EditIndex = -1;
OleDbConnection con; // create connection
OleDbCommand com; // create command
con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db\db1.mdb");
con.Open();
DateTime date = Convert.ToDateTime(data);
com = new OleDbCommand("Update Table1 set Nume=" + nume + " , Prenume=" + prenume + ", Data Nasterii= @date where ID=" + id, con);
com.Parameters.AddWithValue("@date", OleDbType.Date).Value=data;
com.ExecuteNonQuery();
con.Close();
this.BindGrid();
Response.Write("alert('DATA UPDATED')");
}
谁能帮我?
最佳答案
如果列名包含两个单词,则需要在其中使用方括号。喜欢;
[Data Nasterii] = @date
但是更重要重要,您应该始终使用parameterized queries。此类字符串连接对SQL Injection攻击开放。
我看到您已将
data
值参数化,也将其他值也参数化。还可以使用
using
statement处置OleDbConnection
和OleDbCommand
。using(OleDbConnection con = new OleDbConnection(conString))
using(OleDbCommand cmd = con.CreateCommand())
{
// Set your CommandText property.
// Define and add your parameter values.
// Open your OleDbConnection.
// Execute your query.
}
关于c# - C#asp.net中UPDATE语句中的语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24798574/