存储过程
存储过程:
ALTER proc User_Login_1
@UserID nchar(10),
@Password char(20),
@Return int=0 output
as
declare @flag int
select @flag=count(*) from [table]
where @UserID=name and @Password=password
if @flag>0
begin
select @Return=1
end
连接数据库:
SqlConnection con = dbobj.GetConnection();
SqlCommand com = new SqlCommand();
com.Connection = con;
con.Open();
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "User_Login_1";
com.Parameters.Add("@UserID", SqlDbType.NChar, 10).Value = user_text.Text.Trim();
com.Parameters.Add("@Password", SqlDbType.Char, 20).Value = password.Text.Trim();
com.Parameters.Add("@Return", SqlDbType.Int).Direction =ParameterDirection.Output;
com.ExecuteNonQuery();
int i =Convert.ToInt32(com.Parameters["@Return"].Value);
if (i>0)
{
Response.Write("<script language=javascript>alert('登陆成功');</script>");
}
else
{
Response.Write("<script language=javascript>alert('登陆不成功');</script>");
}
con.Close();
这里int i =Convert.ToInt32(com.Parameters["@Return"].Value); 为什么会出现对象不能从 DBNull 转换为其他类型的错误?
[解决办法]
空字符串是不能进行类型转换的!!
先给@Return赋默认值,保证@flag=0时也有返回值!!