c# - C#asp.net中UPDATE语句中的语法错误

标签 c# asp.net compiler-errors oledb

因此,我对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处置OleDbConnectionOleDbCommand
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/

相关文章:

c# - 使用通用存储库和存储过程

c# - 为什么在第二种情况下不执行简单的 linq 命令?

c# - 动态操作<T> : Invalid Arguments when executed

c# - 在 Web 应用程序中传递用户信息的最佳方式是什么?

asp.net - "Site is under construction"asp.net 站点页面

c++ - 在 Xcode 4 中使用和引用多个 cpp 文件

c - 为什么这不是重复符号错误?

c# - DDD : How should adding and removing related entities be modelled?

triggers - 触发编译错误(插入)

asp.net - 微软.AspNet.WebApi.Cors : The requested resource does not support http method 'OPTIONS'