[英]I can't add text to sql database on c#
I can't add text from website on sql database 我无法从SQL数据库的网站添加文本
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. 说明:执行当前Web请求期间发生未处理的异常。 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'. 异常详细信息:System.Data.SqlClient.SqlException:关键字'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... 在第27行出现错误,请帮助我...
The database server tells you that you're using the keyword Table
in a place where it should not appear. 数据库服务器告诉您在不应该出现的地方使用了关键字
Table
。 You might be able to convince the database server that Table
is the name for a table by wrapping it in brackets: [Table]
. 通过将其包装在括号中,您可以使数据库服务器确信
Table
是Table
的名称: [Table]
。
Edit: If you're using MySQL, you might want to use 编辑:如果您使用的是MySQL,则可能要使用
String insert = "INSERT INTO `Table` (Name,Surname,E...
instead. 代替。
Firstly, note that SqlConnection
is the ADO.Net client to SQL Server, not MySQL. 首先,请注意
SqlConnection
是SQL Server的ADO.Net客户端,而不是MySQL。 If you really need to connect to MySQL, you'll need: MySqlConnection
. 如果确实需要连接到MySQL,则需要:
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. 问题在于Table是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).
如果要将表的名称更改为MyTable,则代码将正常工作(假设其他地方没有其他错误)。
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 如果您想知道使用TSQL时应避免使用的单词,请参见以下指向MSDN的链接: https : //msdn.microsoft.com/zh-cn/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?
取而代之的是,提供一个更有意义的名称并描述其内容-在您的示例和所提供的列的情况下,“联系方式”可能是更好的选择?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.