18

テーブルに値を挿入する簡単なプログラムを作成しました[regist]が、エラーが発生し続けます

')' 付近の構文が正しくありません

オンcmd.ExecuteNonQuery();:

 private void button1_Click(object sender, EventArgs e)
 {
      SqlConnection cn = new SqlConnection("Data Source=DELL-PC;initial catalog=AdventureWorks2008R2 ; User ID=sa;Password=sqlpass;Integrated Security=SSPI;");

      SqlCommand cmd = new SqlCommand("INSERT INTO dbo.regist (" + " FirstName, Lastname, Username, Password, Age, Gender,Contact, " + ") VALUES (" + " @textBox1.Text, @textBox2.Text, @textBox3.Text, @textBox4.Text, @comboBox1.Text,@comboBox2.Text,@textBox7.Text" + ")", cn);

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

私はこれが初めてで、本当に混乱しています。

4

5 に答える 5

52

コメントで述べたように、クエリでは常にパラメーターを使用する必要があります。SQLステートメントを自分で連結しないでください。

また、クリック イベント ハンドラを実際のコードから分離して、データを挿入することをお勧めします。

だから私はあなたのコードを次のように書き直します

Web ページのコード ビハインド ファイル ( yourpage.aspx.cs)

private void button1_Click(object sender, EventArgs e)
{
      string connectionString = "Data Source=DELL-PC;initial catalog=AdventureWorks2008R2 ; User ID=sa;Password=sqlpass;Integrated Security=SSPI;";

      InsertData(connectionString,
                 textBox1.Text.Trim(),  -- first name
                 textBox2.Text.Trim(),  -- last name
                 textBox3.Text.Trim(),  -- user name
                 textBox4.Text.Trim(),  -- password
                 Convert.ToInt32(comboBox1.Text),  -- age
                 comboBox2.Text.Trim(), -- gender
                 textBox7.Text.Trim() );  -- contact
}

他のコード (例: a databaselayer.cs):

private void InsertData(string connectionString, string firstName, string lastname, string username, string password
                        int Age, string gender, string contact)
{
    // define INSERT query with parameters
    string query = "INSERT INTO dbo.regist (FirstName, Lastname, Username, Password, Age, Gender,Contact) " + 
                   "VALUES (@FirstName, @Lastname, @Username, @Password, @Age, @Gender, @Contact) ";

    // create connection and command
    using(SqlConnection cn = new SqlConnection(connectionString))
    using(SqlCommand cmd = new SqlCommand(query, cn))
    {
        // define parameters and their values
        cmd.Parameters.Add("@FirstName", SqlDbType.VarChar, 50).Value = firstName;
        cmd.Parameters.Add("@Lastname", SqlDbType.VarChar, 50).Value = lastName;
        cmd.Parameters.Add("@Username", SqlDbType.VarChar, 50).Value = userName;
        cmd.Parameters.Add("@Password", SqlDbType.VarChar, 50).Value = password;
        cmd.Parameters.Add("@Age", SqlDbType.Int).Value = age;
        cmd.Parameters.Add("@Gender", SqlDbType.VarChar, 50).Value = gender;
        cmd.Parameters.Add("@Contact", SqlDbType.VarChar, 50).Value = contact;

        // open connection, execute INSERT, close connection
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }
}

次のようなコード:

  • SQL インジェクション攻撃に対して脆弱ではない
  • SQL Server でのパフォーマンスが大幅に向上します (クエリが実行プランに一度解析され、キャッシュされて後で再利用されるため)
  • イベント ハンドラー (コード ビハインド ファイル) を実際のデータベース コードから分離します (それらが属する場所に配置する - 大量のスパゲッティ コードによる「太りすぎ」のコード ビハインドを回避するのに役立ち、UI イベントの処理からデータベース アクセスまですべてを実行します -良くありません)デザイン!)
于 2012-11-26T21:55:41.047 に答える
7

カンマを取り除く

... Gender,Contact, " + ") VALUES ...
                  ^-----------------here
于 2012-11-26T21:24:46.520 に答える
-2

最後のコンマを削除する必要があります.nrodicが言ったように、コマンドは正しくありません.

次のように変更する必要があります。

SqlCommand cmd = new SqlCommand("INSERT INTO dbo.regist (" + " FirstName, Lastname, Username, Password, Age, Gender,Contact " + ") VALUES (" + " textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, comboBox1.Text,comboBox2.Text,textBox7.Text" + ")", cn);
于 2012-11-26T21:52:06.180 に答える