-2
我有這種特定的方式獲取CSV,其中一個Web服務在Web瀏覽器中生成CSV文件,然後我抓住所有數據並解析它並將其流入變量併爲每行執行一次SQL插入。現在問題是這需要很長時間,我不知道如何將其轉換爲批量插入。把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!");
}
}
如果有人能夠幫助我想出解決辦法,將不勝感激。
您能夠將csv流式傳輸(或以其他方式導入)到DataTable中嗎? –
您可以嘗試使用Cinchoo ETL lib進行批量插入,請參閱此文章https://www.codeproject.com/Tips/1170498/Cinchoo-ETL-Bulk-Insert-CSV-File-into-SQLServer以獲取更多信息 – RajN