[英]Turn csv stream parser sql insert into bulk insert
我有這種獲取 CSV 的特定方法,其中 Web 服務在 Web 瀏覽器中生成 CSV 文件,然后我獲取所有數據並解析它並將其流式傳輸到變量中,並為每一行執行 SQL 插入。 現在的問題是這需要很長時間,我不確定如何將其轉換為批量插入。
我的代碼在下面
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!");
}
}
如果有人能夠幫助我解決這個問題,將不勝感激。
您需要創建一個DataTable,然后添加一個到數據表的映射,以便將DataTable 的列映射到數據庫的列。 我從創建和填充數據表開始。 現在在網上搜索有關將數據表批量復制到數據庫的信息。
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.