简体   繁体   中英

Turn csv stream parser sql insert into bulk insert

I have this specific way of getting a CSV where a web service generates the CSV file in the web browser and I then grab all of the data and parse it and stream it into variables and do an SQL insert for each row. Now the problem is this takes to long and I am not sure how to convert this to do a bulk insert.

my code is below

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Net;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    using System.ComponentModel;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.FileIO;
    using System.IO;
    using System.Data.SqlClient;
    using System.Data.Sql;


    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

         WebClient client = new WebClient();
         Uri ur = new Uri("http://1.1.1.1/portal/FEDE?ORGANIZATION_ID=96&SERVLET_ACTION=getItemsList");
        public void scrapeCSV()
            {

        Stream myStream = client.OpenRead(ur);
        StreamReader stream = new StreamReader(myStream);
        //Parse the stream

        using (TextFieldParser parser = new TextFieldParser(stream))
        {
            parser.TextFieldType = FieldType.Delimited;
            parser.TextFieldType = FieldType.Delimited;
            parser.SetDelimiters(",");
            int rowCount = 1, colCount = 1;
            string strInsert = "";
            string rowName = "";
            string itemCode = "", description = "", barcode = "";
            int boxQty = 0, palletQty = 0;
            double weight = 0.0;
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["casi"].ConnectionString);
            conn.Open();
            SqlCommand cmd1 = new SqlCommand("delete from itemslist", conn);
            cmd1.ExecuteNonQuery();
            while (!parser.EndOfData)
            {
                //Processing row
                string[] row = parser.ReadFields();
                rowName = row[0].ToString();
                    if (rowCount > 2) //Skip header row
                    {


                        foreach (string field in row)
                        {
                                if (colCount == 1)
                                {
                                    itemCode = field;
                                }
                                else if (colCount == 2)
                                {

                                    description = field.Replace("'", "''"); ;
                                }
                                else if (colCount == 3)
                                {
                                    if (field != "")
                                    {
                                        boxQty = Convert.ToInt32(field);
                                    }
                                    else
                                    {
                                        boxQty = 0;
                                    }
                                }
                                else if (colCount == 4)
                                {
                                    if (field != "")
                                    {
                                        palletQty = Convert.ToInt32(field);
                                    }
                                    else
                                    {
                                        palletQty = 0;
                                    }
                                }
                                else if (colCount == 5)
                                {
                                    if (field != "")
                                    {
                                        weight = Convert.ToDouble(field);
                                    }
                                    else
                                    {
                                        weight = 0.0;
                                    }
                                }
                                else if (colCount == 6)
                                {
                                    barcode = field;
                                }
                                colCount++; 
                        }
                        colCount = 1;


                        strInsert = @"INSERT INTO ItemsList (ItemCode, Description, BoxQty, PalletQty,Weight,Barcode)
    VALUES ('" + itemCode + "', '" + description + "', '" + boxQty + "', '" + palletQty + "', '" + weight + "', '" + barcode + "')";



                            SqlCommand cmd2 = new SqlCommand(strInsert, conn);

       try
        {
         cmd2.ExecuteNonQuery();
        }
    catch (Exception ex)
         {
             //put code trace log here such as write a txt file content ex.tostring;
             if (ex is FormatException || ex is OverflowException)

             {
                 Response.Write(strInsert);
                 continue;
             }
             //continue;//will run code bellow cmd.ExecuteNonQuery(); or you can put any code running if
             Response.Write(strInsert);
             continue;
         }

                      }
                    this.Label1.Text = Convert.ToString(rowCount);
                    rowCount++;

                }
            conn.Close();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            scrapeCSV();
            Response.Write("Download finished!");
        }
    }

If someone is able to help me figure this out that would be very appreciated.

You need to create a DataTable and then add a mapping to the datatable so the columns of the DataTable are mapped to the columns of the database. I got you started by creating and filling the datatable. Now search web for info on bulk copy datatable to database.

            DataTable dt = new DataTable();

            dt.Columns.Add("ItemCode", typeof(string));
            dt.Columns.Add("Description", typeof(string));
            dt.Columns.Add("BoxQty", typeof(int));
            dt.Columns.Add("PalletQty", typeof(int));
            dt.Columns.Add("Weight", typeof(decimal));
            dt.Columns.Add("Barcode", typeof(string));

            //inside while  loop
            dt.Rows.Add(new object[] {itemCode, description, boxQty, palletQty, weight, barcode});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM