2015-11-03 86 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 ;";