的ExecuteNonQuery()抛出"近关键字'用户'"语法不正确; [英] ExecuteNonQuery() throws "Incorrect syntax near the keyword 'User'"
问题描述
我开始学习C#上周,现在我有使用INSERT SQL语句的麻烦。我用下面的代码,那么我告诉你发生了什么。
私人无效AddNewUserToDataBase(用户用户)
{
字符串的CommandText =INSERT INTO用户(名字,姓氏,ADRESS,镇,favoriteCurrency)+
VALUES(@名字,@姓氏,@ ADRESS,@镇@ favoriteCurrency);使用(SqlConnection的连接=新的SqlConnection(全球:: Laboratoire1.Properties.Settings.Default.Database1ConnectionString))
{
使用
(的SqlCommand命令=新的SqlCommand())
{
command.Connection =连接;
command.CommandText =的CommandText;
command.CommandType = CommandType.Text;
//济莱过时ICI参数应用倒éviter莱erreurs金杜需要等级字符串COMMANDE
command.Parameters.Add(新的SqlParameter(@名字,user.getFirstName( ))); //他者永宏加上兴业
command.Parameters.Add(新的SqlParameter(@姓氏,user.getLastName()));
command.Parameters.Add(新的SqlParameter(@住址」,user.getAdress()));
command.Parameters.Add(新的SqlParameter(@镇,user.getTown()));
command.Parameters.Add(新的SqlParameter(@ favoriteCurrency,user.getFavoriteCurrencyId()));
试
{
connection.Open();
command.ExecuteNonQuery();
的Connection.close();
}
赶上(SQLEXCEPTION EX)
{
MessageBox.Show(内容Une ERREUR s'est produite。);
}
}
}
}
我的程序不崩溃和异常消息框出现。我试图了解我的错误,并改变我command.Parameters.Add的语法,但它仍然无法正常工作:/
改变catch块显示异常,而不是吞咽下去后,它抛出:
不正确的语法关键字附近用户
块引用>
解决方案您已经有了一个名为用户一表,但这是保留关键字一个。
您可以重命名表,或将其包含在方括号当你引用它:
字符串的CommandText =INSERT [用户](名字...
I started learning C# last week and I'm now having trouble with an INSERT sql statement. I used the following code, then I tell you what happens.
private void AddNewUserToDataBase(User user) { string commandText = "INSERT INTO User (firstName,lastName,adress,town,favoriteCurrency) " + "VALUES (@firstName,@lastName,@adress,@town,@favoriteCurrency)"; using (SqlConnection connection = new SqlConnection(global::Laboratoire1.Properties.Settings.Default.Database1ConnectionString)) { using (SqlCommand command = new SqlCommand()) { command.Connection = connection; command.CommandText = commandText; command.CommandType = CommandType.Text; // je passe les paramètres ici pour éviter les erreurs au niveau du string commande command.Parameters.Add(new SqlParameter("@firstName", user.getFirstName())); // autre façon plus générale command.Parameters.Add(new SqlParameter("@lastName", user.getLastName())); command.Parameters.Add(new SqlParameter("@adress", user.getAdress())); command.Parameters.Add(new SqlParameter("@town", user.getTown())); command.Parameters.Add(new SqlParameter("@favoriteCurrency", user.getFavoriteCurrencyId())); try { connection.Open(); command.ExecuteNonQuery(); connection.Close(); } catch (SqlException ex) { MessageBox.Show("Une erreur s'est produite."); } } } }
My program do not crashes and the exception message box shows up. I've trying to understand my error and changing the syntax of my command.Parameters.Add, but it still doesn't work :/.
After changing the catch block to display the exception instead of swallowing it, it throws:
Incorrect syntax near the keyword 'User'
解决方案You've got a table with the name "User", but that's a reserved keyword.
You could rename the table, or enclose it in brackets when you reference it:
string commandText = "INSERT INTO [User] (firstName ...
这篇关于的ExecuteNonQuery()抛出"近关键字'用户'"语法不正确;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!