111,129
社区成员
发帖
与我相关
我的任务
分享
public static void info_import_allTable(string sqlTable, string[] tableHead)
{
try
{
using (SqlConnection destinationConnection = DbConnection.createConn())
{
destinationConnection.Open();
using (SqlBulkCopy bcp = new SqlBulkCopy(destinationConnection)) // use app.config 文件
{
// Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM " + sqlTable + ";", destinationConnection);
commandRowCount.CommandTimeout = 180; //180s 延时时间 3 分钟
long countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
System.Data.SqlClient.SqlBulkCopyColumnMapping sqlbkColumnMapping = new System.Data.SqlClient.SqlBulkCopyColumnMapping();
foreach (string head in tableHead)
{
//if (head.Trim() == "备注" && sqlTable.Trim() == "salary")
//{
// bcp.ColumnMappings.Add(head, "基本类备注");
//}
//else
//{
bcp.ColumnMappings.Add(head, head);
//}
}
// bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcpRowView);
bcp.BatchSize = 1;//每次传输的行数
bcp.NotifyAfter = 1;//进度提示的行数
//// bcp.DestinationTableName = sheetName;//目标表
bcp.DestinationTableName = sqlTable;//目标表
bcp.WriteToServer(excelDataSet.Tables[0]);// excelDataSet.Tables["sheet1"]
long countEnd = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
long rowscount = countEnd - countStart;
string message = "导入成功,共导入 " + rowscount + " 行";
MessageBox.Show(message, "导入完成", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
}
catch (System.InvalidOperationException ioe)
{
string error = ioe.ToString();
string captionName = "提示 未打开文件!";
// MessageBox.Show(error.Substring(0, error.IndexOf("\r")),captionName, MessageBoxButtons.OK, MessageBoxIcon.Information );
MessageBox.Show(error, captionName, MessageBoxButtons.OK, MessageBoxIcon.Information);
// errorInput.toFile(captionName, ioe.ToString());
}
catch (SqlException sqlexc)
{
MessageBox.Show(sqlexc.ToString(), "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
//Console.WriteLine(sqlexc.ToString());
}
catch (Exception exc)
{
/// string error = exc.ToString();
string fileName = "未打开文件或数据已经导入过!";
// string message = error.Substring(0, error.IndexOf("\r"));
MessageBox.Show(exc.Message, fileName, MessageBoxButtons.OK, MessageBoxIcon.Information);
// MessageBox.Show(error.Substring(0, error.IndexOf("\r")));
// errorInput.toFile(fileName, exc.ToString());
}
}
public static void info_import_allTable(DataTable csvBasicTable, string sqlTable, string[] tableHead)
{
//SqlBulkCopy
try
{
using (SqlConnection destinationConnection = DbConnection.createConn())
{
destinationConnection.Open();
using (SqlBulkCopy bcp = new SqlBulkCopy(destinationConnection)) // use app.config 文件
{
// Perform an initial count on the destination table.
SqlCommand commandRowCount = new SqlCommand("SELECT COUNT(*) FROM " + sqlTable + ";", destinationConnection);
commandRowCount.CommandTimeout = 180; //180s 延时时间 3 分钟
long countStart = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
System.Data.SqlClient.SqlBulkCopyColumnMapping sqlbkColumnMapping = new System.Data.SqlClient.SqlBulkCopyColumnMapping();
foreach (string head in tableHead)
{
//if (head.Trim() == "备注" && sqlTable.Trim() == "salary")
//{
// bcp.ColumnMappings.Add(head, "基本类备注");
//}
//else
//{
bcp.ColumnMappings.Add(head, head);
//}
}
// bcp.SqlRowsCopied += new System.Data.SqlClient.SqlRowsCopiedEventHandler(bcpRowView);
bcp.BatchSize = 1;//每次传输的行数
bcp.NotifyAfter = 1;//进度提示的行数
//// bcp.DestinationTableName = sheetName;//目标表
bcp.DestinationTableName = sqlTable;//目标表
bcp.WriteToServer( csvBasicTable );// excelDataSet.Tables["sheet1"]
long countEnd = System.Convert.ToInt32(commandRowCount.ExecuteScalar());
long rowscount = countEnd - countStart;
string message = "导入成功,共导入 " + rowscount + " 行";
MessageBox.Show(message, "导入完成", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
}
}
}
catch (Exception exc)
{
/// string error = exc.ToString();
string fileName = "未打开文件或数据已经导入过!";
// string message = error.Substring(0, error.IndexOf("\r"));
MessageBox.Show(exc.Message, fileName, MessageBoxButtons.OK, MessageBoxIcon.Information);
// MessageBox.Show(error.Substring(0, error.IndexOf("\r")));
// errorInput.toFile(fileName, exc.ToString());
}
}
// sqlbulkcopy