Introduced into the batch table Datatable

 

Packaging batch submission to the data table for data sync

GetSelectFieldNames String Private (the dataTable the DataTable, String tableName = "", strWhere String = "") 
{ 
IF (the dataTable == null || dataTable.Columns.Count == 0) 
{ 
return ""; 
} 
IF (tableName.NotHasValue () ) 
{ 
tableName = dataTable.TableName; 
} 
var Columns = (SELECT from the DataColumn column in dataTable.Columns column.ColumnName) .ToList (); 

String strColumns String.Join = ( ",", Columns); 
String strSql = string.Format ( "SELECT from {0} {2} {}. 1", strColumns, tableName, strWhere); 
return strSql; 
} 

/// <Summary> 
/// bulk full table data synchronization 
/// ways to support the given case detailed information of the field error 
/// </ Summary>  
/// <param name="dataTable"></param>
/// <param name="toTableName"></param>
/// <returns></returns>
public bool BulkCopyToDataTable(DataTable dataTable, string toTableName = "")
{
if (dataTable == null || dataTable.Rows.Count == 0)
{
Tools.Debug("提交的表为空");
return true;
}
if (toTableName.NotHasValue())
{
toTableName = dataTable.TableName;
}
Tools.Debug("一共提交" + dataTable.Rows.Count + "条数据到" + toTableName);

using (var connection = new SqlConnection(DbConnectionString))
{

string strSelectSql = GetSelectFieldNames(dataTable, toTableName, "where 1=2");
SqlTransaction tran =null;
try
{
connection.Open();
var newDatatable = new DataTable();
using (var myDataAdapter = new SqlDataAdapter(strSelectSql, connection)) 
{ 

myDataAdapter.Fill(newDatatable);
for (int j = 0; j < dataTable.Rows.Count; j++)
{
newDatatable.Rows.Add(dataTable.Rows[j].ItemArray);
}

using (var sqlcommanBuilder = new SqlCommandBuilder(myDataAdapter))
{
tran = connection.BeginTransaction();
myDataAdapter.SelectCommand.Transaction = tran;
myDataAdapter.Update(newDatatable);
tran.Commit();
}
myDataAdapter.Dispose();
}
}
catch (SqlException ex)
{
if (tran != null) tran.Rollback();
Tools.Debug (String.Format ( "synchronization platform table: {0}, perform database: {1}, error: {2}", toTableName, DBConnectionString, ex.Message)); 
Tools.Error (EX); 
return to false ; 
} 
} 
return to true; 
}

  

Guess you like

Origin www.cnblogs.com/songconglai/p/11355838.html