本文介绍了如何使用c#.net将数据插入到ms SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,



我正在使用visual studio 2015,我在向SQL服务器数据库插入数据时遇到问题。

这是我的代码:

Dear all,

I am using visual studio 2015, I have a problem with insert data to database of SQL server.
Here is my code:

try
        {
            SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Mohammad\Desktop\loginpage\App_Data\CMS.mdf;Integrated Security=True");
            conn.Open();
            string insert_query = "insert into Table (username,email,password,country) values (@username ,@email ,@password ,@country)";
            SqlCommand cmd = new SqlCommand(insert_query, conn);
            cmd.Parameters.AddWithValue("@username", t_username.Text);
            cmd.Parameters.AddWithValue("@email", t_email.Text);
            cmd.Parameters.AddWithValue("@password", t_password.Text);
            cmd.Parameters.AddWithValue("@country", D_country.SelectedItem.ToString());

            cmd.ExecuteNonQuery();
            Response.Redirect("manegar.aspx");
            Response.Write("Registration Is Successfully");
            conn.Close();
        }
        catch (Exception ex)
        {
            Response.Write("ERORR:" + ex.ToString());

        }
    }
}





我尝试过:



ERORR:System.Data.SqlClient.SqlException(0x80131904):关键字Table附近的语法不正确。在System.Data.SqlClient.SqlClient上的System.Data.SqlClient.SqlConnection.OnError(SqlException异常,Boolean breakConnection,Action`1 wrapCloseInAction)处于System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,Boolean breakConnection,Action`1 wrapCloseInAction) 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,Boolean isInternal,Boolean forDescribeParameterEncryption)(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task& task,布尔asyncWrite,Bool System.Data.SqlClient.SqlCommand.RunExecuteReader中的ean inRetry,SqlDataReader ds,Boolean describeParameterEncryptionRequest)(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String方法,TaskCompletionSource`1完成,Int32超时,任务&任务,布尔& usedCache,Boolean asyncWrite,Boolean inRetry)在System.Data.SqlClient的System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1完成,String methodName,Boolean sendToPipe,Int32 timeout,Boolean& usedCache,Boolean asyncWrite,Boolean inRetry)中。登录时的SqlCommand.ExecuteNonQuery()。在c:\ Users \Mohammad \Desktop\loginpage\login.aspx.cs中的对象发件人,EventArgs e:第33行ClientConnectionId:fb623e67-d06e-45d3-9133 -53f264d26604错误号:156,状态:1,类:15



What I have tried:

ERORR:System.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, Boolean isInternal, Boolean forDescribeParameterEncryption) at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, Boolean inRetry, 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& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at login.Button1_Click(Object sender, EventArgs e) in c:\Users\Mohammad\Desktop\loginpage\login.aspx.cs:line 33 ClientConnectionId:fb623e67-d06e-45d3-9133-53f264d26604 Error Number:156,State:1,Class:15

推荐答案

string insert_query = "insert into [Table] (username,email,password,country) values (@username ,@email ,@password ,@country)";


这篇关于如何使用c#.net将数据插入到ms SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 20:37