我有以下方法从C#app调用SP:
public bool CheckEmail(string email)
{
bool success = false;
string name;
using (SqlConnection con = new SqlConnection(Internal_Audit_Capture.Properties.Settings.Default.Intenal_AuditDB))
{
con.Open();
try
{
using (SqlCommand command = new SqlCommand("IntAudit.p_checkEmail", con))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@email", maskedTextBox1.Text.Trim()));
command.Parameters.Add("@success", SqlDbType.Bit).Direction = ParameterDirection.Output;
command.Parameters.Add("@name", SqlDbType.VarChar).Direction = ParameterDirection.Output;
command.ExecuteNonQuery();
success = (bool)command.Parameters["@success"].Value;
name = (string)command.Parameters["@name"].Value;
}
return success;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return success;
}
}
}
p_checkEmail查询下表:
[IntAudit].[LOGINDETAILS](
[ID] [int] IDENTITY(1,1) NOT NULL,
[NAME] [varchar](30) NOT NULL,
[PASSWORD] [nvarchar](30) NOT NULL,
[ADMIN] [bit] NULL,
[EMAIL] [varchar](50) NOT NULL,PRIMARY KEY CLUSTERED ([ID] ASC))
它的代码是:
create procedure IntAudit.p_checkEmail
@email varchar(50),
@success bit OUTPUT,
@name varchar(30) OUTPUT
as
set nocount on;
if(exists(select 1 from [IntAudit].[LOGINDETAILS] where [EMAIL] = @email))
begin
select @name = [NAME] from [IntAudit].[LOGINDETAILS] where [EMAIL] = @email
set @success = 1
return
end
else
begin
set @name = null
set @success = 0
return
end
go
运行checkEmail方法会产生System.InvalidOperationException:String [2]:size属性的大小为0,无效。在SSMS中执行该过程运行正常。如果用户输入的地址存在,则该方法不是检查表中的电子邮件地址并返回用户名。我试过更改数据类型,但我仍然得到相同的错误。有什么我想念的吗?
编辑:例外细节:
System.InvalidOperationException:String [2]:Size属性的大小无效。
at System.Data.SqlClient.SqlParameter.Validate(Int32 index,Boolean isCommandProc)
at System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc,Int32 startCount,Boolean inSchema,SqlParameterCollection参数)
at System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema,SqlParameterCollection parameters,_SqlRPC& rpc)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 timeout,Task& task,Boolean asyncWrite,SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String method,TaskCompletionSource`1 completion,Int32 timeout,Task& task,Boolean asyncWrite)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion,String methodName,Boolean sendToPipe,Int32 timeout,Boolean asyncWrite)
在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
在C:\ Internal Audit \ Internal Audit Capture \ Internal Audit Capture \ ForgotPassword.cs:line 41
中的Internal_Audit_Capture.ForgotPassword.CheckEmail(String email)答案 0 :(得分:5)
您需要指定@name参数的大小。
command.Parameters.Add("@name", SqlDbType.VarChar, 30).Direction = ParameterDirection.Output;
答案 1 :(得分:0)
试试这个
command.Parameters.Add("@email", SqlDbType.VarChar).Value = maskedTextBox1.Text.Trim();
而不是
command.Parameters.Add(new SqlParameter("@email", maskedTextBox1.Text.Trim()));