C# sql语法错误-无法将字符串附加到数据库
我目前正在进行一个虚拟项目,其中我正在制作一个登录屏幕。除了学习一些C#和sql之外,我对这个项目没有什么大的意图 我目前正在尝试将一个新用户附加到包含每个用户名及其密码的数据库中,但由于某种原因,我收到了一条错误消息 文本框中写入的条目应存储在数据库中,但由于某种原因,这种情况不会发生 我得到一个错误,说明我有一个语法错误,我不知道我是否理解C# sql语法错误-无法将字符串附加到数据库,c#,sql,database,syntax-error,C#,Sql,Database,Syntax Error,我目前正在进行一个虚拟项目,其中我正在制作一个登录屏幕。除了学习一些C#和sql之外,我对这个项目没有什么大的意图 我目前正在尝试将一个新用户附加到包含每个用户名及其密码的数据库中,但由于某种原因,我收到了一条错误消息 文本框中写入的条目应存储在数据库中,但由于某种原因,这种情况不会发生 我得到一个错误,说明我有一个语法错误,我不知道我是否理解 private void create_user_username_box_Leave(object sender, EventArgs e) {
private void create_user_username_box_Leave(object sender, EventArgs e)
{
// Add user/password to database when when someone leaves the area.
using (DbConnection connection = new SqlConnection(@"Server=localhost\SQLEXPRESS01;Database=master;Trusted_Connection=True;"))
{
connection.Open();
using (DbCommand command = new SqlCommand("INSERT INTO [dbo].[information] (id,password) VALUES ("+create_user_username_textbox.Text+","+create_user_password_textbox.Text+");"))
{
command.Connection = connection;
command.ExecuteNonQuery(); // System.Data.SqlClient.SqlException: 'Incorrect syntax near ')'.'
}
}
}
这些值是字符串,因此生成的SQL命令文本应将它们括在单引号内
VALUES ('"+create_user_username_textbox.Text+"','"...
但是,您确实应该参数化查询,以防止Sql注入攻击的可能性
将字符串更改为:
VALUES (@id,@pw)"))
将参数添加到命令:
command.Parameters.Add(new SqlParameter("@id", create_user_username_textbox.Text));
command.Paramaters.Add(new SqlParameter("@pw", create_user_password_textbox.Text));
试试这个-
private void create_user_username_box_Leave(object sender, EventArgs e)
{
// Add user/password to database when when someone leaves the area.
using (SqlConnection connection = new SqlConnection(@"Server=localhost\SQLEXPRESS01;Database=master;Trusted_Connection=True;"))
{
connection.Open();
using (SqlCommand command = new SqlCommand("INSERT INTO [dbo].[information] (id,password) VALUES ("+create_user_username_textbox.Text+","+create_user_password_textbox.Text+");"))
{
command.Connection = connection;
command.ExecuteNonQuery(); // System.Data.SqlClient.SqlException: 'Incorrect syntax near ')'.'
}
}
}
永远不要做以下事情
"INSERT INTO [dbo].[information] (id,password)
VALUES (" + someStringVariable + "," + someOtherStringVariable + ")"
想想你在这里做什么——你把用户输入的任何文本直接输入到你的查询字符串中。这是删除数据库或使其包含的所有信息被盗的最简单方法
相反,使用准备好的语句
var commandText = "INSERT INTO [dbo].[information] (id,password) VALUES (@Username, @Password)"
using (var command = new SqlCommand(commandText, connection))
{
command.Parameters.Add("@Username", SqlDbType.VarChar).Value = create_user_username_textbox.Text
command.Parameters.Add("@Password", SqlDbType.VarChar).Value = create_user_password_textbox.Text
command.ExecuteNonQuery();
}
您也应该强烈考虑不要在纯文本
中存储密码。更新时建议替换
参数。AddWithValue
-显然,如果数据库中的列类型不同,请相应地设置它您的语法错误将是因为您没有在SQL中的字符串值周围添加单引号。另外,研究如何使用SQL参数来构造SQL语句——像这样使用串联会使您容易受到SQL注入攻击,通常被认为是一件非常糟糕的事情代码>也需要从查询字符串中删除!我似乎收到了错误消息:方法“Add”不重载2个参数
@SteveHarrisnote另外,使用参数而不是字符串连接还有其他好处:除了保护您免受SQLInjection的影响外,它还可以避免您需要转义用户可以插入的“特殊”字符(如果用户插入Shaquille O'Neal
作为用户名怎么办?)更新-需要是新的SqlParameterAddWithValue
似乎不会通过自动完成弹出?我认为这是因为您已将命令
变量指定为DbCommand
,使用var
或SqlCommand
instead@Due无论如何,更新了@Crowcoder,并不是100%确定关于va长度的最佳实践但是rchars(例如,如果您扩展了最大用户名长度,那么进行检查和更新会很痛苦)您不需要设置长度,它将自动使用输入数据的长度。我几乎使用了重载,即name和sqldbtype,然后设置值
属性,就像您在编辑中所做的那样。答案与OP的问题相同!