2015-11-03 88 views
1

我在SQL中有两个表。我需要从一个表插入数据到另一个。我已经成功地完成了,但问题是我需要从文本框额外添加一个列值。我将显示我的代码从一个表插入表值到另一个

protected void Button2_Click1(object sender, EventArgs e) 
{ 
    String str1 = "insert into table2(Code,Qty,UPrice,Total) select Code,Qty,UPrice,Total from table1 ;";  
    SqlCommand cmd = new SqlCommand(str1, con); 
    con.Open(); 
    SqlDataAdapter da1 = new SqlDataAdapter(); 
    cmd.Parameters.AddWithValue("@Num", TextBox1.Text); 
    da1.SelectCommand = cmd; 
    DataSet ds1 = new DataSet(); 
    da1.Fill(ds1, "Code"); 
    GridView1.DataSource = ds1; 
    con.Close(); 
} 

这里我需要添加Num到我的table2不在table1中。所以我需要从textbox1中添加它。如何将该Num值添加到文本框中的所有数据。

回答

1
protected void Button2_Click1(object sender, EventArgs e) 
{ 
String str1 = "insert into table2(Code,Qty,UPrice,Total,Num) select Code,Qty,UPrice,Total,'"+TextBox1.Text+"' from table1 ;";  
SqlCommand cmd = new SqlCommand(str1, con); 
con.Open(); 
SqlDataAdapter da1 = new SqlDataAdapter(); 
da1.SelectCommand = cmd; 
DataSet ds1 = new DataSet(); 
da1.Fill(ds1); 
GridView1.DataSource = ds1; 
con.Close(); 
} 
+0

谢谢Kaushik for ur help –

+0

欢迎好友:) –

1

我猜Num是你的列名,您可以修改您的查询作为

insert into table2(Code,Qty,UPrice,Total, Num) 
select Code,Qty,UPrice,Total, textbox1.Text from table1 ; 
1

只需设置一个临时变量的动态值。

String temp = TextBox1.Text; 

Now use this variable with your insert query 

String str1 = "insert into table2(Code,Qty,UPrice,Total,Num) 
select Code,Qty,UPrice,Total,'"+temp+"' from table1 ;";