2011-03-30 16 views
0


テキストボックスの値を挿入してSQLデータベースに保存するにはどうすればよいですか? 上記の質問に関して私はここでいくつかの助けが必要です。保存ボタンをクリックすると、入力テキストボックスがsqlデータベースワーカーに更新されます。あなたはこれを達成するためにいくつかのコーディングサンプルを作ってもらえますか?私がしていることはまったく働いていないからです。これはコーディングです:C#テキストボックスの値を挿入してSQLデータベースに保存するには?

private void btnSave_Click(object sender, EventArgs e) { 
#region SaveButton 
      // System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(); 

      //System.Data.SqlClient.SqlCommandBuilder cb; 
      //cb = new System.Data.SqlClient.SqlCommandBuilder (da); 

      //add to Dataset a new row 
      DataRow dRow = ds1.Tables["Workers"].NewRow(); 

      //add data to the new row just have been created 
      //refer to first_Name 
      dRow[1] = textBox1.Text; 
      dRow[2] = textBox2.Text; 
      dRow[3] = textBox3.Text; 

      //add command 
      //add to table worker a new row that declared by row variable name dRow 
      ds1.Tables["Workers"].Rows.Add(dRow); 

      MaxRows = MaxRows + 1; //to enable last row is still last row 
      inc = MaxRows - 1; 

      //call data adapter da to update and save data into database sql server 
      //da.Update(ds1, "Workers"); 

      MessageBox.Show("Entry Added!"); 
#endregion 
      con.ConnectionString = "Data Source=.\\SQLEXPRESS; AttachDbFilename =D:\\MyWorkers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"; 


      string strSQL = "INSERT INTO Workers (first_Name, last_Name, job_Title)" + " VALUES ('" + textBox1.Text + "', '" + textBox2.Text + "', " + " '" + textBox3.Text + "') "; 





      con.Close(); 
     } 
+1

あなたは何もしないされています。それはうまくいきません;-) –

+4

...これがSQLインジェクション攻撃の仕組みです。 –

+0

@Daniel Hilgarth –

答えて

0

あなたは、私が労働者データベースに正しく接続することによって、この問題を解決した非クエリー

Source

using (SqlConnection connection = new SqlConnection(
       connectionString)) 
    { 
     SqlCommand command = new SqlCommand(queryString, connection); 
     command.Connection.Open(); 
     command.ExecuteNonQuery(); 

    } 
+0

@Kennethありがとうございますが、まだエラーがあります... –

+0

ボタンを保存するにはデータベースに保存し、永久に更新する必要があります。挿入を使用して以来、これを行う方法に関する提案は機能していませんか? –

+0

取得しているエラーは何ですか? – justinlabenne

1

を実行する必要があります。そうだね!ここで

は、この質問のための右のコードです:

private void btnSave_Click(object sender, EventArgs e) 
{ 
    #region SaveButton 
    System.Data.SqlClient.SqlDataAdapter da; 
    string sql = "SELECT * From tblWorkers"; 
    da = new System.Data.SqlClient.SqlDataAdapter(sql, con); 

    System.Data.SqlClient.SqlCommandBuilder cb; 
    cb = new System.Data.SqlClient.SqlCommandBuilder (da); 

    //add to Dataset a new row 
    DataRow dRow = ds1.Tables["Workers"].NewRow(); 

    //add data to the new row that has just been created 
    //refer to first_Name 
    dRow[1] = textBox1.Text; 
    dRow[2] = textBox2.Text; 
    dRow[3] = textBox3.Text; 

    //add command 
    //add to table worker a new row that declared by row variable name dRow 
    ds1.Tables["Workers"].Rows.Add(dRow); 

    MaxRows = MaxRows + 1; //to enable last row is still last row 
    inc = MaxRows - 1; 

    //call data adapter da to update and save data into database sql server 
    da.Update(ds1, "Workers");    

    MessageBox.Show("Entry Added!"); 
    con.Close(); 
    #endregion 
関連する問題