本文整理汇总了C#中System.Data.SqlClient.SqlBulkCopy.Close方法的典型用法代码示例。如果您正苦于以下问题:C# System.Data.SqlClient.SqlBulkCopy.Close方法的具体用法?C# System.Data.SqlClient.SqlBulkCopy.Close怎么用?C# System.Data.SqlClient.SqlBulkCopy.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Data.SqlClient.SqlBulkCopy
的用法示例。
在下文中一共展示了System.Data.SqlClient.SqlBulkCopy.Close方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: OleCopyToTempTable
/// <summary>
/// Use bulk copy to insert the worksheet rows into the specified table
/// </summary>
/// <param name="tempTableName"></param>
/// <param name="excelConnectionString"></param>
/// <param name="worksheetName"></param>
private void OleCopyToTempTable(string tempTableName, string excelConnectionString, string worksheetName)
{
using (System.Data.OleDb.OleDbConnection excelConnection = new System.Data.OleDb.OleDbConnection(excelConnectionString))
{
try
{
excelConnection.Open();
if (string.IsNullOrEmpty(ExcelQuery))
ExcelQuery = "SELECT * FROM [" + worksheetName + "$]";
else
ExcelQuery += "FROM [" + worksheetName + "$]";
using (System.Data.OleDb.OleDbCommand selectAllComand = new System.Data.OleDb.OleDbCommand(ExcelQuery)) //SELECT * FROM [" + worksheetName + "$]"))
{
selectAllComand.Connection = excelConnection;
using (System.Data.OleDb.OleDbDataReader excelReader = selectAllComand.ExecuteReader())
{
using (System.Data.SqlClient.SqlBulkCopy bc = new System.Data.SqlClient.SqlBulkCopy(_dbConn))
{
bc.BatchSize = 50;
bc.DestinationTableName = @"[dbo].[" + tempTableName + @"]"; //tempDatasheetImport]";
// User notification with the SqlRowsCopied event
//bc.NotifyAfter = 100;
//bc.SqlRowsCopied += new SqlRowsCopiedEventHandler(OnSqlRowsCopied);
if (_dbConn.State != System.Data.ConnectionState.Open)
_dbConn.Open();
bc.WriteToServer(excelReader);
bc.Close();
}
excelReader.Close();
_dbConn.Close();
}
}
excelConnection.Close();
}
catch (InvalidOperationException ex)
{
//if (ex.Message != @"Column 'tmpIndentNo' does not allow DBNull.Value.") //not a problem, just reaching the footer and easier to skip an exception than try to filter it out
// throw new Exception("OleCopyToTempTable 1: " + ex.Message);
//else if (ex.Message != @"Column 'tmpMSEP' does not allow DBNull.Value.") //not a problem, just reaching the footer
// throw new Exception("OleCopyToTempTable 2: " + ex.Message);
//else if (ex.Message != @"Column 'tmpIndentNo' does not allow DBNull.Value.")
// throw new Exception("OleCopyToTempTable 3: " + ex.Message);
}
finally
{
excelConnection.Close();
_dbConn.Close();
}
//catch (Exception ex)
//{
// throw new Exception("OleCopyToTempTable 4: " + ex.Message);
//}
}
}
示例2: InsertDataTable
/// <summary>
/// 使用datatable保存方法,table中的列要與數據表中的列一至,并且將數據表名放到table.TableName中
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public bool InsertDataTable(DataTable dt)
{
try
{
string dbcon = System.Configuration.ConfigurationManager.ConnectionStrings["LinqToSQLModel.Properties.Settings.HBPMSDBConnectionString"].ConnectionString;
using (System.Data.SqlClient.SqlBulkCopy sbc = new System.Data.SqlClient.SqlBulkCopy(dbcon))
{
sbc.BatchSize = 100000;//每次传输的行数
sbc.BulkCopyTimeout = 900;
sbc.DestinationTableName = dt.TableName;
sbc.WriteToServer(dt);
sbc.Close();
}
return true;
}
catch
{
}
return false;
}