求助,三层架构插入数据的时候老是报异常..(已解决)
具体代码:public class SqlHelper
{
readonly static string ConnStr = ConfigurationManager.ConnectionStrings["SqlCon"].ConnectionString;
public static SqlCommand PrePareCommand(string sqlstr,SqlParameter[] para)
{
SqlConnection con = new SqlConnection(ConnStr);
SqlCommand cmd = new SqlCommand(sqlstr,con);
if(para!=null)
{
cmd.Parameters.AddRange(para);
}
return cmd;
}
public static int ExcuteNonQuery(string sqlstr,SqlParameter[] para)
{
SqlCommand cmd = PrePareCommand(sqlstr, para);
try
{
cmd.Connection.Open();
return cmd.ExecuteNonQuery();
}
finally
{
cmd.Connection.Close();
}
}
public static SqlDataReader ExcuteReader(string sqlstr, SqlParameter[] para)
{
SqlCommand cmd = PrePareCommand(sqlstr,para);
cmd.Connection.Open();
SqlDataReader sdr = cmd.ExecuteReader( CommandBehavior.CloseConnection);
return sdr;
}
}
DAL层中用下面这个方法插数据
public bool Insert(string name,string pwd,string sex,string email,string address)
{
string sqlStr = "insert into UserInfo (UName,UPwd,USex,UEmail,UAddress) values (@name,@pwd,@sex,@email,@address)";
SqlParameter [] paras = new SqlParameter[]{
new SqlParameter("@uname",name),
new SqlParameter("@upwd",pwd),
new SqlParameter("@usex",sex),
new SqlParameter("@uemail",email),
new SqlParameter("@uaddress",address)
};
if (SqlHelper.ExcuteNonQuery(sqlStr,paras) > 0)
{
return true;
}
else
{
return false;
}
}
BLL层
public bool InsertUser(string name, string pwd, string sex, string email, string address)
{
return us.Insert(name,pwd, sex,email,address);
}
页面中
protected void btnSubmit_Click(object sender, EventArgs e)
{
string name = this.txtName.Text;
string pwd = this.txtPwd.Text;
string sex = string.Empty;
string email = this.txtEmail.Text;
string address = this.txtAddress.Text;
if (this.radM.Checked)
{
sex = "男";
}
else
{
sex = "女";
}
bool flag = um.InsertUser(name, pwd, sex, email, address);
if (flag)
{
}
}
异常的地方是SqlHelper类中 return cmd.ExecuteNonQuery(); 说"="号附近有语法错误
try
{
cmd.Connection.Open();
return cmd.ExecuteNonQuery();
}
finally
{
cmd.Connection.Close();
}
弄了很久不知道哪里写错了
请大家看一看
谢谢
[[it] 本帖最后由 jevonsjin 于 2008-9-14 03:22 编辑 [/it]]