从csv文件中检索值并在C#中将其替换为数据库

时间:2011-04-06 17:01:00

标签: c# tsql sql-server-2008-r2

我正在创建一个应用程序,它将csv文件读入C#中的数据网格视图。该文件包含项目编号和库存等数据。

如果项目编号相同,则应添加其库存,然后使用相同的料号替换数据库中的当前库存。

我能够将csv文件中的库存添加到数据库中具有相同项目编号的数据库,但我无法替换库存。

这是我的代码:

    string sql_select = "select count(*) from PRODUCTS where item_no= '" + 
                            itemNo + "'";
    SqlCommand cmdCheckPmk = new SqlCommand(sql_select, Class1.conn);

    int selectItemNo = Convert.ToInt32(cmdCheckPmk.ExecuteScalar());

    if (selectItemNo != 0)
    {
        string sql_update = "update PRODUCTS set item_stock=+'" + Stock + 
                            "' where item_no= '" + itemNo + "'";
        SqlCommand cmd1 = new SqlCommand(sql_update, Class1.conn);
    }
    else
    {
        SqlCommand cmd11 = new SqlCommand("insert into PRODUCTS(item_no,item_name,price,cost,item_stock,dept_id,tax_rate1,tax_rate2,bulk_price,bulk_qty) values ('" + itemNo + "','" + itemName + "'," + price + "," + cost + "," + Stock + ",'" + dept + "','" + tax1 + "','" + tax2 + "'," + BulkPrize +"," + BulkQty +") ", Class1.conn);
        cmd11.ExecuteNonQuery();
    }

我使用的是SQL Server 2008 R2。

1 个答案:

答案 0 :(得分:0)

上面的例子有很多小问题。

将以下基本示例作为更好的方法进行研究。

// Pass in your SQL Connection object so you do not have to worry about
// multiple open connections
public int jp2Test(SqlConnection conn) {
  // Verify someone did not pass in a NULL object
  if (conn != null) {
    string sql_text = "select count(*) from PRODUCTS where item_no=@item_no";
    using (SqlCommand cmd = new SqlCommand(sql_text, conn)) {
      // Get in the habit of using Parameters. If you know the SqlDbType, use one of
      // the Parameter overloads that provides for this.
      cmd.Parameters.AddWithValue("@item_no", itemNo);
      // Open the connection if it is not already
      if ((cmd.Connection.State & ConnectionState.Open) != ConnectionState.Open) {
        cmd.Connection.Open();
      }
      // initialize an item_number variable
      int selectedItemNo = -1;
      object value = cmd.ExecuteScalar();
      // Check for both NULL and DBNull
      if ((value != null) && (value != DBNull.Value)) {
        selectedItemNo = Convert.ToInt32(value);
      }
      // Update your SQL Text based on the value you received.
      if (0 < selectedItemNo) {
        sql_text = "update PRODUCTS set item_stock=@item_stock where item_no=@item_no";
        // this value is already included
        // cmd.Parameters.AddWithValue("@item_no", itemNo);
        // this is a common value that will be added after the conditional
        // cmd.Parameters.AddWithValue("@item_stock", Stock);
      } else {
        sql_text = "insert into PRODUCTS" +
          " (item_no, item_name, price, cost, item_stock, dept_id, tax_rate1, tax_rate2, bulk_price, bulk_qty)" +
          " values " +
          "(@item_no,@item_name,@price,@cost,@item_stock,@dept_id,@tax_rate1,@tax_rate2,@bulk_price,@bulk_qty)";
        // this value is already included
        // cmd.Parameters.AddWithValue("@item_no", itemNo);
        cmd.Parameters.AddWithValue("@item_name", itemName);
        cmd.Parameters.AddWithValue("@price", price);
        cmd.Parameters.AddWithValue("@cost", cost);
        // this is a common value that will be added after the conditional
        // cmd.Parameters.AddWithValue("@item_stock", Stock);
        cmd.Parameters.AddWithValue("@dept_id", dept);
        cmd.Parameters.AddWithValue("@tax_rate1", tax1);
        cmd.Parameters.AddWithValue("@tax_rate2", tax2);
        cmd.Parameters.AddWithValue("@bulk_price", BulkPrize);
        cmd.Parameters.AddWithValue("@bulk_qty", BulkQty);
      }
      cmd.CommandText = sql_text;
      cmd.Parameters.AddWithValue("@item_stock", Stock);
      // Return the number of SQL records that were affected.
      return cmd.ExecuteNonQuery();
    }
  }
  // return -1 on Error
  return -1;
}