1

问:

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

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;
        }

4

2 回答 2

3

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

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

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

于 2011-07-27T09:56:54.283 回答
1

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.

于 2011-07-27T10:00:27.647 回答