Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP连接错误:关键字';使用者_C#_Asp.net_Sql Server - Fatal编程技术网

C# ASP连接错误:关键字';使用者

C# ASP连接错误:关键字';使用者,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我犯了个错误 System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理 其他信息:关键字“User”附近的语法不正确 我的代码: SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\user\OneDrive\Documents\Visual Studio

我犯了个错误

System.Data.dll中发生“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理

其他信息:关键字“User”附近的语法不正确

我的代码:

SqlConnection con = new SqlConnection(@"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename = C:\Users\user\OneDrive\Documents\Visual Studio 2015\Projects\WebApplication2\WebApplication2\App_Data\Database1.mdf; Integrated Security = True");

protected void btnOK_Click(object sender, EventArgs e)
{
    con.Open();

    SqlCommand cmd = con.CreateCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText =  "insert into User(name, status, type) values('"+txtName.Text+"', '"+txtStatus.Text+ "', '" + txtType.Text + "')";

    cmd.ExecuteNonQuery();
    con.Close();
    Response.Redirect("Webform1.aspx");
}

User
是SQL中的保留关键字,应使用方括号

试试这个:

 cmd.CommandText =  "insert into [User](name, status, type) values('"+txtName.Text+"', '"+txtStatus.Text+ "', '" + txtType.Text + "')";

快乐的sql注入请不要给出一个从一开始就让人上sql注入火车的答案。公平地说,OP从一开始就已经在sql注入序列中。关于未知实例是用户输入类型的一些有疑问的假设。坚持手头的问题。解决方括号的问题可能是微不足道的。让OP满意地离开充满问题的代码是一种伤害,并且没有达到本网站上答案所期望的水平。在开始编写将按钮单击事件与SqlCommand对象混合的代码之前,请阅读此内容。-您不应该将SQL语句连接在一起,而应该使用参数化查询来避免SQL注入