我有一个代码将数据从asp.net(c#代码)移动到sql。在下面的代码中,我直接插入了值(即)xxx和yyy。这也有效。现在我想插入在文本框中输入的值。这样做的代码是什么?帮帮我朋友。
protected void Button2_Click(object sender, EventArgs e)
{
string comm="insert into login values('xxx','yyy')";
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString();
cmd = new SqlCommand(comm, con);
cmd.Connection.Open();
con.Open();
int i = cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
答案 0 :(得分:2)
protected void Button2_Click(object sender, EventArgs e)
{
string comm="insert into login values(@val1,@val2)";
con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString.ToString();
cmd = new SqlCommand(comm, con);
cmd.Connection.Open();
cmd.Parameters.Add("@val1",TextBox1.Text); //Replace TextBox1.Text with your first textbox
cmd.Parameters.Add("@val2",TextBox2.Text); //Replace TextBox2.Text with your second textbox
con.Open();
int i = cmd.ExecuteNonQuery();
cmd.Connection.Close();
}