【问题标题】:SQL StoredProc Inexplicably Not Seeing Param [closed]SQL StoredProc 莫名其妙地看不到参数 [关闭]
【发布时间】:2018-02-23 17:30:19
【问题描述】:

我从来没有在存储过程方面遇到过这么大的麻烦。我使用 C#/ADO.NET 如下:

SqlConnection gconn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand("AddCustomerComments", gconn);

SqlParameter param = new SqlParameter("@surveyId", 
SqlDbType.UniqueIdentifier, 250);
param.Value = SurveyId;
cmd.Parameters.Add(param);

从头到尾遍历所有值,一切都加起来并成立。我得到的错误是没有提供@surveyId。但它就在那里。

这是存储过程:

ALTER proc [dbo].[AddCustomerComments]
    -- Add the parameters for the stored procedure here
    @surveyId uniqueidentifier, 
    @comment varchar(250)
AS

-- Insert statements for procedure here
insert into SurveyAddendums (SurveyId, CustomerComment, RCFollowUp, RCViewed) 
values (@surveyId, @comment, 0, 0);

唯一可能影响这一点的其他因素是SurveyId 是一个Guid。

为什么会出现这个错误?

【问题讨论】:

  • 你需要设置cmd.CommandType = CommandType.StoredProcedure
  • 是的,在我发布问题后才看到它。呜呜
  • 错误信息不是很清楚...
  • @Matt SQL 和 Web 开发是编程中最“令人欣慰”的两个领域……在大学里,我下定决心要像瘟疫一样避开这两个领域。 ..但后来,我被录用了。 :-)

标签: c# sql-server stored-procedures ado.net


【解决方案1】:

产生误导性错误的原因是因为您错误地将UniqueIdentifier字段的长度设置为250,所以它不匹配。

此外,由于您没有默认值(使其成为必需值),因此您还需要传递 @comment 参数。

SqlConnection gconn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand("AddCustomerComments", gconn);
cmd.CommandType = CommandType.StoredProcedure

SqlParameter surveyIdParam = new SqlParameter("@surveyId", System.Data.SqlDbType.UniqueIdentifier);
SqlParameter commentParam = new SqlParameter("@comment ", System.Data.SqlDbType.VarChar, 250);

surveyIdParam.Value = SurveyId;
commentParam.Value = "This is the comment";

cmd.Parameters.Add(surveyIdParam);
cmd.Parameters.Add(commentParam);

最后,您需要为存储过程设置cmd.CommandType = CommandType.StoredProcedure。

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2014-01-23
    • 2012-07-01
    相关资源
    最近更新 更多