过程需要未提供的参数 - 日志记录说的不同

时间:2014-08-29 19:17:14

标签: c# sql-server dictionary parameters

我传递参数的过程说我缺少@accountNumber,字典键。我有两种使用相同代码的方法。一个工作,一个不工作。这两个程序也以同样的方式宣布,所以我不知所措。

修改:原来我在方法中遗漏了comm.CommandType = CommandType.StoredProcedure;

程序:

CREATE PROCEDURE [dbo].[updateNumbers_StatementDB]
(
 @accountNumber varchar(50),
 @accountType char(4),
 @padding varchar(50),
 @proc_dateStart datetime,
 @proc_dateEnd datetime
 )

调用updateNumbers_StatementDB的方法:

using (SqlCommand comm = new SqlCommand("updateNumbers_StatementDB", osql))
                    {
                        strMsg = "Statement DB";
                        _newLog.LogIt(strMsg);
                        foreach (KeyValuePair<string, string> item in dt)
                        {   //problem parameter
                            var param = comm.Parameters.AddWithValue("@accountNumber", item.Key);
                            //item.key shows up in logging as 1020, what I set it as. 
                            strMsg = "item key: " + item.Key;
                            _newLog.LogIt(strMsg);

                            var accountTypeParam = comm.Parameters.AddWithValue("@accountType", item.Value);
                            strMsg = "item value: " + item.Value;
                            _newLog.LogIt(strMsg);

                            var paddingParam = comm.Parameters.AddWithValue("@padding", padding);
                            var dateStartParam = comm.Parameters.AddWithValue("@proc_dateStart", startDate);
                            var dateEndParam = comm.Parameters.AddWithValue("@proc_dateEnd", endDate);

                            var reader = comm.ExecuteNonQuery(); //or NonQuery, etc. 
                            strMsg = "Statement query executed at least once";
                            _newLog.LogIt(strMsg);
                        }
                    }

添加其他有效的方法:

 using (SqlCommand comm = new SqlCommand("updateNumbers_ArchiveDB", osql))
                    {

                    comm.CommandType = CommandType.StoredProcedure;

                    foreach (KeyValuePair<string, string> item in dt)
                    {
                        var param = comm.Parameters.AddWithValue("@accountNumber", item.Key);

                        strMsg = "item key: " + item.Key;
                        _newLog.LogIt(strMsg);
                        var accountTypeParam = comm.Parameters.AddWithValue("@accountType", item.Value);

                        strMsg = "item value: " + item.Value;
                        _newLog.LogIt(strMsg);

                        var paddingParam = comm.Parameters.AddWithValue("@padding", padding);
                        var dateStartParam = comm.Parameters.AddWithValue("@proc_dateStart", startDate);
                        var dateEndParam = comm.Parameters.AddWithValue("@proc_dateEnd", endDate);
                        strMsg = "Getting to the bottom of the foreach loop";
                        _newLog.LogIt(strMsg);
                        var reader = comm.ExecuteNonQuery(); //or NonQuery, etc. 
                        strMsg = "Query executed at least once";
                        _newLog.LogIt(strMsg);
                    }
                }

程序:

CREATE PROCEDURE [dbo].[updateNumbers_ArchiveDB]
(
 @accountNumber varchar(50),
 @accountType char(4),
 @padding varchar(50),
 @proc_dateStart datetime,
 @proc_dateEnd datetime
 )

通常,当您的变量为null时会发生此错误。但是,在这种情况下,我的日志记录显示变量具有值。关于为什么我的程序没有看到价值的任何想法?

1 个答案:

答案 0 :(得分:3)

在第一种方法中缺少comm.CommandType = CommandType.StoredProcedure;