本文介绍了如何将值从一个表传递到数据库中的其他表。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings [ConnectionString]。ConnectionString);
conn.Open();
string insertQuery =insert into table1(ans,ans1,ans2,ans3,ans4)values(@ ans,@ ans1,@ ans2,@ ans3,@ ans4);
SqlCommand cmd = new SqlCommand(insertQuery,conn);
cmd.Parameters.AddWithValue(ans,RadioButtonList1.SelectedItem.Text);
cmd.Parameters.AddWithValue(ans1,RadioButtonList2.SelectedItem.Text);
cmd.Parameters.AddWithValue(ans2,RadioButtonList3.SelectedItem.Text);
cmd.Parameters.AddWithValue(ans3,RadioButtonList4.SelectedItem.Text);
cmd.Parameters.AddWithValue(ans4,RadioButtonList5.SelectedItem.Text);
cmd.ExecuteNonQuery();
Session [username] = insertQuery;
Session [empid] = insertQuery;
conn.Close();
}
catch(exception ex)
{
Response.Write(error+ ex.ToString());
}
我尝试过:
我尝试使用会话变量将值username和empid从登录表传递给table1,但该值未通过。请帮忙解决这个问题。
解决方案
try { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); conn.Open(); string insertQuery = "insert into table1(ans,ans1,ans2,ans3,ans4) values (@ans,@ans1,@ans2,@ans3,@ans4)"; SqlCommand cmd = new SqlCommand(insertQuery, conn); cmd.Parameters.AddWithValue("ans", RadioButtonList1.SelectedItem.Text); cmd.Parameters.AddWithValue("ans1", RadioButtonList2.SelectedItem.Text); cmd.Parameters.AddWithValue("ans2", RadioButtonList3.SelectedItem.Text); cmd.Parameters.AddWithValue("ans3", RadioButtonList4.SelectedItem.Text); cmd.Parameters.AddWithValue("ans4", RadioButtonList5.SelectedItem.Text); cmd.ExecuteNonQuery(); Session["username"] = insertQuery; Session["empid"] = insertQuery; conn.Close(); } catch (Exception ex) { Response.Write("error" + ex.ToString()); }
What I have tried:
I have tried using session variable to pass the value username and empid from login table to table1 but the value does not pass. Please help solving this.
解决方案
这篇关于如何将值从一个表传递到数据库中的其他表。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!