【问题标题】:Failed trying to insert my data尝试插入我的数据失败
【发布时间】:2016-01-23 11:36:09
【问题描述】:

我正在尝试将一些列表插入到我的数据库(Microsoft)中,但我收到了这个运行时错误

无法将参数值从 List`1 转换为 Int32。

这是我的代码

  public void InsertInventory(DateTime _date, int _customer_Id,
                            int _employee_Id, List<int> _product_Id,
                            List<int> _amountSold,
                            List<int> _unitPrice, List<int> _totalPrice)
    {
        Connection_String = @"Data Source=MOSTAFA-PC;Initial Catalog="
                           + "Sales and Inventory System"
                           + ";Integrated Security=TrueData Source=MOSTAFA-PC;Initial Catalog="
                           + "Sales and Inventory System"
                           + ";Integrated Security=True;";

        Query = "insert into Inventory" +
                  "(Customer_Id,Employee_Id,Product_Id,[Date],[Amount Sold],[Unit Price],[Total Price])" +
                    "values (@customer_id,@Employee_id,@Product_id,@[Date],@[Amount_Sold],@[Unit_Price],@[Total_Price])";

        using (Con = new SqlConnection(Connection_String))
        using (Cmd = new SqlCommand(Query, Con))
        {
            Cmd.Parameters.Add("@customer_id", SqlDbType.Int);
            Cmd.Parameters.Add("@Employee_id", SqlDbType.Int);
            Cmd.Parameters.Add("@Product_id", SqlDbType.Int);
           //Cmd.Parameters.Add("@[Date]", SqlDbType.NVarChar);
            Cmd.Parameters.Add("@[Date]", SqlDbType.Date);
            Cmd.Parameters.Add("@[Amount_sold]", SqlDbType.Int);
            Cmd.Parameters.Add("@[Unit_Price]", SqlDbType.Decimal);
            Cmd.Parameters.Add("@Total_Price", SqlDbType.Decimal);

            Cmd.Connection = Con;
            Con.Open();

            int RecordToAdd = _product_Id.Count;
            for (int i = 0; i < RecordToAdd; i++)
            {
                Cmd.Parameters["@customer_id"].Value = _customer_Id;
                Cmd.Parameters["@Employee_id"].Value = _employee_Id;
                Cmd.Parameters["@Product_id"].Value = _product_Id;
                Cmd.Parameters["@[Date]"].Value = _date;
                Cmd.Parameters["@[Amount_sold]"].Value = _amountSold;
                Cmd.Parameters["@[Unit_Price]"].Value = _unitPrice;
                Cmd.Parameters["@Total_Price"].Value = _totalPrice;
                Cmd.ExecuteNonQuery();
            }

我搜索了网站,但找不到任何有用或与我的问题相似的东西

我该怎么办?

【问题讨论】:

  • 您似乎正在尝试将 List 插入到预期和整数的参数中。如果您尝试做的是基于列表的批量插入,您应该使用 for 循环中的 i 迭代器作为列表的索引号。 Cmd.Parameters["@Product_id"].Value = _product_Id[i]; 是一个涉及列表 的示例。

标签: c# mysql winforms runtime-error runtimeexception


【解决方案1】:

改变这个:

        int RecordToAdd = _product_Id.Count;
        for (int i = 0; i < RecordToAdd; i++)
        {
            Cmd.Parameters["@customer_id"].Value = _customer_Id;
            Cmd.Parameters["@Employee_id"].Value = _employee_Id;
            Cmd.Parameters["@Product_id"].Value = _product_Id;
            Cmd.Parameters["@[Date]"].Value = _date;
            Cmd.Parameters["@[Amount_sold]"].Value = _amountSold;
            Cmd.Parameters["@[Unit_Price]"].Value = _unitPrice;
            Cmd.Parameters["@Total_Price"].Value = _totalPrice;
            Cmd.ExecuteNonQuery();
        }

到这里:

        int RecordToAdd = _product_Id.Count;
        for (int i = 0; i < RecordToAdd; i++)
        {
            Cmd.Parameters["@customer_id"].Value = _customer_Id;
            Cmd.Parameters["@Employee_id"].Value = _employee_Id;
            Cmd.Parameters["@Product_id"].Value = _product_Id[i];
            Cmd.Parameters["@[Date]"].Value = _date;
            Cmd.Parameters["@[Amount_sold]"].Value = _amountSold[i];
            Cmd.Parameters["@[Unit_Price]"].Value = _unitPrice[i];
            Cmd.Parameters["@Total_Price"].Value = _totalPrice[i];
            Cmd.ExecuteNonQuery();
        }

请注意,在第二个块中,我使用循环的迭代器来索引我每次插入的列表中的哪个项目。

编辑:另外,您构建的 connection_string 看起来不正确。您可能需要查看它并更正任何错误

【讨论】:

    【解决方案2】:

    将 FUNCTION PARAMETERS 中的“List”更改为 Int32 或 Integer。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 2011-10-24
      • 2021-08-24
      • 2015-03-21
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多