0
MemberChecker.CommandType = CommandType.StoredProcedure;

添加 并解决了问题!

---- 这是一个有趣的问题!

为什么我的 SqlCommand 访问存储过程仅在我使用时才有效

ParameterDirection.InputOutput

并不是

ParameterDirection.Output

此 SqlParameterCollection 不包含具有 ParameterName '@Customer' 的 SqlParameter

这是我的一些代码:

        using (SqlConnection H20_Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["H20"].ConnectionString))
        {
            SqlCommand MemberChecker = new SqlCommand("execute custom_x_Check", H20_Connection);
            MemberChecker.Parameters.Add("@TN", SqlDbType.VarChar,50).Value = TNH20.Text.Trim();
            MemberChecker.Parameters.Add("@Customer_ID", SqlDbType.VarChar, 50).Value = TNH20.Text.Trim();

            MemberChecker.Parameters.Add("@Customer", SqlDbType.VarChar, 100).Direction = ParameterDirection.Output;
            MemberChecker.Parameters.Add("@Out_TN", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
            MemberChecker.Parameters.Add("@Out_Customer_ID", SqlDbType.VarChar, 50).Direction = ParameterDirection.Output;
            H20_Connection.Open();
            if (MemberChecker.ExecuteNonQuery() > 1)
            {
                Top.Visible = false;
                Bottom.Visible = true;
            }
            else
            {
                Top.Visible = true;
                Bottom.Visible = false;
            }
        }

和我的 SQL

alter proc custom_Check

@TN varchar(50) ='',
@Customer_ID varchar(50)='',
@Customer varchar(100) out,
@Out_TN varchar(50) out,
@Out_Customer_ID varchar(50) out

as

select @Out_Customer_ID=Customer_ID,
@Out_TN = MainTN,@Customer = Customer
 from [vCustomerName]
where Customer_ID  = @Customer_ID or @TN = MainTN

谢谢!

4

2 回答 2

1

@Customer, @Out_TN, 并且@Out_Customer_ID都应该使用,ParameterDirection.Output因为它们是outSQL 中的参数。

像这样的东西:

MemberChecker.Parameters.Add("@Customer").Direction = ParameterDirection.Output;
MemberChecker.Parameters.Add("@Out_TN").Direction = ParameterDirection.Output;
MemberChecker.Parameters.Add("@Out_Customer_ID").Direction = ParameterDirection.Output;

另请注意,如果有多个记录符合您的条件,您的 SQL 将失败:

where Customer_ID  = @Customer_ID or @TN = MainTN

execute正如您敏锐地注意到的那样,您正在使用在这种情况下不正确的语句调用存储过程。将命令更改为:

SqlCommand MemberChecker = new SqlCommand("custom_Check", H20_Connection);
MemberChecker.CommandType = CommandType.StoredProcedure;
于 2013-02-04T20:30:41.020 回答
0
MemberChecker.CommandType = CommandType.StoredProcedure
于 2013-04-15T18:36:24.657 回答