Select 查询到另一个 table 的结果

Result of a Select query into another table

在 Windows 表单应用程序中,我想将 Select SQL 查询的结果添加到另一个 table。

  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 : you open connection before creating of sqlcommand object

2: you did not write code to execute the query properly

3: you also put parenthesis before the select statement

注: 确保 textBox1.Text 不为 null 或为空,您还应该使用参数化查询