批量数据替换助手V2.0版发布,欢迎使用

盼望着,盼望着,批量数据替换助手V2.0版终于发布了,^\(^o^)/。
你可以点击 批量数据替换助手V1.0版 了解它的由来。

很感谢园子里的朋友cdboy、yongfa365、心儿醉 xinerzhui、Vincent.Q提出了他们对1.0版本的修改意见和鼓励。在本次2.0版本中主要对软件操作的友好性以及大数据量处理性能优化做了改进。主要修改如下:
1.改进点击替换按钮时没有选中任何表的提示信息。
2.遍历表数据时不采用原先的DataTable来做数据载体,而改用SqlDataReader循环读取数据,读取数据的同时对数据进行处理。这样一来,节省了内存空间,数据读取速度也提高了。

 #region 得到要更新的sql语句集合
/// <summary>
/// 得到要更新的sql语句集合
/// </summary>
/// <param name="sqlList"></param>
/// <param name="tabInfo"></param>
/// <param name="searchString"></param>
/// <param name="replaceString"></param>
/// <param name="kt"></param>
public static void GetUpdateSqlList(ref List<string> sqlList, TableInfo tabInfo, string searchString, string replaceString, KeyType kt)
{
SqlDataReader dr = GetTableData(tabInfo);

List<ColumnInfo> primaryKeys = tabInfo.GetCols(ColType.PrimaryKey); //筛选类型为主键的列
List<ColumnInfo> cols = tabInfo.GetCols(ColType.Normal); //筛选非主键列

ColumnInfo columnInfo = null;
List<ColumnInfo> colInfoList = new List<ColumnInfo>();

while (dr.Read())
{
colInfoList.Clear();

for (int i = 0; i < primaryKeys.Count; i++)
{
columnInfo = primaryKeys[i];
primaryKeys[i].ColValue = dr[columnInfo.ColName].ToString();
}


for (int i = 0; i < cols.Count; i++)
{
columnInfo = new ColumnInfo();
columnInfo.ColName = cols[i].ColName;
columnInfo.ColValue = dr[columnInfo.ColName].ToString();

colInfoList.Add(columnInfo);
}


if (HasCheckedReplace(ref colInfoList, searchString, replaceString, kt))
{
sqlList.Add(GetRowUpdateSql(tabInfo, colInfoList, primaryKeys));
}
}
dr.Close();
}
#endregion

3.采用批量sql脚本更新策略,即当有1w条sql语句需要执行时我们将它分成10次发给数据库,每次发送1k条sql语句的脚本块。这样一来节省了与数据库的交互次数,同时分多次发送的策略可以有效的降低网络带宽的占用。

                List<string> sqlList = new List<string>();
foreach (TableInfo tabInfo in tabList)
{

ReplaceHelper.GetUpdateSqlList(ref sqlList, tabInfo, searchString, replaceString, kt);
}

StreamWriter sw = File.AppendText(string.Format("update{0}.sql",DateTime.Now.ToString("yyyyMMddHHmmss"))); //将更新Sql语句写入文件
int executeSqlCount = 1000; //单次批处理包含sql语句的条数
int executeTimes = (sqlList.Count % executeSqlCount) == 0 ? (sqlList.Count / executeSqlCount) : (sqlList.Count / executeSqlCount) +1; //批处理的次数
StringBuilder sb = null;

int startIndex = 0;
int endIndex = 0;

pBar1.Visible = true;
pBar1.Minimum = 1;
pBar1.Value = 1;
pBar1.Step = executeSqlCount;
pBar1.Maximum = sqlList.Count;


for (int i = 0; i < executeTimes; i++)
{
sb = new StringBuilder();
startIndex = i * executeSqlCount;
endIndex = startIndex + executeSqlCount -1;
endIndex = endIndex >= sqlList.Count ? sqlList.Count -1 : endIndex;

for (int j = startIndex; j <= endIndex; j++)
{
sb.AppendLine(sqlList[j]);
}


sw.Write(sb.ToString());
ReplaceHelper.ExecuteSql(sb.ToString());
pBar1.PerformStep();
}

sw.Close();



4.替换数据的同时将sql语句保存到文本文件中,已备以后使用,更方便和人性化。

就这么多,欢迎各位童鞋有发表你的意见。最后附上程序源码的下载地址,http://dl.dbank.com/c0wexphaza

原文地址:https://www.cnblogs.com/huangzelin/p/2203958.html