SQL 语法错误 -

本文关键字:错误 语法 SQL | 更新日期: 2023-09-27 18:32:46

>我正在尝试执行此查询,但不断收到此错误;

errorSystem.Data.SqlClient.SqlException (0x80131904(:关键字"user"附近的语法不正确。 at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction) at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action 1 wrapCloseInAction( at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(

TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose( at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReaderdataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady( at System.Data.SqlClient.SqlCommand.FinishExecuteReader (SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString( at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds( atSystem.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource 1 completion, Int32 timeout, Task& task, Boolean asyncWrite) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource 1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite( at System.Data.SqlClient.SqlCommand.ExecuteNonQuery(( at Registration.RegistrationPage.Button1_Click1(Object sender, EventArgs e( in c:''Users''kristhnen.jagambrum''Documents''Visual Studio 2012''Projects''Registration''Registration''注册页面.aspx.cs:第 52 行 客户端连接 ID:8c00c1af-b9a7-477b-881f-f849a8c0d0ec

我尝试执行的查询是;

SqlConnection conn = new  SqlConnection(ConfigurationManager.ConnectionStrings["userinfo.ConnectionString"].ConnectionString);
conn.Open();
// string uname = usernametextbox.Text;
// string fname = fnametextbox.Text;
// string lname = lnametextbox.Text;
// string email = emailtextbox.Text;
// string ppassword = passwordtextbox.Text;
// string ccountry = DropDownListcountry.SelectedItem.Text;
string insertQuery = "INSERT INTO user (UserName, FirstName, LastName, Email, Password, Country) VALUES (@uname, @fname, @lname, @email, @password, @country)";
SqlCommand comm = new SqlCommand(insertQuery, conn);
//int temp = Convert.ToInt32(comm.ExecuteScalar().ToString());
comm.Parameters.AddWithValue("@uname", usernametextbox.Text);
comm.Parameters.AddWithValue("@fname", fnametextbox.Text);
comm.Parameters.AddWithValue("@lname", lnametextbox.Text);
comm.Parameters.AddWithValue("@email", emailtextbox.Text);
comm.Parameters.AddWithValue("@country", DropDownListcountry.ToString());
comm.Parameters.AddWithValue("@password", passwordtextbox.Text);
comm.ExecuteNonQuery();
// Response.Redirect("manager.aspx");
conn.Close();

SQL 语法错误 -

用户一个关键字,如果是这样的话,将表名包装在[]

"INSERT INTO [user] (UserName, FirstName, LastName, Email, Password, Country)
             VALUES (@uname, @fname, @lname, @email, @password, @country)";

或者您也可以使用此样式

"INSERT INTO [dbo].[user] (UserName, FirstName, LastName, Email, Password, Country) 
    VALUES (@uname, @fname, @lname, @email, @password, @country)";