sqlserver 批量数据替换助手V1.0版发布

所属分类: 网络编程 / ASP.NET 阅读数: 654
收藏 0 赞 0 分享
这种方法操作繁琐,而且一般不是很懂数据库的人很难操作。于萌发了要写一个小程序的念头,经过两天时间的折腾这个小软件终于和各位见面了,希望各位童鞋多给点意见。说了这么些之后还是先上界面吧,^..^

现在就来说说这个小程序的开发思路吧。
第一步:通过 sp_helpdb系统存储过程得到SqlServer中的所有数据库名称。

复制代码 代码如下:

#region 测试数据库连接,并显示数据库列表
/// <summary>
/// 测试数据库连接,并显示数据库列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnTest_Click(object sender, EventArgs e)
{
this.btnTest.Enabled = false;
saveConfig();

ConfigInfo.Server = this.txtIP.Text.Trim();
ConfigInfo.DataBase = "master";
ConfigInfo.UID = this.txtUID.Text.Trim();
ConfigInfo.Pwd = this.txtPwd.Text.Trim();

try
{
DataTable dt = Data.SqlHelper.ExecuteDataset(ConfigInfo.getConnect(), CommandType.Text, "sp_helpdb").Tables[0];

this.cmbDataBaseList.DataSource = dt;
this.cmbDataBaseList.DisplayMember = "name";
this.cmbDataBaseList.SelectedIndex = 0;
this.cmbDataBaseList.DropDownStyle = ComboBoxStyle.DropDownList;

this.ExecuteFilterBtn.Enabled = true;
}
catch (Exception ex)
{
this.ExecuteFilterBtn.Enabled = false;
MessageBox.Show(string.Format("错误:{0}!",ex.Message),"错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
this.btnTest.Enabled = true;
}
}
#endregion


第二步:当选择某个数据库时得到数据库里面的所有表信息,通过下面Sql语句就可以查询到了。
select [name] from sysobjects where xtype='u' order by [name] asc

复制代码 代码如下:

#region 当选择不同的数据库时,读取数据库的表信息
/// <summary>
/// 当选择不同的数据库时,读取数据库的表信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
this.chkboxTableList.Items.Clear();
ConfigInfo.DataBase = ((DataRowView)this.cmbDataBaseList.SelectedItem)["name"].ToString();
DataSet ds = Data.SqlHelper.ExecuteDataset(ConfigInfo.getConnect(), CommandType.Text, "select [name] from sysobjects where xtype='u' order by [name] asc");

foreach (DataRow row in ds.Tables[0].Rows)
{
this.chkboxTableList.Items.Add(row["name"].ToString());
}
}
#endregion


第三步:当点击替换按钮时获取被选中表的信息,并遍历表中的行列信息,并进行查找替换。

复制代码 代码如下:

#region 执行批量替换操作
/// <summary>
/// 执行批量替换操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ExecuteFilterBtn_Click(object sender, EventArgs e)
{
saveConfig();
total = 0;
if (this.chkboxTableList.CheckedIndices.Count == 0) return; //没有选中任何表的情况
if (this.txtSearchKey.Text.Trim() == "")
{
DialogResult result = MessageBox.Show("当前查找内容为空,确认此操作?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
if (result == DialogResult.No) return;
}

this.ExecuteFilterBtn.Enabled = false;

List<TableInfo> tabList = new List<TableInfo>();
string searchString = this.txtSearchKey.Text.Trim() == "" ? " " : this.txtSearchKey.Text;
string replaceString = this.txtReplaceStr.Text;
KeyType kt = this.chkIsRegex.Checked == true ? KeyType.Regex : KeyType.Text;
bool isRegex = this.chkIsRegex.Checked;

//得到被选中表的基本信息,并添加到集合中
foreach (int index in this.chkboxTableList.CheckedIndices)
{
string tabName = this.chkboxTableList.Items[index].ToString();
TableInfo tInfo = FilterInfo.initTableInfo(tabName);
if (tInfo == null)
{
continue;
}
tabList.Add(tInfo);
}

try
{
if (tabList.Count == 0) return; //没有符合检测的数据表

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

//循环过滤表中要替换的数据
foreach (TableInfo info in tabList)
{
FilterInfo.Execute(info, searchString, replaceString, kt);
pBar1.PerformStep(); //进度条
}
}
catch (Exception ex)
{
MessageBox.Show(string.Format("异常:{0}", ex.Message), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
finally
{
this.ExecuteFilterBtn.Enabled = true;
}

MessageBox.Show(string.Format("数据替换完毕,共有{0}行数据被修改!",total),"消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#endregion

以上就是整个大致思路,详情可以参看源代码。

附带一些操作截图,希望大家可以看的更清楚一些。

这个就是被注入的数据,当然实际的会有区别。

编写查找内容,并启用正则匹配功能。

哈哈,数据终于恢复原貌!!
源程序下载地址

更多精彩内容其他人还在看

解析WPF实现音频文件循环顺序播放的解决方法

本篇文章是对WPF实现音频文件循环顺序播放的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解决.net framework 4.0环境下遇到版本不同编译不通过的方法详解

本篇文章是对.net framework 4.0环境下遇到版本不同编译不通过的解决方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

将文件上传、下载(以二进制流保存到数据库)实现代码

将文件以二进制流的格式写入数据库:首先获得文件路径,然后将文件以二进制读出保存在一个二进制数组中具体请祥看本文,希望对你有所帮助
收藏 0 赞 0 分享

点击提交按钮后DropDownList的值变为默认值实现分析

在点击提交按钮后,页面上所有的绑定到数据库的控件值都恢复到默认值,下面与大家分享下DropDownList的值变为默认值
收藏 0 赞 0 分享

ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法

ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

Linkbutton控件在项目中的简单应用

Button控件可分为button控件、LinkButton控件、ImageButton控件三类,而LinkButton控件则在页面上显示为一个超级链接,下面与大家分享下其具体应用
收藏 0 赞 0 分享

Web.config 和 App.config 的区别分析

Web.config 和 App.config 的区别分析,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于.Net中的数字与日期格式化规则助记词的使用详解

本篇文章是对.Net中的数字与日期格式化规则助记词的使用进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解决在Web.config或App.config中添加自定义配置的方法详解

本篇文章是对在Web.config或App.config中添加自定义配置的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入本机影像生成器(Ngen.exe)工具使用方法详解

本篇文章是对本机影像生成器(Ngen.exe)工具使用方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享
查看更多