异常详细信息: System.Data.SqlClient.SqlException: 必须声明标量变量 "@sxh".

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;
public partial class insert : System.Web.UI.Page
{
string sql = WebConfigurationManager.AppSettings ["myconn"];
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btntj_Click(object sender, EventArgs e)
{
string sxh = this.tbx_ID.Text;
string sxm = this.tbx_xm.Text;
string sxb = RadioButtonList1.SelectedItem.Text;
string szy = DropDownList1.SelectedItem.Text;
string scsrq = this.tbx_csrq.Text;
string sdk; if (CheckBox1.Checked)
{
sdk = "是";
}
else sdk = "否";
string ssfz = this.tbx_sfz.Text;
string sjg = this.tbx_jg.Text;
string sjtdz = this.tbx_jtdz.Text;
SqlConnection conn = new SqlConnection(sql);
string da = "insert into xsdate(ID,xm,xb,zy,csrq,dk,sfz,jg,jtdz) values(@sxh,@sxm,@sxb,@szy,@scsrq,@sdk,@ssfz,@sjg,@sjtdz)";
SqlCommand cmd = new SqlCommand (da ,conn );
conn.Open();
if (cmd.ExecuteNonQuery() > 0)
{
Response.Write("添加成功");
}
cmd.Prepare();
conn .Close ();

当然会报错了,没有传入SqlParameter。

SqlParameter[] sqlParams = new SqlParameter{
    new SqlParameter("@sxh", sxh),
    new SqlParameter("@sxm", sxm),
    ……
 };

然后传入,如果不知道  SqlParameter怎么用,请百度! :)

温馨提示:答案为网友推荐,仅供参考
相似回答