.net DataTable批量添加、修改到数据库表中(有问题,请高手指教)
利用SqlDataAdapter.Update批量添加、修改数据库
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace pcd.Tools.DAL
{
/// <summary>
/// sql数据库大批量操作
/// </summary>
public class SqlBulkCopy
{
#region Insert
/// <summary>
/// 大批量添加数据ds.Tables[0] 表名strTblName(数据库表名称)
/// </summary>
/// <param name="ds"></param>
/// <param name="strTblName"></param>
/// <returns>受影响的行数</returns>
public static int SqlBulkInsert(DataTable dt, string strTblName, SqlConnection conn)
{
int affect = 0;
try
{
SqlCommand myCommand = new SqlCommand("select * from " + strTblName, conn);
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand();
foreach (DataRow dr in dt.Rows)
{
if (dr.RowState != DataRowState.Added)
dr.SetAdded();
}
conn.Open();
affect = myAdapter.Update(dt);
// ds.AcceptChanges();
conn.Close();
// return ds;
}
catch (Exception err)
{
conn.Close();
throw err;
}
return affect;
}
#endregion
#region Update
/// <summary>
/// 大批量更改数据ds.Tables[0] 表名strTblName(数据库表名称,表需要有主键)
/// </summary>
/// <param name="ds"></param>
/// <param name="strTblName"></param>
/// <returns>受影响的行数</returns>
public static int SqlBulkUpdate(DataTable dt, string strTblName, SqlConnection conn)
{
int affect = 0;
try
{
//foreach (DataRow dr in dt.Rows)
//{
// if (dr.RowState == DataRowState.Added)
// {
// dr[0] = dr[0];
// dr.AcceptChanges();
// dr.SetModified();
// }
//}
SqlCommand myCommand = new SqlCommand("select * from " + strTblName, conn);
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
conn.Open();
affect = myAdapter.Update(dt);
// ds.AcceptChanges();
conn.Close();
// return ds; //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds
}
catch (Exception err)
{
conn.Close();
throw err;
}
return affect;
}
/// <summary>
/// 大批量更改数据ds.Tables[0] 表名strTblName(数据库表名称,表需要有主键)
/// </summary>
/// <param name="ds"></param>
/// <param name="strTblName"></param>
/// <returns>受影响的行数</returns>
public static int SqlBulkUpdate(DataTable dt, string strTblName, SqlConnection conn, SqlCommand updatecmd)
{
int affect = 0;
try
{
//foreach (DataRow dr in dt.Rows)
//{
// if (dr.RowState == DataRowState.Added)
// {
// dr[0] = dr[0];
// dr.AcceptChanges();
// dr.SetModified();
// }
//}
SqlCommand myCommand = new SqlCommand("select * from " + strTblName, conn);
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.UpdateCommand = updatecmd;
conn.Open();
affect = myAdapter.Update(dt);
// ds.AcceptChanges();
conn.Close();
// return ds; //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds
}
catch (Exception err)
{
conn.Close();
throw err;
}
return affect;
}
#endregion
}
}
该方法将一个DataTable写到数据库表的确很快,我在另个应用中,同时涉及到3个5万条以上数据批量处理时,效率并不高。 using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace pcd.Tools.DAL
{
/// <summary>
/// sql数据库大批量操作
/// </summary>
public class SqlBulkCopy
{
#region Insert
/// <summary>
/// 大批量添加数据ds.Tables[0] 表名strTblName(数据库表名称)
/// </summary>
/// <param name="ds"></param>
/// <param name="strTblName"></param>
/// <returns>受影响的行数</returns>
public static int SqlBulkInsert(DataTable dt, string strTblName, SqlConnection conn)
{
int affect = 0;
try
{
SqlCommand myCommand = new SqlCommand("select * from " + strTblName, conn);
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.InsertCommand = myCommandBuilder.GetInsertCommand();
foreach (DataRow dr in dt.Rows)
{
if (dr.RowState != DataRowState.Added)
dr.SetAdded();
}
conn.Open();
affect = myAdapter.Update(dt);
// ds.AcceptChanges();
conn.Close();
// return ds;
}
catch (Exception err)
{
conn.Close();
throw err;
}
return affect;
}
#endregion
#region Update
/// <summary>
/// 大批量更改数据ds.Tables[0] 表名strTblName(数据库表名称,表需要有主键)
/// </summary>
/// <param name="ds"></param>
/// <param name="strTblName"></param>
/// <returns>受影响的行数</returns>
public static int SqlBulkUpdate(DataTable dt, string strTblName, SqlConnection conn)
{
int affect = 0;
try
{
//foreach (DataRow dr in dt.Rows)
//{
// if (dr.RowState == DataRowState.Added)
// {
// dr[0] = dr[0];
// dr.AcceptChanges();
// dr.SetModified();
// }
//}
SqlCommand myCommand = new SqlCommand("select * from " + strTblName, conn);
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
conn.Open();
affect = myAdapter.Update(dt);
// ds.AcceptChanges();
conn.Close();
// return ds; //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds
}
catch (Exception err)
{
conn.Close();
throw err;
}
return affect;
}
/// <summary>
/// 大批量更改数据ds.Tables[0] 表名strTblName(数据库表名称,表需要有主键)
/// </summary>
/// <param name="ds"></param>
/// <param name="strTblName"></param>
/// <returns>受影响的行数</returns>
public static int SqlBulkUpdate(DataTable dt, string strTblName, SqlConnection conn, SqlCommand updatecmd)
{
int affect = 0;
try
{
//foreach (DataRow dr in dt.Rows)
//{
// if (dr.RowState == DataRowState.Added)
// {
// dr[0] = dr[0];
// dr.AcceptChanges();
// dr.SetModified();
// }
//}
SqlCommand myCommand = new SqlCommand("select * from " + strTblName, conn);
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.UpdateCommand = updatecmd;
conn.Open();
affect = myAdapter.Update(dt);
// ds.AcceptChanges();
conn.Close();
// return ds; //数据集的行状态在更新后会都变为: UnChange,在这次更新后客户端要用返回的ds
}
catch (Exception err)
{
conn.Close();
throw err;
}
return affect;
}
#endregion
}
}
比如设备租金结算模块:
这里一次性结算的设备至少在5~10万台设备,这样就涉及到15~30W条记录的添加和修改,尽管使用DataTable批量存储但是结算时耗费的时间还是难以让人接受。高手指点吧。