带输出参数的存储过程
调用带输出参数的存储过程,在程序中除了声明该参数的值类型外,还必须指明返回值的大小,否则会出现错误.
正确例子:public void getpwd()
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.CommandText = "Stest";
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
SqlParameter[] prams ={ new SqlParameter("@username", SqlDbType.NChar), new SqlParameter("@userpwd", SqlDbType.NChar,20) };//@userpwd 是output 大小 20,在次必须指明
prams[0].Value = this._username;
prams[1].Direction = ParameterDirection.Output ;
comm.Parameters.AddRange(prams);
try
{
comm.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
this._UserPwd = comm.Parameters[1].Value.ToString ();
conn.Close();
}
}
正确例子:public void getpwd()
{
conn.Open();
SqlCommand comm = new SqlCommand();
comm.CommandText = "Stest";
comm.CommandType = CommandType.StoredProcedure;
comm.Connection = conn;
SqlParameter[] prams ={ new SqlParameter("@username", SqlDbType.NChar), new SqlParameter("@userpwd", SqlDbType.NChar,20) };//@userpwd 是output 大小 20,在次必须指明
prams[0].Value = this._username;
prams[1].Direction = ParameterDirection.Output ;
comm.Parameters.AddRange(prams);
try
{
comm.ExecuteNonQuery();
}
catch (Exception e)
{
throw e;
}
finally
{
this._UserPwd = comm.Parameters[1].Value.ToString ();
conn.Close();
}
}

浙公网安备 33010602011771号