如何通过单击按钮[C#]执行2个SQL查询

时间:2017-10-20 08:03:50

标签: c# sql database visual-studio-2013

我想复制表中的特定行,并复制将其复制到新表(name- cart )中。并在 dataGridView 中显示 cart 表中的所有记录。帮助!

con = new System.Data.SqlClient.SqlConnection();

        con.ConnectionString = " Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\posdb.mdf;Integrated Security=True";



        string query = "INSERT INTO cart (item_id,Description,Unit_Price,Qty) SELECT item_id,Description,price,'" + txtqty.Text + "' FROM items WHERE item_id = '" + textBox1.Text + "'";
        string qry = "SELECT * FROM cart";

        System.Data.SqlClient.SqlDataAdapter da;
        System.Data.SqlClient.SqlDataAdapter da1;
        da = new System.Data.SqlClient.SqlDataAdapter(query, con);
        da1 = new System.Data.SqlClient.SqlDataAdapter(qry, con);

        ds1 = new DataSet();
        con.Open();
        da1.Fill(ds1,"cart");
        con.Close();
        dataGridView1.DataSource = ds1;
        dataGridView1.DataMember = "cart";

帮助! ......

1 个答案:

答案 0 :(得分:2)

protected void btn_Click(object sender, EventArgs e)
    {
     insert();
     View();
     }
    private void insert()
    {
      // write insert query 
    }
   private void View()
    {
     // write select query and bind to grid
    }