簡體   English   中英

選擇查詢到另一個表的結果

[英]Result of a Select query into another table

在Windows窗體應用程序中,我想將“選擇SQL查詢的結果”添加到另一個表中。

  • 輸入pid(textBox1.text)是整數。
  • 運行此應用程序時,我收到消息“已添加產品”,但實際上沒有。
  • 點擊按鈕后面的代碼,我認為SQL查詢在某處是錯誤的。 請幫忙

      private void button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection("Data Source=HOME;Initial Catalog=Test;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand("insert into list (pname, pprice) (select pname, pprice from products where pid='" +textBox1.Text+"')", con); MessageBox.Show("Product Added"); con.Close(); } 
  private void button1_Click(object sender, EventArgs e)
  {
  SqlDataAdapter SDA = new SqlDataAdapter();
  DataTable dt = new DataTable();
  SqlConnection con = new SqlConnection("Data Source=HOME;Initial   
                           Catalog=Test;Integrated Security=True");

  SqlCommand cmd = new SqlCommand("insert into list (pname, pprice)
  select pname, pprice from products where pid='" +textBox1.Text+"'", con);
  con.Open();
  SDA.SelectCommand = cmd;
  SDA.Fill(dt);
  con.Close();
  MessageBox.Show("Product Added");

  }

您在代碼中犯了以下錯誤

1:在創建sqlcommand對象之前打開連接

2:您沒有編寫代碼來正確執行查詢

3:還將括號放在select語句之前

注意:確保textBox1.Text不為null或為空,還應該使用參數化查詢

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM