c#如何插入文本框的值,并将其保存到SQL数据库?

问题描述:


如何插入文本框的值并将其保存到sql数据库? 我需要一些关于上述问题的帮助。当我点击按钮保存时,它应该将输入文本框更新为sql数据库Workers。你们可以制作一些编码样本来达到这个目的吗?因为我所做的一切都不工作。这是编码: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

你什么都不做。难怪,它不工作;-) – 2011-03-30 09:15:23

+4

...这是如何开始sql注入攻击。 – 2011-03-30 09:16:39

+0

@Daniel Hilgarth – 2011-03-30 09:16:57

你需要执行非查询

Source

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

    } 
+0

@Kenneth谢谢,但仍然有错误... – 2011-03-30 10:01:14

+0

按钮保存应该保存到数据库,永久更新它。任何建议如何做到这一点,因为使用插入不工作? – 2011-03-30 10:06:20

+0

你得到的错误是什么? – justinlabenne 2011-03-30 11:23:26

我已经正确连接到数据库的工人解决了这个问题。 YeaY!

下面是这个问题的正确代码:

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