ASP.NET三层架构中报错System.Data.SqlClient.SqlException: 必须声明标量变量 "@userid"
业务逻辑层中插入代码如下:publicintInsertUser(stringuserid,stringusername,stringpassword,stringsex...
业务逻辑层中插入代码如下:
public int InsertUser(string userid, string username, string password, string sex, string birth, string city, string schoolname)
{
string sqlText = "insert into Register values(@userid,@username,@password,@sex,@birth,@city,@schoolname)";
string[] paraNames = { "@userid", "@username", "@password", "@sex", "@birth", "@city", "@schoolname" };
object[] paraValues = { userid, username, password, sex, birth, city, schoolname };
int i = Convert.ToInt32(DA.ExecuteSql(sqlText,CommandType.Text , paraNames, paraValues));
return i;
}
数据访问层代码:
static SqlConnection conn;
static SqlCommand cmd;
static SqlDataAdapter sda;
static DataSet ds;
static DA()
{
conn = new SqlConnection(@"server=.\sqlexpress;database=scofield;integrated security=sspi");
cmd = new SqlCommand();
cmd.Connection = conn;
}
public static int ExecuteSql(string sqlText, CommandType commandType, string[] paraNames, object[] paravalues)//数据库语句,语句类型,@sno等,@sno等的值
{
int count;
if (conn.State != ConnectionState.Open)
{
conn.Open();
cmd.Parameters.Clear();
}
cmd.CommandText = sqlText;
cmd.CommandType = commandType;
if (paraNames != null)
{
int d = paraNames.Length;
for (int i = 0; i < d; i++)
{
cmd.Parameters.AddWithValue(paraNames[i], paravalues[i]);
}
}
count = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
return count; }
界面层:
protected void Button1_Click(object sender, EventArgs e)
{
RegisterBusiness rb = new RegisterBusiness();
Register rr = new Register();
rr.Userid = Txt_userid.Text.Trim();
rr.Username = Txtusername.Text.Trim();
rr.Password = Txtpwd.Text.Trim();
string confirm = Txtconfirm.Text.Trim();
rr.Sex = RadioButtonList1.SelectedValue;
rr.Birthday = DropDownList1.SelectedValue + "年" + DropDownList2.SelectedValue + "月" + DropDownList3.SelectedValue + "日";
rr.City = Txtcity.Text.Trim();
rr.Schoolname = Txtschoolname.Text.Trim();
if (confirm == rr.Password) 展开
public int InsertUser(string userid, string username, string password, string sex, string birth, string city, string schoolname)
{
string sqlText = "insert into Register values(@userid,@username,@password,@sex,@birth,@city,@schoolname)";
string[] paraNames = { "@userid", "@username", "@password", "@sex", "@birth", "@city", "@schoolname" };
object[] paraValues = { userid, username, password, sex, birth, city, schoolname };
int i = Convert.ToInt32(DA.ExecuteSql(sqlText,CommandType.Text , paraNames, paraValues));
return i;
}
数据访问层代码:
static SqlConnection conn;
static SqlCommand cmd;
static SqlDataAdapter sda;
static DataSet ds;
static DA()
{
conn = new SqlConnection(@"server=.\sqlexpress;database=scofield;integrated security=sspi");
cmd = new SqlCommand();
cmd.Connection = conn;
}
public static int ExecuteSql(string sqlText, CommandType commandType, string[] paraNames, object[] paravalues)//数据库语句,语句类型,@sno等,@sno等的值
{
int count;
if (conn.State != ConnectionState.Open)
{
conn.Open();
cmd.Parameters.Clear();
}
cmd.CommandText = sqlText;
cmd.CommandType = commandType;
if (paraNames != null)
{
int d = paraNames.Length;
for (int i = 0; i < d; i++)
{
cmd.Parameters.AddWithValue(paraNames[i], paravalues[i]);
}
}
count = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
return count; }
界面层:
protected void Button1_Click(object sender, EventArgs e)
{
RegisterBusiness rb = new RegisterBusiness();
Register rr = new Register();
rr.Userid = Txt_userid.Text.Trim();
rr.Username = Txtusername.Text.Trim();
rr.Password = Txtpwd.Text.Trim();
string confirm = Txtconfirm.Text.Trim();
rr.Sex = RadioButtonList1.SelectedValue;
rr.Birthday = DropDownList1.SelectedValue + "年" + DropDownList2.SelectedValue + "月" + DropDownList3.SelectedValue + "日";
rr.City = Txtcity.Text.Trim();
rr.Schoolname = Txtschoolname.Text.Trim();
if (confirm == rr.Password) 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询