我在做忘记密码,我写下面的代码.但是我得到了错误.那就是size属性的大小为0.
问题描述:
public string forgotpassword(string tutor_username, string tutor_email)
{
SqlConnection con = new SqlConnection(StrCon);
SqlCommand cmd = new SqlCommand();
con.ConnectionString = StrCon;
//DataSet resultds = new DataSet();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
//SqlDataAdapter da = new SqlDataAdapter();
cmd.CommandText = "tutor_Retrievepassword";
cmd.Parameters.Add("@tutor_username", SqlDbType.NVarChar).Direction = ParameterDirection.Input;
cmd.Parameters.Add("@tutor_password", SqlDbType.NVarChar).Direction = ParameterDirection.Output;
cmd.Parameters.Add("@tutor_email", SqlDbType.NVarChar).Direction = ParameterDirection.Input;
cmd.Parameters["@tutor_username"].Value = tutor_username;
//cmd.Parameters["@password"].Value = password;
cmd.Parameters["@tutor_email"].Value = tutor_email;
// da.SelectCommand = cmd;
try
{
con.Open();
cmd.ExecuteReader();
return cmd.Parameters["@tutor_password"].ToString();
// da.Fill(resultds);
}
catch (Exception ex)
{
throw ex;
// return null;
}
finally
{
cmd.Dispose();
con.Close();
con.Dispose();
}
//return resultds;
}
答
可能应该在存储过程中指定Size参数.
您可以提供tutor_Retrievepassword过程的SQL吗?
Possibly you should specify Size argument in yours stored procedure.
Can you provide SQL of tutor_Retrievepassword procedure?
调试代码以查看哪一行会引发错误.
如果执行查询(cmd.ExecuteReader()
)
尝试在SQL Server查询分析器上使用相同的参数运行存储过程,并检查存储过程是否成功.
Debug the code to see which line throws the error.
If it''s on execution of query (cmd.ExecuteReader()
)
try running the stored procedure with same parameters on SQL Server Query Analyzer and check if it succeeds there.
在上面的代码中,您正在使用nvarchar.默认情况下,此数据类型包含1个字符(第一个字符).在这里,如果您在Sql参数中提到了大小,那么您提到的错误将不会出现.您可以尝试查看结果. :)
In the above code you are using nvarchar. This data type by default hold 1 characters(the first one). Here if you mention the size in Sql Parameter, then the error you mentioned will not come. You may try and see the result. :)