我有插入我的数据库用C#麻烦 [英] I am having trouble inserting into my database with c#

查看:106
本文介绍了我有插入我的数据库用C#麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到这个错误尝试将数据插入到我的数据库时。




类型的未处理的异常System.Data.SqlClient的。 SQLEXCEPTION出现在system.data.dll



更多信息:关键字附近有语法错误。用户




下面是代码:

 如果(txtRegisterSecurityAnswerOne.TextLength大于0和放大器;&安培; txtRegisterSecurityAnswerTwo.TextLength大于0)
{
SqlConnection的连接1 =新的SqlConnection(
Properties.Settings.Default.BlackBookDBConnectionString);

System.Data.SqlClient.SqlCommand CMD =新System.Data.SqlClient.SqlCommand();
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText =INSERT INTO用户(用户名,密码,SecurityQuestionOne,
+SecurityQuestionTwo,SecurityAnswerOne,SecurityAnswerTwo)VALUES(
+ txtRegisterUsername.Text +,
+ txtRegisterPassword.Text +,
+ lstRegisterSecurityQuestionOne.SelectedText +,
+ lstRegisterSecurityQuestionTwo.SelectedItem +,
+ txtRegisterSecurityAnswerOne.Text +,
+ txtRegisterSecurityAnswerTwo.Text +);
cmd.CommandText =INSERT INTO用户();
cmd.Connection =连接1;

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



我已经编辑我的代码。但是它仍然没有插入任何东西到我的数据库由于某种原因

 如果(txtRegisterSecurityAnswerOne.TextLength大于0和放大器;&安培; txtRegisterSecurityAnswerTwo.TextLength大于0)
{
SqlConnection的连接1 =新的SqlConnection(Properties.Settings.Default.BlackBookDBConnectionString);

字符串的SQLQuery =INSERT INTO [用户](用户名,密码,SecurityQuestionOne,
+SecurityAnswerOne,SecurityQuestionTwo,SecurityAnswerTwo)
+VALUES(@用户名,@口令,@ QuestionOne,@ AnswerOne,@ QuestionTwo,@ AnswerTwo);
的SqlCommand命令=新的SqlCommand(sqlquery的,连接1);

字符串username = txtRegisterUsername.Text;
command.Parameters.AddWithValue(用户名,用户名);

字符串密码= txtRegisterRepeatPassword.Text;
command.Parameters.AddWithValue(密码,密码);

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

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

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

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

command.Connection =连接1;

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


解决方案

 如果(txtRegisterSecurityAnswerOne.TextLength大于0和放大器;&安培; txtRegisterSecurityAnswerTwo.TextLength大于0)
{
SqlConnection的连接1 =新的SqlConnection(Properties.Settings.Default.BlackBookDBConnectionString);
connection1.Open();
字符串的SQLQuery =INSERT INTO [用户](用户名,密码,SecurityQuestionOne,
+SecurityAnswerOne,SecurityQuestionTwo,SecurityAnswerTwo)
+VALUES(@用户名,@密码,@ QuestionOne @ AnswerOne,@ QuestionTwo,@ AnswerTwo);

的SqlCommand命令=新的SqlCommand(sqlquery的,连接1);

字符串username = txtRegisterUsername.Text;
command.Parameters.Add(@用户名,SqlDbType.VarChar,200).value的=用户名;
字符串密码= txtRegisterRepeatPassword.Text;
command.Parameters.Add(@密码,SqlDbType.VarChar,200).value的=密码;
串questionOne = lstRegisterSecurityQuestionOne.SelectedText;
command.Parameters.Add(@ QuestionOne,SqlDbType.VarChar,200).value的= questionOne;
串questionTwo = lstRegisterSecurityQuestionTwo.SelectedText;
command.Parameters.Add(@ QuestionTwo,SqlDbType.VarChar,200).value的= questionTwo;
串answerOne = txtRegisterSecurityAnswerOne.SelectedText;
command.Parameters.Add(@ AnswerOne,SqlDbType.VarChar,200).value的= answerOne;
串answerTwo = txtRegisterSecurityAnswerTwo.SelectedText;
command.Parameters.Add(@ AnswerTwo,SqlDbType.VarChar,200).value的= answerTwo;

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


I get this error when trying to insert data into my database.

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Incorrect syntax near the keyword 'User'.

Here is the code:

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();
}

I have edited my code. However it still does not insert anything into my database for some reason.

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();
}

解决方案

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();
}

这篇关于我有插入我的数据库用C#麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆