我有这种获取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!");
}
}
如果有人能够帮我解决这个问题,我将非常感激。
答案 0 :(得分:1)
您需要创建一个DataTable,然后向数据表添加映射,以便DataTable的列映射到数据库的列。我从创建和填充数据表开始。现在搜索web以获取有关批量复制数据到数据库的信息。
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});