【问题标题】:I cant insert data into my DB我无法将数据插入我的数据库
【发布时间】:2016-01-23 10:00:02
【问题描述】:

我正在尝试将数据插入到我的数据库中,当我按下按钮时它应该完成但它不会

我猜它来自我查询中某处的最后一层

这是我的代码

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

    } 

我不知道我的问题出在哪里

【问题讨论】:

  • 你的问题是你没有错误处理
  • 添加 int records = Cmd.ExecuteNonQuery(); 然后使用 MessageBox 显示 records 变量。如果记录已插入,则应为 1,否则应为 0。此外,该连接字符串文本是重复的。这只是一个错字吗?
  • 顺便说一句,你用 MySql TAG 标记了这个,但这似乎是一个 SQL Server 数据库,来自连接字符串和 Sql Server 特定类(如 SqlConnection 等)的使用......什么是正确的那么数据库呢?
  • @Steve 这是微软的 sql 服务器

标签: c# sql-server database winforms


【解决方案1】:

问题是由您为参数设置的值引起的。
您应该使用索引器从列表而不是整个列表中检索元素

// These doesn't change inside the loop, so set it once for all...
Cmd.Parameters["@customer_id"].Value = _customer_Id;
Cmd.Parameters["@Employee_id"].Value = _employee_Id;
Cmd.Parameters["@Date"].Value = _date;

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

【讨论】:

    猜你喜欢
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 2012-08-05
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-11-29
    相关资源
    最近更新 更多