Retrieve value from csv file and replace it in database in C#

筅森魡賤 提交于 2019-12-12 05:13:21

问题


I am creating an application which reads a csv file into a data grid view in C#. The file consists of data such as item number and stock.

If item number is same then its stock should be added and then replace the present stock in the database with same item number.

I am able to add stock from the csv file to the database with same item numbers in the database but I am not able to replace stock.

Here is my code:

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

I am using SQL server 2008 R2.


回答1:


Your example above had lots of little problems.

Study the basic example below as a better approach.

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


来源:https://stackoverflow.com/questions/5570172/retrieve-value-from-csv-file-and-replace-it-in-database-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!