SqlBulkCopy很慢。我该怎么办?

时间:2018-11-19 19:53:33

标签: c#

我想从dataTable中插入200万行到SQL Server,但这需要很多时间。

这是我的代码

DataTable tbl_xml = new DataTable();

var xmlFilePath = (Server.MapPath("~/") + FileUpload1.FileName);//Location of the XML file.

DataSet dsCameraDetails = new DataSet();
dsCameraDetails.ReadXml(xmlFilePath);// Load XML in dataset

dtl = dsCameraDetails.Tables["Row"];
dtl.Columns.Add("state", typeof(string));
int countfile = dtl.Rows.Count;

SqlConnection con2 = new SqlConnection(ConfigurationManager.ConnectionStrings["azmoonak"].ConnectionString);
SqlBulkCopy bulk2 = new SqlBulkCopy(con2);
bulk2.DestinationTableName = "tbl_xml";

try
{
    foreach (DataColumn col in dtl.Columns)
        bulk2.ColumnMappings.Add(col.ColumnName, col.ColumnName);
}
catch(Exception eee)
{
}

con2.Open();

try
{
    bulk2.BulkCopyTimeout = 1000;
    bulk2.WriteToServer(dtl);
}
catch(Exception eee)
{
}

con2.Close();

实际上,我想从一个巨大的xml文件中读取数据,并在短时间内将其插入SQL Server。还有其他方法吗?

0 个答案:

没有答案