简体   繁体   English

在SSMS中正常运行时,为什么在c#中调用存储过程会给出InvalidOperationException?

[英]Why does calling Stored Procedure in c# give InvalidOperationException when runs fine in SSMS?

I have the following method calling an SP from C# app: 我有以下方法从C#应用程序调用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 queries the following table: 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))

And it's code is: 它的代码是:

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

Running the checkEmail method gives a System.InvalidOperationException: String[2]: size property has invalid size of 0 error. 运行checkEmail方法会给出System.InvalidOperationException:String [2]:size属性具有无效的大小0错误。 Executing the procedure in SSMS runs fine though. 不过,在SSMS中执行该程序运行正常。 The method is mean't to check for an email address in the table and return the username, if the address entered by the user exists. 该方法不是要检查表中的电子邮件地址并返回用户名(如果用户输入的地址存在)。 I have tried changing the data types but I still get the same error. 我尝试更改数据类型,但仍然遇到相同的错误。 Is there anything I am missing? 我有什么想念的吗?

EDIT: Exception Details: 编辑:异常详细信息:

System.InvalidOperationException: String[2]: the Size property has an invalid size of 0. System.InvalidOperationException:字符串[2]:Size属性的大小无效为0。

at System.Data.SqlClient.SqlParameter.Validate(Int32 index, Boolean isCommandProc) 在System.Data.SqlClient.SqlParameter.Validate(Int32索引,布尔isCommandProc)

at System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc, Int32 startCount, Boolean inSchema, SqlParameterCollection parameters) 在System.Data.SqlClient.SqlCommand.SetUpRPCParameters(_SqlRPC rpc,Int32 startCount,布尔inSchema,SqlParameterCollection参数)

at System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema, SqlParameterCollection parameters, _SqlRPC& rpc) 在System.Data.SqlClient.SqlCommand.BuildRPC(Boolean inSchema,SqlParameterCollection参数,_SqlRPC和rpc)

at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) 在System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean异步,Int32超时,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) 在System.Data.SqlClient.SqlCommand.RunExecuteReader处(CommandBehavior cmdBehavior,RunBehavior runBehavior,布尔值returnStream,字符串方法,TaskCompletionSource`1完成,Int32超时,任务和任务,布尔asyncWrite)

at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) 在System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1完成,String methodName,布尔sendToPipe,Int32超时,布尔asyncWrite)

at System.Data.SqlClient.SqlCommand.ExecuteNonQuery() 在System.Data.SqlClient.SqlCommand.ExecuteNonQuery()

at Internal_Audit_Capture.ForgotPassword.CheckEmail(String email) in C:\\Internal Audit\\Internal Audit Capture\\Internal Audit Capture\\ForgotPassword.cs:line 41 在C:\\ Internal Audit \\ Internal Audit Capture \\ Internal Audit Capture \\ ForgotPassword.cs:line 41中的Internal_Audit_Capture.ForgotPassword.CheckEmail(String email)

您需要指定@name参数的大小。

command.Parameters.Add("@name", SqlDbType.VarChar, 30).Direction = ParameterDirection.Output;

Try this 尝试这个

command.Parameters.Add("@email", SqlDbType.VarChar).Value = maskedTextBox1.Text.Trim();

instead of 代替

command.Parameters.Add(new SqlParameter("@email", maskedTextBox1.Text.Trim()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM