为什么I一直小于0 表示弄一下午 都没成功 我是想插入一条数据 然后返回 i 若果i>0就注册成功 但是一直注册失败
int i = bll.Add(model, out msg);
if (i > 0)
{
//
Response.Redirect("/ShowMsg.aspx?msg=" + Server.UrlEncode(msg) + "&txt=" + Server.UrlEncode("首页") + "&url=/Default.aspx");
}
/// <summary>
/// 增加一条数据,部分类
/// </summary>
public int Add(BookShop.Model.Users model, out string msg)
{
//判断用户名是否已经被占用。
if (CheckUser(model.LoginId))//表示用户名已经被占用了
{
msg = "用户注册失败!!";
return -1;
}
msg = "注册成功";
return dal.Add(model);
}
/// <summary>
/// 根据用户名校验用户是否存在
/// </summary>
/// <param name="userName"></param>
/// <returns></returns>
public bool CheckUser(string userName)
{
Model.Users model = dal.GetModel(userName);
if (model != null)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 根据用户名得到一个对象实体
/// </summary>
public BookShop.Model.Users GetModel(string userName)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select top 1 Id,LoginId,LoginPwd,Name,Address,Phone,Mail,UserStateId from Users ");
strSql.Append(" where LoginId=@LoginId ");
SqlParameter[] parameters = {
new SqlParameter("@LoginId", SqlDbType.NVarChar,50)};
parameters[0].Value = userName;
BookShop.Model.Users model = new BookShop.Model.Users();
DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
if (ds.Tables[0].Rows.Count >0)
{
if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
{
model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
}
model.LoginId = ds.Tables[0].Rows[0]["LoginId"].ToString();
model.LoginPwd = ds.Tables[0].Rows[0]["LoginPwd"].ToString();
model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
model.Address = ds.Tables[0].Rows[0]["Address"].ToString();
model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
model.Mail = ds.Tables[0].Rows[0]["Mail"].ToString();
return model;
}
else
{
return null;
}
}
跟踪下 return dal.Add(model);的返回值~
0
注册失败 居然还可以插入数据
public int Add(BookShop.Model.Users model)
{
StringBuilder strSql=new StringBuilder();
strSql.Append("insert into Users(");
strSql.Append("LoginId,LoginPwd,Name,Address,Phone,Mail,UserStateId)");
strSql.Append(" values (");
strSql.Append("@LoginId,@LoginPwd,@Name,@Address,@Phone,@Mail,@UserStateId)");
strSql.Append(";select @@IDENTITY");
SqlParameter[] parameters = {
new SqlParameter("@LoginId", SqlDbType.NVarChar,50),
new SqlParameter("@LoginPwd", SqlDbType.NVarChar,50),
new SqlParameter("@Name", SqlDbType.NVarChar,50),
new SqlParameter("@Address", SqlDbType.NVarChar,200),
new SqlParameter("@Phone", SqlDbType.NVarChar,100),
new SqlParameter("@Mail", SqlDbType.NVarChar,100),
new SqlParameter("@UserStateId", SqlDbType.Int,4)};
parameters[0].Value = model.LoginId;
parameters[1].Value = model.LoginPwd;
parameters[2].Value = model.Name;
parameters[3].Value = model.Address;
parameters[4].Value = model.Phone;
parameters[5].Value = model.Mail;
parameters[6].Value = model.UserStateId;
object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
if (obj == null)
{
return 1;
}
else
{
return Convert.ToInt32(obj);
}
}
应该有一个自增的id吧?既然插入的数据,根据“;select @@IDENTITY”查到了自增的那个Id??试试“;SELECT SCOPE_IDENTITY()”呢,看看能不能返回一个id,插入成功了是肯定要返回一个自增的Id的,所以一楼的判断是有问题的if (obj == null),可以这样判断if(obj!=null && !string.IsNotNullOrEmpty(obj.ToString())){return int.Parse(obj.ToString())}else{return 0};
就int.Parse和Convert.ToInt32而言,int.Parse执行效率快,省去了Convert的判断环节