简体   繁体   中英

I can't add text to sql database on c#

I can't add text from website on sql database

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString);
    String insert = "INSERT INTO dbo.Table (Name,Surname,Email,Mobile,Telephone) VALUES (@name,@surname,@email,@mobile,@telephone)";
    SqlCommand command = new SqlCommand(insert, connection);

    command.Parameters.AddWithValue("@name", Name.Text);
    command.Parameters.AddWithValue("@surname", Surname.Text);
    command.Parameters.AddWithValue("@email", Email.Text);
    command.Parameters.AddWithValue("@mobile", Mobile.Text);
    command.Parameters.AddWithValue("@telephone", Telephone.Text);
    connection.Open();
    command.ExecuteNonQuery();
    connection.Close();
}
}

It shows me this error Server Error in '/' Application. Incorrect syntax near the keyword 'Table'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'Table'.

Source Error:

Line 25:      command.Parameters.AddWithValue("@telephone", Telephone.Text);
Line 26:         connection.Open();
Line 27:         command.ExecuteNonQuery();
Line 28:         connection.Close();
Line 29:     }

Error its on line 27 Please help me...

The database server tells you that you're using the keyword Table in a place where it should not appear. You might be able to convince the database server that Table is the name for a table by wrapping it in brackets: [Table] .

Edit: If you're using MySQL, you might want to use

String insert = "INSERT INTO `Table` (Name,Surname,E...

instead.

Firstly, note that SqlConnection is the ADO.Net client to SQL Server, not MySQL. If you really need to connect to MySQL, you'll need: MySqlConnection .

Make sure Table is the actual name of your table. If it is, escape it like: [Table]

Plus, don't forget to dispose the connection:

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString))
     {
            String insert = "INSERT INTO [dbo].[Table] (Name,Surname,Email,Mobile,Telephone) VALUES (@name,@surname,@email,@mobile,@telephone)";
            SqlCommand command = new SqlCommand(insert, connection);

            command.Parameters.AddWithValue("@name", Name.Text);
            command.Parameters.AddWithValue("@surname", Surname.Text);
            command.Parameters.AddWithValue("@email", Email.Text);
            command.Parameters.AddWithValue("@mobile", Mobile.Text);
            command.Parameters.AddWithValue("@telephone", Telephone.Text);
            connection.Open();
            command.ExecuteNonQuery();
    }

The problem is that Table is a reserved word in SQL. Therefore the system thinks that it is a command and not the name of a table. If you were to change the name of the table to MyTable the code would work correctly (assuming that there are no other errors elsewhere).

This code works with a test database and the updated table name:

INSERT INTO MyTable (Name,Surname,Email,Mobile,Telephone) 
VALUES (@name,@surname,@email,@mobile,@telephone);

If you wish to know which words you should avoid when using TSQL see this link to MSDN: https://msdn.microsoft.com/en-us/library/ms189822.aspx

Although as suggested by another respondent you could escape the table name to use it you would be best advised NOT to do so. It is a very bad code smell and can lead to many other problems and confusions later on. Instead, give a name that makes more sense and it descriptive of the contents - in the case of your example and the columns provided perhaps 'Contacts' might have been a better choice?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM