SqlBulkCopy的用法
SqlBuck:
protected void Button1_Click(object sender, EventArgs e)
{
string strOle = string.Empty;
strOle = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/Files/copy.xlsx") + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'";
string SrcConString;
string DesConString;
OleDbConnection SrcCon = new OleDbConnection();
SqlConnection DesCon = new SqlConnection();
OleDbCommand SrcCom = new OleDbCommand();
OleDbDataAdapter SrcAdapter = new OleDbDataAdapter();
DataTable dt = new DataTable();
SrcConString = strOle;
//ConfigurationManager.ConnectionStrings["SrcDBConnectionString"].ConnectionString;
DesConString =
ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
SrcCon.ConnectionString = SrcConString;
SrcCom.Connection = SrcCon;
SrcCom.CommandText = " SELECT * From [Sheet1$]";
SrcCom.CommandType = CommandType.Text;
SrcCom.Connection.Open();
SrcAdapter.SelectCommand = SrcCom;
SrcAdapter.Fill(dt);
DataColumn dc = new DataColumn("Flag", typeof(string));
dc.DefaultValue = "True";
dt.Columns.Add(dc);
dt.AcceptChanges();
SqlBulkCopy DesBulkOp;
DesBulkOp = new SqlBulkCopy(DesConString,
SqlBulkCopyOptions.UseInternalTransaction);
DesBulkOp.BulkCopyTimeout = 500000000;
DesBulkOp.SqlRowsCopied +=
new SqlRowsCopiedEventHandler(OnRowsCopied);
DesBulkOp.NotifyAfter = dt.Rows.Count;
try
{
DesBulkOp.DestinationTableName = "My_Orders";
DesBulkOp.WriteToServer(dt);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
SrcCon.Close();
DesCon.Close();
}
}
private void OnRowsCopied(object sender, SqlRowsCopiedEventArgs args)
{
Response.Write(args.RowsCopied.ToString() + " rows are copied<Br>");
}
{
string strOle = string.Empty;
strOle = "provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/Files/copy.xlsx") + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1'";
string SrcConString;
string DesConString;
OleDbConnection SrcCon = new OleDbConnection();
SqlConnection DesCon = new SqlConnection();
OleDbCommand SrcCom = new OleDbCommand();
OleDbDataAdapter SrcAdapter = new OleDbDataAdapter();
DataTable dt = new DataTable();
SrcConString = strOle;
//ConfigurationManager.ConnectionStrings["SrcDBConnectionString"].ConnectionString;
DesConString =
ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
SrcCon.ConnectionString = SrcConString;
SrcCom.Connection = SrcCon;
SrcCom.CommandText = " SELECT * From [Sheet1$]";
SrcCom.CommandType = CommandType.Text;
SrcCom.Connection.Open();
SrcAdapter.SelectCommand = SrcCom;
SrcAdapter.Fill(dt);
DataColumn dc = new DataColumn("Flag", typeof(string));
dc.DefaultValue = "True";
dt.Columns.Add(dc);
dt.AcceptChanges();
SqlBulkCopy DesBulkOp;
DesBulkOp = new SqlBulkCopy(DesConString,
SqlBulkCopyOptions.UseInternalTransaction);
DesBulkOp.BulkCopyTimeout = 500000000;
DesBulkOp.SqlRowsCopied +=
new SqlRowsCopiedEventHandler(OnRowsCopied);
DesBulkOp.NotifyAfter = dt.Rows.Count;
try
{
DesBulkOp.DestinationTableName = "My_Orders";
DesBulkOp.WriteToServer(dt);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
SrcCon.Close();
DesCon.Close();
}
}
private void OnRowsCopied(object sender, SqlRowsCopiedEventArgs args)
{
Response.Write(args.RowsCopied.ToString() + " rows are copied<Br>");
}