参数数量错误(参数化查询)

发布于 2024-11-26 11:00:18 字数 2477 浏览 1 评论 0原文

问:

当我尝试执行以下参数化查询时:

INSERT INTO days (day,short,name,depcode,studycode,batchnum) values (?,?,?,?,?,?);SELECT SCOPE_IDENTITY();

通过 command.ExecuteScalar();

抛出以下异常:

错误 [07001] [Informix .NET 提供程序]参数数量错误。

问题出在哪里?

编辑:

 public static int InsertDays(List<Day> days)
        {

            int affectedRow = -1;
            Dictionary<string, string> daysParameter = new Dictionary<string, string>();
            try
            {
                foreach (Day a in days)
                {
                    daysParameter.Add("day", a.DayId.ToString());
                    daysParameter.Add("short", a.ShortName);
                    daysParameter.Add("name", a.Name);
                    daysParameter.Add("depcode", a.DepCode.ToString());
                    daysParameter.Add("studycode", a.StudyCode.ToString());
                    daysParameter.Add("batchnum", a.BatchNum.ToString());

                    affectedRow = DBUtilities.InsertEntity_Return_ID("days", daysParameter);
                    daysParameter.Clear();
                    if (affectedRow < 0)
                    {
                        break;
                    }
                }
            }
            catch (Exception ee)
            {
                string message = ee.Message;
            }

            return affectedRow;

        }

public static int InsertEntity_Return_ID(string tblName, Dictionary<string, string> dtParams)
        {
            int Result = -1;
            DBConnectionForInformix DAL_Helper = new DBConnectionForInformix("");
            string[] field_names = new string[dtParams.Count];
            dtParams.Keys.CopyTo(field_names, 0);
            string[] field_values = new string[dtParams.Count];
            string[] field_valuesParam = new string[dtParams.Count];
            dtParams.Values.CopyTo(field_values, 0);
            for (int i = 0; i < field_names.Length; i++)
            {
                field_valuesParam[i] = "?";
            }
            string insertCmd = @"INSERT INTO " + tblName + " (" + string.Join(",", field_names) + ") values (" + string.Join(",", field_valuesParam) + ");SELECT SCOPE_IDENTITY();";

        Result = int.Parse(DAL_Helper.Return_Scalar(insertCmd));
        return Result;
        }

Q:

When i try to execute the following parametrized query:

INSERT INTO days (day,short,name,depcode,studycode,batchnum) values (?,?,?,?,?,?);SELECT SCOPE_IDENTITY();

through command.ExecuteScalar();

throws the following exception:

ERROR [07001] [Informix .NET provider]Wrong number of parameters.

Where is the problem?

EDIT:

 public static int InsertDays(List<Day> days)
        {

            int affectedRow = -1;
            Dictionary<string, string> daysParameter = new Dictionary<string, string>();
            try
            {
                foreach (Day a in days)
                {
                    daysParameter.Add("day", a.DayId.ToString());
                    daysParameter.Add("short", a.ShortName);
                    daysParameter.Add("name", a.Name);
                    daysParameter.Add("depcode", a.DepCode.ToString());
                    daysParameter.Add("studycode", a.StudyCode.ToString());
                    daysParameter.Add("batchnum", a.BatchNum.ToString());

                    affectedRow = DBUtilities.InsertEntity_Return_ID("days", daysParameter);
                    daysParameter.Clear();
                    if (affectedRow < 0)
                    {
                        break;
                    }
                }
            }
            catch (Exception ee)
            {
                string message = ee.Message;
            }

            return affectedRow;

        }

public static int InsertEntity_Return_ID(string tblName, Dictionary<string, string> dtParams)
        {
            int Result = -1;
            DBConnectionForInformix DAL_Helper = new DBConnectionForInformix("");
            string[] field_names = new string[dtParams.Count];
            dtParams.Keys.CopyTo(field_names, 0);
            string[] field_values = new string[dtParams.Count];
            string[] field_valuesParam = new string[dtParams.Count];
            dtParams.Values.CopyTo(field_values, 0);
            for (int i = 0; i < field_names.Length; i++)
            {
                field_valuesParam[i] = "?";
            }
            string insertCmd = @"INSERT INTO " + tblName + " (" + string.Join(",", field_names) + ") values (" + string.Join(",", field_valuesParam) + ");SELECT SCOPE_IDENTITY();";

        Result = int.Parse(DAL_Helper.Return_Scalar(insertCmd));
        return Result;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜巴黎 2024-12-03 11:00:18

您尚未显示实际填充参数值的位置。。鉴于您有正确数量的问号,我怀疑这就是问题所在。

编辑:好的,现在您已经发布了更多代码,很明显出了什么问题:您的 Return_Scalar 方法不接受任何实际值!填充后,您不会在任何地方使用 field_values 。需要在命令中设置参数。

(顺便说一下,您还应该查看 .NET 命名约定。 ..)

You haven't shown where you're actually populating the parameter values. Given that you've got the right number of question marks, I suspect that's where the problem lies.

EDIT: Okay, now you've posted more code, it's obvious what's going wrong: your Return_Scalar method isn't accepting any actual values! You're not using field_values anywhere after populating it. You need to set the parameters in the command.

(You should also look at .NET naming conventions, by the way...)

俏︾媚 2024-12-03 11:00:18

确保在提供参数时其中一个值不为空。这可能会导致提供者忽略该参数。如果这是您的问题,请通过DBNull

编辑

正如 Jon 所说,您需要使用 command.Parameters 为命令提供要在查询中使用的参数。

Ensure that where you are providing the parameters that one of the values is not null. That may cause the provider to ignore the parameter. If this is your issue pass DBNull.

EDIT

As Jon stated you need to use command.Parameters to give the command the parameters to use in the query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文