先生,我在执行db代码时遇到错误 [英] Sir, I getting error while executing db code
问题描述
protected void Button2_Click(object sender,EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString);
conn.Open();
string insert =insert into Table(用户名,电子邮件,密码,国家/地区)值(@ uname,@ email,@ pwd,@ country);
SqlCommand com = new SqlCommand(insert,conn);
com.Parameters .AddWithValue( @ UNAME,un.Text);
com.Parameters.AddWithValue( @电子邮件,eid.Text);
com.Parameters.AddWithValue ( @pwd,pwd.Text);
com.Parameters.AddWithValue( @国,country.SelectedItem.ToString());
com.ExecuteNonQuery ();
Response.Redirect(data.aspx);
R esponse.Write(注册成功);
conn.Close();
}
catch(exception ex)
{
Response.Write(error+ ex.ToString());
}
}
错误:
errorSystem.Data.SqlClient.SqlException(0x80131904):不正确关键字Table附近的语法。在System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,Action`1 wrapCloseInAction)在System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection,Action`1 wrapCloseInAction)在System.Data.SqlClient的System.Data.SqlClient上的System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj,Boolean& dataReady)中的.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,Boolean callerHasConnectionLock,Boolean asyncClose) System.Data.SqlClient.SqlCommand.RunExecuteReaderTds中的.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,String resetOptionsString)(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task& task,Boolean asyncWrite,SqlDataReader ds,布尔值describeParameterEncryptionRequest)at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,TaskCompletionSource`1 completion,Int32 timeout,Task&任务,布尔asyncWrite)在System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1完成,字符串methodName中,布尔sendToPipe,的Int32超时,布尔asyncWrite)在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()在web.register。 Button2_Click(对象发件人,EventArgs e)如E:\TECOMP VI\web\web\web\register.aspx.cs:线31 ClientConnectionId:cc386255-98b2-42bb-8105-a2c5a76799dd错误号:156,州:1,等级:15
我尝试过:
先生,我在执行db代码时遇到错误,请检查此
protected void Button2_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
string insert = "insert into Table(username,email,password,country) values (@uname,@email,@pwd,@country)";
SqlCommand com = new SqlCommand(insert, conn);
com.Parameters.AddWithValue("@uname", un.Text);
com.Parameters.AddWithValue("@email", eid.Text);
com.Parameters.AddWithValue("@pwd", pwd.Text);
com.Parameters.AddWithValue("@country", country.SelectedItem.ToString());
com.ExecuteNonQuery();
Response.Redirect("data.aspx");
Response.Write("Registration successful");
conn.Close();
}
catch (Exception ex)
{
Response.Write("error" + ex.ToString());
}
}
error:
errorSystem.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near the keyword 'Table'. 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, SqlDataReader dataStream, 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, Boolean describeParameterEncryptionRequest) at System.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 web.register.Button2_Click(Object sender, EventArgs e) in E:\TECOMP VI\web\web\web\register.aspx.cs:line 31 ClientConnectionId:cc386255-98b2-42bb-8105-a2c5a76799dd Error Number:156,State:1,Class:15
What I have tried:
sir,i getting error while executing db code,plz check this
推荐答案
您的sql正在插入一个名为Table的表中。如果这是正确的,你的表真的被调用,那么解决方案是将表重命名为适当的类似用户。无论如何,当你使用关键词作为表或字段名称时,你需要将它们放在方括号中,这样SQL就知道你的字面意思是
Your sql is inserting into a table literally called Table. If that is right and your table is really called that then the solution is to rename the table to something appropriate like "User". Regardless, when you use key words as table or field names you need to put them in square brackets so SQL knows you mean them literally
string insert = "insert into [Table](username,email,password,country) values (@uname,@email,@pwd,@country)";
如果您的表未被称为Table,则只需使用正确的名称替换Table。
If your table isn't called Table then simply replace Table with the correct name.
这篇关于先生,我在执行db代码时遇到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!