简体   繁体   English

将csv流解析器sql插入转成批量插入

[英]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.我有这种获取 CSV 的特定方法,其中 Web 服务在 Web 浏览器中生成 CSV 文件,然后我获取所有数据并解析它并将其流式传输到变量中,并为每一行执行 SQL 插入。 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.您需要创建一个DataTable,然后添加一个到数据表的映射,以便将DataTable 的列映射到数据库的列。 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});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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