【问题标题】:I am having trouble inserting into my database with c#我无法使用 c# 插入我的数据库
【发布时间】:2014-01-22 02:06:49
【问题描述】:

尝试将数据插入数据库时​​出现此错误。

System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的未处理异常

附加信息:关键字“用户”附近的语法不正确。

代码如下:

if(txtRegisterSecurityAnswerOne.TextLength >0 && txtRegisterSecurityAnswerTwo.TextLength >0)
{
    SqlConnection connection1 = new SqlConnection(
        Properties.Settings.Default.BlackBookDBConnectionString);

    System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
    cmd.CommandType = System.Data.CommandType.Text;
    cmd.CommandText = "INSERT INTO User (Username, Password, SecurityQuestionOne, "
        + "SecurityQuestionTwo, SecurityAnswerOne, SecurityAnswerTwo); VALUES ("
        + txtRegisterUsername.Text + ","
        + txtRegisterPassword.Text + ","
        + lstRegisterSecurityQuestionOne.SelectedText + ","
        + lstRegisterSecurityQuestionTwo.SelectedItem + ","
        + txtRegisterSecurityAnswerOne.Text + ","
        + txtRegisterSecurityAnswerTwo.Text + ")";
    cmd.CommandText = "INSERT INTO USer ()";
    cmd.Connection = connection1;

    connection1.Open();
    cmd.ExecuteNonQuery();
    connection1.Close();
}

我已经编辑了我的代码。但是由于某种原因,它仍然没有向我的数据库中插入任何内容。

if(txtRegisterSecurityAnswerOne.TextLength >0 && txtRegisterSecurityAnswerTwo.TextLength >0)
{
    SqlConnection connection1 = new SqlConnection(Properties.Settings.Default.BlackBookDBConnectionString);

    string sqlquery = "INSERT INTO [User] (Username,Password,SecurityQuestionOne,"
        + "SecurityAnswerOne,SecurityQuestionTwo,SecurityAnswerTwo) "
        + "VALUES (@Username,@Password,@QuestionOne,@AnswerOne,@QuestionTwo,@AnswerTwo)";
    SqlCommand command = new SqlCommand(sqlquery, connection1);

    string userName = txtRegisterUsername.Text;
    command.Parameters.AddWithValue("Username", userName);

    string password = txtRegisterRepeatPassword.Text;
    command.Parameters.AddWithValue("Password", password);

    string questionOne = lstRegisterSecurityQuestionOne.SelectedText;
    command.Parameters.AddWithValue("QuestionOne", questionOne);

    string questionTwo = lstRegisterSecurityQuestionTwo.SelectedText;
    command.Parameters.AddWithValue("QuestionTwo", questionTwo);

    string answerOne = txtRegisterSecurityAnswerOne.SelectedText;
    command.Parameters.AddWithValue("AnswerOne", answerOne);

    string answerTwo = txtRegisterSecurityAnswerTwo.SelectedText;
    command.Parameters.AddWithValue("AnswerTwo", answerTwo);

    command.Connection = connection1;

    connection1.Open();
    command.ExecuteNonQuery();
    connection1.Close();
}

【问题讨论】:

  • 使用占位符。 缺乏使用是(或者更确切地说,一个)直接问题(因为没有正确引用值),使用他们避免将来出现问题 - 意外或恶意 SQL 注入。
  • 您正在乞求 SQL 注入攻击。请考虑使用参数进行内联查询。 stackoverflow.com/questions/4624811/…
  • 你的连接字符串是什么?
  • SqlConnection connection1 = new SqlConnection(Properties.Settings.Default.BlackBookDBConnectionString);连接1.Open();
  • 你的连接字符串是什么样子的?(它的内容)

标签: c# sql-server database


【解决方案1】:

删除这一行:

cmd.CommandText = "INSERT INTO USer ()";

编辑
查看新代码后,您的参数名称错误(缺少@)。您应该将代码更改为:

string userName = txtRegisterUsername.Text;
command.Parameters.AddWithValue("@Username", userName);

string password = txtRegisterRepeatPassword.Text;
command.Parameters.AddWithValue("@Password", password);

string questionOne = lstRegisterSecurityQuestionOne.SelectedText;
command.Parameters.AddWithValue("@QuestionOne", questionOne);

string questionTwo = lstRegisterSecurityQuestionTwo.SelectedText;
command.Parameters.AddWithValue("@QuestionTwo", questionTwo);

string answerOne = txtRegisterSecurityAnswerOne.SelectedText;
command.Parameters.AddWithValue("@AnswerOne", answerOne);

string answerTwo = txtRegisterSecurityAnswerTwo.SelectedText;
command.Parameters.AddWithValue("@AnswerTwo", answerTwo);

【讨论】:

  • 您不需要所有直接的局部变量,但使用占位符是可行的方法。我还将包括使用此类绑定的更新 SQL 语句。
  • 其实我确实注意到了,但是它仍然没有插入。
  • 是的,它告诉我用户附近的语法无效
  • sqlquery 在您的新代码中似乎没有任何问题,您已将方括号放在那里 ([User])
【解决方案2】:
  1. 删除第二行分配cmd.CommandText - 它覆盖第一行
  2. User 是 SQL Server 中的关键字,如果您有一个具有该名称的表(您不应该)将其括在方括号中:

    cmd.CommandText = "INSERT INTO [User] ...

附带说明 - 了解 parametrized queries。它们是避免 SQL 注入攻击的好方法(更不用说混乱的字符串连接)

【讨论】:

  • 我已经更新了我的代码,你可以看看它,看看你现在能不能发现它有什么问题?
  • @user3221702,如果您阅读 Yuriy 提供的链接,您至少会发现一件事是错误的。
【解决方案3】:
if(txtRegisterSecurityAnswerOne.TextLength >0 && txtRegisterSecurityAnswerTwo.TextLength >0)
{
    SqlConnection connection1 = new SqlConnection(Properties.Settings.Default.BlackBookDBConnectionString);
    connection1.Open();
    string sqlquery = "INSERT INTO [User] (Username,Password,SecurityQuestionOne,"
        + "SecurityAnswerOne,SecurityQuestionTwo,SecurityAnswerTwo) "
        + "VALUES (@Username,@Password,@QuestionOne,@AnswerOne,@QuestionTwo,@AnswerTwo)";

    SqlCommand command = new SqlCommand(sqlquery, connection1);

    string userName = txtRegisterUsername.Text;
    command.Parameters.Add("@Username", SqlDbType.VarChar, 200).Value = userName;
    string password = txtRegisterRepeatPassword.Text;
    command.Parameters.Add("@Password", SqlDbType.VarChar, 200).Value = password;
    string questionOne = lstRegisterSecurityQuestionOne.SelectedText;
    command.Parameters.Add("@QuestionOne", SqlDbType.VarChar, 200).Value = questionOne;
    string questionTwo = lstRegisterSecurityQuestionTwo.SelectedText;
    command.Parameters.Add("@QuestionTwo", SqlDbType.VarChar, 200).Value = questionTwo;
    string answerOne = txtRegisterSecurityAnswerOne.SelectedText;
    command.Parameters.Add("@AnswerOne", SqlDbType.VarChar, 200).Value = answerOne;
    string answerTwo = txtRegisterSecurityAnswerTwo.SelectedText;
    command.Parameters.Add("@AnswerTwo", SqlDbType.VarChar, 200).Value = answerTwo;

    command.ExecuteNonQuery();
    connection1.Close();
}

【讨论】:

    猜你喜欢
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-23
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多