Select 查询到另一个 table 的结果
Result of a Select query into another table
在 Windows 表单应用程序中,我想将 Select SQL 查询的结果添加到另一个 table。
- 输入的pid(textBox1.text)是一个整数编号。
- 当 运行 这个应用程序时,我收到消息 "Product Added" 但实际上不是。
这是单击按钮后的代码,我认为 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 : 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 或为空,您还应该使用参数化查询
在 Windows 表单应用程序中,我想将 Select SQL 查询的结果添加到另一个 table。
- 输入的pid(textBox1.text)是一个整数编号。
- 当 运行 这个应用程序时,我收到消息 "Product Added" 但实际上不是。
这是单击按钮后的代码,我认为 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 : 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 或为空,您还应该使用参数化查询