以前做的是下面这个样子的,采用Winform
最近将它改为WPF的,以后做工具不用winform了....
对流程进行了优化,将中部改为树形结构,可以选择存储过程函数,以前不能选择全部进行新建。
数据库连接选择也改为WPF了
新的主界面
XAML
<Window x:Class="FYJ.Tools.DBTool.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:FYJ.Windows.Controls;assembly=FYJ.Windows.Controls" Title="数据库工具" Height="700" Width="625"> <DockPanel> <StackPanel DockPanel.Dock="Top"> <Menu > <MenuItem Header="文件(_F)"> <MenuItem Header="数据表传输" x:Name="men数据表传输" Click="men数据表传输_Click"></MenuItem> <MenuItem Header="行数浏览" x:Name="menu行数据浏览" Click="men行数据浏览_Click"></MenuItem> <MenuItem Header="Oracle创建用户" x:Name="menuOracle创建用户" Click="menuOracle创建用户_Click"></MenuItem> </MenuItem> <MenuItem Header="帮助(_H)"/> </Menu> <DockPanel Margin="0,10,0,0"> <TextBlock Text="源数据连接"></TextBlock> <Button Content="..." Width="40" DockPanel.Dock="Right" x:Name="btnSrcConnectBrowser" Click="btnSrcConnectBrowser_Click"></Button> <TextBox IsEnabled="False" Margin="20,0,10,0" x:Name="txtSrcConnectionString"></TextBox> </DockPanel> <DockPanel Margin="0,10,0,0"> <TextBlock Text="目标数据连接"></TextBlock> <Button Content="..." Width="40" DockPanel.Dock="Right" x:Name="btnDestConnectBrowser" Click="btnDestConnectBrowser_Click"></Button> <TextBox IsEnabled="False" Margin="10,0,10,0" x:Name="txtDestConnectionString"></TextBox> </DockPanel> <DockPanel Margin="0,10,0,0"> <WrapPanel DockPanel.Dock="Top"> <TextBlock Text="生成数据和删除对象操作只针对源数据连接" VerticalAlignment="Center" Margin="90,0,0,0" Foreground="Red"></TextBlock> </WrapPanel> <c:CustomTreeView Margin="0,10,0,0" x:Name="objectTree" Height="200"></c:CustomTreeView> </DockPanel> <GroupBox Header="生成数据" Margin="0,10,0,0"> <WrapPanel Margin="60,10,0,10"> <TextBlock Text="生成" VerticalAlignment="Center"></TextBlock> <c:TextBoxEx Width="100" Text="1000" Margin="10,0,0,0" VerticalAlignment="Center" x:Name="txtTestLineCount"></c:TextBoxEx> <TextBlock Text="行测试数据" Margin="10,0,0,0" VerticalAlignment="Center"></TextBlock> <Button Content="生成" Width="80" Margin="10,0,0,0" VerticalAlignment="Center" Height="30" x:Name="btnCreateTestDate" Click="btnCreateTestDate_Click"></Button> </WrapPanel> </GroupBox> <GroupBox Header="同步数据" Margin="0,10,0,0"> <WrapPanel Margin="60,10,0,10"> <c:CheckBoxEx Content="创建表" VerticalAlignment="Center" x:Name="isCreateTable"></c:CheckBoxEx> <c:CheckBoxEx Content="创建数据" VerticalAlignment="Center" Margin="10,0,0,0" x:Name="isFillData"></c:CheckBoxEx> <TextBlock Text="前" VerticalAlignment="Center" Margin="20,0,0,0"></TextBlock> <c:TextBoxEx Width="60" Text="1000" Margin="10,0,0,0" VerticalAlignment="Center" x:Name="txtSyncRowCount"></c:TextBoxEx> <TextBlock Text="行" Margin="10,0,0,0" VerticalAlignment="Center"></TextBlock> <TextBlock Text="0表示全部" Margin="10,0,0,0" VerticalAlignment="Center" Foreground="Gray"></TextBlock> <c:CheckBoxEx Content="忽略已存在对象" Margin="10,0,0,0" VerticalAlignment="Center"></c:CheckBoxEx> <Button Content="同步" Width="80" Margin="10,0,0,0" VerticalAlignment="Center" Height="30" x:Name="btnSync" Click="btnSync_Click"></Button> </WrapPanel> </GroupBox> <GroupBox Header="删除对象" Margin="0,10,0,0"> <WrapPanel Margin="60,10,0,10"> <Button Content="删除对象" Height="30" Width="80" Margin="10,0,0,0" VerticalAlignment="Center" x:Name="btnDeleteObject" Click="btnDeleteObject_Click"></Button> <Button Content="清空表数据" Height="30" Width="80" Margin="10,0,0,0" VerticalAlignment="Center" x:Name="btnClearData" Click="btnClearData_Click"></Button> </WrapPanel> </GroupBox> </StackPanel> <DockPanel DockPanel.Dock="Bottom" Margin="0,10,0,0" Height="30"> <c:ProgressBarEx Width="160" Height="24" DockPanel.Dock="Right" Margin="10,0,0,0" x:Name="toolStripProgressBar1"></c:ProgressBarEx> <c:TextBlockEx Text="就绪" x:Name="txtStates"></c:TextBlockEx> </DockPanel> <c:LogListBox x:Name="logList1"></c:LogListBox> </DockPanel> <Window.TaskbarItemInfo> <TaskbarItemInfo x:Name="taskbarItemInfo1"> </TaskbarItemInfo> </Window.TaskbarItemInfo> </Window>
后台比较复杂
using FYJ.Data; using FYJ.Data.Builder; using FYJ.DBConnect; using FYJ.Windows.Controls; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Data; using System.Linq; using System.Text; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Shell; namespace FYJ.Tools.DBTool { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private FYJ.Data.IDbHelper srcDbHelper; private FYJ.Data.IDbHelper destDbHelper; void helper_AddEvent(int completed, int count) { if (this.toolStripProgressBar1.Value + 1 < this.toolStripProgressBar1.Maximum) { this.toolStripProgressBar1.Value = this.toolStripProgressBar1.Value + 1; this.taskbarItemInfo1.Dispatcher.Invoke((Action)(() => { this.taskbarItemInfo1.ProgressState = TaskbarItemProgressState.Normal; this.taskbarItemInfo1.ProgressValue = completed / (double)count; })); //this.toolStripStatusLabel1.Text = String.Format("{0:p}", toolStripProgressBar1.Value / (double)toolStripProgressBar1.Maximum); } this.txtStates.Text = "当前进度:" + completed + "/" + count + " ,总进度:" + this.toolStripProgressBar1.Value + "/" + this.toolStripProgressBar1.Maximum; } private void btnSrcConnectBrowser_Click(object sender, RoutedEventArgs e) { ConnWindow src = new ConnWindow(); src.Completed += src_Completed; src.ShowDialog(); } private void btnDestConnectBrowser_Click(object sender, RoutedEventArgs e) { ConnWindow dest = new ConnWindow(); dest.Completed += dest_Completed; dest.ShowDialog(); } void dest_Completed(Data.IDbHelper dbHelper, string userName) { destDbHelper = dbHelper; this.txtDestConnectionString.Text = dbHelper.ConnectionString; } private void SetSqlServerTree(FYJ.Data.IDbHelper dbHelper) { ObservableCollection<TreeModel> rootTreeList = new ObservableCollection<TreeModel>(); TreeModel tableTree = new TreeModel(); tableTree.Id = "U"; tableTree.Name = "表"; tableTree.Icon = "/FYJ.Windows.Controls;component/Images/16_16/folder_go.png"; tableTree.IsExpanded = false; TreeModel viewTree = new TreeModel(); viewTree.Id = "V"; viewTree.Name = "视图"; viewTree.IsExpanded = false; TreeModel programTree = new TreeModel(); programTree.Id = "program"; programTree.Name = "可编程性"; programTree.IsExpanded = false; TreeModel procTree = new TreeModel(); procTree.Id = "P"; procTree.Name = "存储过程"; procTree.IsExpanded = false; TreeModel funcTree = new TreeModel(); funcTree.Id = "func"; funcTree.Name = "函数"; funcTree.IsExpanded = false; TreeModel fnTree = new TreeModel(); fnTree.Id = "FN"; fnTree.Name = "标量值函数"; fnTree.IsExpanded = false; TreeModel ffTree = new TreeModel(); ffTree.Id = "FF"; ffTree.Name = "表值函数"; ffTree.IsExpanded = false; DataTable sqlserverObjs = dbHelper.GetDataTable( @" SELECT t1.name,t1.modify_date,ltrim(rtrim(t1.type)) as [type] FROM sys.all_objects t1 WHERE SCHEMA_NAME(schema_id)=N'dbo' and type in('U','V','FN','TF','IF','P') and name<>'fn_diagramobjects' and name<>'sysdiagrams' and name not like 'sp_%' order by create_date "); foreach (DataRow dr in sqlserverObjs.Select("type='U'").OrderBy(c => c["name"])) { TreeModel m = new TreeModel(); m.Id = dr["name"].ToString(); m.Name = dr["name"].ToString(); m.IsExpanded = false; m.Parent = tableTree; m.Tag = dr["type"]; tableTree.Children.Add(m); } foreach (DataRow dr in sqlserverObjs.Select("type='V'").OrderBy(c => c["name"])) { TreeModel m = new TreeModel(); m.Id = dr["name"].ToString(); m.Name = dr["name"].ToString(); m.IsExpanded = false; m.Parent = viewTree; m.Tag = dr["type"]; viewTree.Children.Add(m); } foreach (DataRow dr in sqlserverObjs.Select("type='FN'").OrderBy(c => c["name"])) { TreeModel m = new TreeModel(); m.Id = dr["name"].ToString(); m.Name = dr["name"].ToString(); m.IsExpanded = false; m.Parent = fnTree; m.Tag = dr["type"]; fnTree.Children.Add(m); } foreach (DataRow dr in sqlserverObjs.Select("type in ('IF','TF')").OrderBy(c => c["name"])) { TreeModel m = new TreeModel(); m.Id = dr["name"].ToString(); m.Name = dr["name"].ToString(); m.IsExpanded = false; m.Parent = ffTree; m.Tag = dr["type"]; ffTree.Children.Add(m); } foreach (DataRow dr in sqlserverObjs.Select("type='P'").OrderBy(c => c["name"])) { TreeModel m = new TreeModel(); m.Id = dr["name"].ToString(); m.Name = dr["name"].ToString(); m.IsExpanded = false; m.Parent = procTree; m.Tag = dr["type"]; procTree.Children.Add(m); } procTree.Parent = programTree; programTree.Children.Add(procTree); ffTree.Parent = funcTree; fnTree.Parent = funcTree; funcTree.Children.Add(ffTree); funcTree.Children.Add(fnTree); funcTree.Parent = programTree; programTree.Children.Add(funcTree); rootTreeList.Add(tableTree); rootTreeList.Add(viewTree); rootTreeList.Add(programTree); this.objectTree.ItemsSourceData = rootTreeList; } private void SetTableTree(List<string> tables) { tables.Sort(); ObservableCollection<TreeModel> rootTreeList = new ObservableCollection<TreeModel>(); TreeModel tableTree = new TreeModel(); tableTree.Id = "U"; tableTree.Name = "表"; tableTree.Icon = "/FYJ.Windows.Controls;component/Images/16_16/folder_go.png"; tableTree.IsExpanded = false; foreach (string s in tables) { TreeModel m = new TreeModel(); m.Id = s; m.Name = s; m.IsExpanded = false; m.Parent = tableTree; tableTree.Children.Add(m); } rootTreeList.Add(tableTree); this.objectTree.ItemsSourceData = rootTreeList; } private DataTable oracleSrcDataTable; void src_Completed(FYJ.Data.IDbHelper dbHelper, string userName) { srcDbHelper = dbHelper; this.txtSrcConnectionString.Text = dbHelper.ConnectionString; FYJ.Data.DbHelperType dbHelperType = dbHelper.DbHelperTypeEnum; switch (dbHelperType) { case FYJ.Data.DbHelperType.SqlServer: SetSqlServerTree(dbHelper); break; case FYJ.Data.DbHelperType.MySql: SetTableTree(srcDbHelper.GetTables()); break; case FYJ.Data.DbHelperType.Oracle: List<string> tables = new List<string>(); oracleSrcDataTable = srcDbHelper.GetDataTable("select table_name,num_rows from dba_tables where table_name in(select table_name from all_tab_comments where owner='" + userName.ToUpper() + "' and table_type='TABLE')"); foreach (DataRow dr in oracleSrcDataTable.Rows) { if (!tables.Contains(dr["TABLE_NAME"].ToString())) { if (dr["TABLE_NAME"].ToString() != "sysdiagrams") { tables.Add(dr["TABLE_NAME"].ToString()); } } } SetTableTree(tables); break; default: break; } } private List<DataCommandEx> GetExistsObject(IDbHelper db) { string sql = @" SELECT t1.name,t1.modify_date FROM sys.all_objects t1 WHERE SCHEMA_NAME(schema_id)=N'dbo' and type in('U','V','FN','TF','IF','P') and name<>'fn_diagramobjects' and name<>'sysdiagrams' and name not like 'sp_%' order by create_date "; List<DataCommandEx> list = new List<DataCommandEx>(); DataTable dt = db.GetDataTable(sql); foreach (DataRow dr in dt.Rows) { if (!list.Exists(c => c.Name == dr["name"].ToString())) { DataCommandEx ex = new DataCommandEx(); ex.Name = dr["name"].ToString(); ex.ModifyDate = Convert.ToDateTime(dr["modify_date"]); list.Add(ex); } } return list; } private bool IsExists(List<DataCommandEx> list, string name) { foreach (DataCommandEx ex in list) { if (ex.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase)) { return true; } } return false; } private void CreatePVFN(string type, IEnumerable<TreeModel> selectedNodes) { if (selectedNodes == null || selectedNodes.Count() == 0) { return; } if(type=="V") { this.txtStates.Text = "正在创建视图..."; this.logList1.AddItem("创建视图..."); } if (type == "P") { this.txtStates.Text = "正在创建存储过程..."; this.logList1.AddItem("创建存储过程..."); } if (type == "FN" || type == "IF" || type == "TF") { this.txtStates.Text = "正在创建函数..."; this.logList1.AddItem("创建函数..."); } string procSrcSql = @" SELECT t1.name,ltrim(rtrim(t1.[type])) as objtype,t1.create_date,t2.id,t2.text,t1.modify_date FROM sys.all_objects t1 INNER JOIN syscomments t2 on t1.object_id=t2.id WHERE SCHEMA_NAME(schema_id)=N'dbo' and type=@type and name<>'fn_diagramobjects' and name<>'sysdiagrams' and name not like 'sp_%' order by t2.id "; DataTable pvSrcDataTable = null; if (String.IsNullOrEmpty(type)) { procSrcSql = procSrcSql.Replace("=@type", " in('U','V','FN','TF','IF','P')"); pvSrcDataTable = srcDbHelper.GetDataTable(procSrcSql); } else { pvSrcDataTable = srcDbHelper.GetDataTable(procSrcSql, srcDbHelper.CreateParameter("@type", type)); } List<DataCommandEx> list = new List<DataCommandEx>(); //执行创建 foreach (DataRow dr in pvSrcDataTable.Rows) { if (!IsExists(list, dr["name"].ToString())) { string text = ""; DataRow[] drs = pvSrcDataTable.Select("name='" + dr["name"] + "'"); foreach (DataRow rr in drs) { text += rr["text"].ToString(); } DataCommandEx ex = new DataCommandEx(); ex.CreateDate = Convert.ToDateTime(dr["create_date"]); ex.ID = Convert.ToInt32(dr["id"]); ex.Name = dr["name"].ToString(); ex.ModifyDate = Convert.ToDateTime(dr["modify_date"]); ex.SQL = text; list.Add(ex); } } MyComparer compara = new MyComparer(); for (int i = 0; i < list.Count; i++) { for (int j = i; j < list.Count; j++) { if (compara.Compare(list[i], list[j]) > 0) { DataCommandEx temp = list[i]; list[i] = list[j]; list[j] = temp; } } } List<DataCommandEx> destObjList = GetExistsObject(destDbHelper); foreach (DataCommandEx ex in list) { if (selectedNodes.Count(c => c.Name == ex.Name) > 0) { DataCommandEx old = destObjList.Where(c => c.Name == ex.Name).FirstOrDefault(); if (old != null) { if (ex.ModifyDate > old.ModifyDate) { ex.SQL = ex.SQL.Replace("CREATE", "ALTER"); } else { continue; } } if (!String.IsNullOrEmpty(ex.SQL)) { try { destDbHelper.ExecuteSql(ex.SQL); } catch (Exception e) { logList1.AddErrorItem(e.Message); continue; } } } } } private void btnSync_Click(object sender, RoutedEventArgs e) { if (srcDbHelper == null || destDbHelper == null) { MessageBox.Show("请先设置数据源"); return; } if(objectTree.CheckedItemsIgnoreRelation().Count==0) { MessageBox.Show("请选择同步的对象"); return; } this.logList1.Items.Clear(); ThreadPool.QueueUserWorkItem((o) => { this.toolStripProgressBar1.Value = 0; this.logList1.AddItem("开始处理..."); var list = this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "U"); if (list.Count() > 0) { int totalCount = GetTotalLineCount(); this.toolStripProgressBar1.Maximum = totalCount; foreach (TreeModel node in list) { try { BuilderOption option = new BuilderOption(); option.handler = helper_AddEvent; option.IsCreateTable = isCreateTable.IsChecked.Value; option.IsCreateDate = isFillData.IsChecked.Value; int top = 0; Int32.TryParse(this.txtSyncRowCount.Text, out top); DataTable dt = srcDbHelper.GetDataTable(node.Name, top); if (srcDbHelper.DbHelperTypeEnum == FYJ.Data.DbHelperType.SqlServer) { FYJ.Data.DbHelperSQL db = (FYJ.Data.DbHelperSQL)srcDbHelper; option.ColumnsInfoList = db.GetDatableSchema(node.Name); option.IsCLUSTERED = true; } dt.TableName = node.Name; FYJ.Data.Builder.IDataBuilder builder = FYJ.Data.Builder.DataBuilderFactory.instance.GetIDataBuilderInstance(destDbHelper); builder.CreateTable(dt, destDbHelper, option); this.toolStripProgressBar1.Value = this.toolStripProgressBar1.Value + 1; //this.listBox1.Items.Add(DateTime.Now + " 表" + s + "传输完毕"); } catch (Exception ex) { this.toolStripProgressBar1.Value = Math.Min(this.toolStripProgressBar1.Value + 1, this.toolStripProgressBar1.Maximum); this.logList1.AddErrorItem("表" + node.Name + "传输错误:" + ex.Message); continue; } } } try { //如果原数据库和目标数据库都是sqlserver if (srcDbHelper.DbHelperTypeEnum == FYJ.Data.DbHelperType.SqlServer && destDbHelper.DbHelperTypeEnum == FYJ.Data.DbHelperType.SqlServer) { //'U','V','FN','TF','IF','P' //必须注意先后顺序 CreatePVFN("FN", this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "FN")); CreatePVFN("V", this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "V")); CreatePVFN("IF", this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "IF")); CreatePVFN("TF", this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "TF")); CreatePVFN("P", this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "P")); } } catch (Exception ex) { logList1.AddErrorItem(ex.Message); } this.logList1.AddItem("处理完成"); this.toolStripProgressBar1.Value = this.toolStripProgressBar1.Maximum; this.txtStates.Text = "完成"; }); } //获取要同步的表总条数 private int GetTotalLineCount() { int count = 0; if (srcDbHelper.DbHelperTypeEnum == FYJ.Data.DbHelperType.SqlServer) { string sql = @"SELECT a.name, b.rows FROM sysobjects AS a INNER JOIN sysindexes AS b ON a.id = b.id WHERE (a.type = 'u') AND (b.indid IN (0, 1)) ORDER BY b.rows DESC ; "; DataTable dt = srcDbHelper.GetDataTable(sql); foreach (DataRow dr in dt.Rows) { count += Convert.ToInt32(dr["rows"]); } } if (srcDbHelper.DbHelperTypeEnum == FYJ.Data.DbHelperType.MySql) { string sql = "SHOW TABLE STATUS "; DataTable dt = srcDbHelper.GetDataTable(sql); foreach (DataRow dr in dt.Rows) { count += Convert.ToInt32(dr["Rows"]); } } if (srcDbHelper.DbHelperTypeEnum == FYJ.Data.DbHelperType.Oracle) { if (oracleSrcDataTable == null) { return 0; } var list = this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "U"); for (int i = 0; i < oracleSrcDataTable.Rows.Count; i++) { string tablename = oracleSrcDataTable.Rows[i]["TABLE_NAME"].ToString(); if (list.Where(c => c.Name == tablename).Count() > 0) { int temp = Convert.ToInt32(this.txtSyncRowCount.Text.Trim()); if (temp == 0) { count += Convert.ToInt32(oracleSrcDataTable.Rows[i][1]); } else { count += Math.Min(temp, Convert.ToInt32(oracleSrcDataTable.Rows[i][1])); } } } } return count; } #region 生成测试数据 private void btnCreateTestDate_Click(object sender, RoutedEventArgs e) { if (srcDbHelper == null) { MessageBox.Show("请先设置数据源"); return; } var list = this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "U"); if (list.Count() == 0) { MessageBox.Show("请选择要生成的表!", "错误"); return; } if (MessageBox.Show("确定要生成吗?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.No) { return; } this.logList1.Items.Clear(); ThreadPool.QueueUserWorkItem(h => { this.toolStripProgressBar1.Maximum = list.Count(); this.toolStripProgressBar1.Value = 0; this.logList1.AddItem("开始处理..."); foreach (TreeModel node in list) { string s = node.Name; //this.listBox1.Items.Add(DateTime.Now+" 开始传输表"+s+"..."); try { int top = 0; Int32.TryParse(this.txtTestLineCount.Text, out top); DataTable dt = srcDbHelper.GetDataTable(s, top); dt.TableName = s; int count = Convert.ToInt32(this.txtTestLineCount.Text) - dt.Rows.Count; //需要生成的数据行数减去已有的行数 for (int i = 0; i < count; i++) { DataRow dr = dt.NewRow(); foreach (DataColumn column in dt.Columns) { if (column.AllowDBNull == false && column.AutoIncrement == false) //只插入必填并且不是自动递增的字段 { if (column.DataType == typeof(int) || column.DataType == typeof(long) || column.DataType == typeof(Int16)) { dr[column.ColumnName] = new Random().Next(10000); } if (column.DataType == typeof(string)) { String str = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; Random ran = new Random((int)(DateTime.Now.Ticks)); TimeSpan ts = new TimeSpan(1); Thread.Sleep(ts); String result = ""; for (int k = 0; k < column.MaxLength; k++) { result += (str.Substring(ran.Next(str.Length), 1)); } dr[column.ColumnName] = result; } if (column.DataType == typeof(double) || column.DataType == typeof(Single) || column.DataType == typeof(float) || column.DataType == typeof(decimal)) { dr[column.ColumnName] = new Random().NextDouble(); } if (column.DataType == typeof(bool)) { int temp = new Random().Next(2); dr[column.ColumnName] = (temp == 0 ? false : true); } if (column.DataType == typeof(DateTime)) { dr[column.ColumnName] = DateTime.Now; } if (column.DataType == typeof(byte)) { byte[] buffer = new byte[1]; new Random().NextBytes(buffer); dr[column.ColumnName] = buffer[0]; } if (column.DataType == typeof(byte[])) { byte[] buffer = new byte[column.MaxLength]; new Random().NextBytes(buffer); dr[column.ColumnName] = buffer; } } } dt.Rows.Add(dr); } // srcDbHelper.Update(dt);---------------------- this.toolStripProgressBar1.Value = this.toolStripProgressBar1.Value + 1; //this.listBox1.Items.Add(DateTime.Now + " 表" + s + "测试数据生成完毕"); this.txtStates.Text = String.Format("{0:p}", toolStripProgressBar1.Value / (double)toolStripProgressBar1.Maximum); } catch (Exception ex) { this.toolStripProgressBar1.Value = this.toolStripProgressBar1.Value + 1; this.logList1.AddErrorItem("表" + s + "生成测试数据错误:" + ex.Message); continue; } } this.logList1.AddItem("处理完成"); this.txtStates.Text = "完成"; }); } #endregion #region 删除数据 private void btnClearData_Click(object sender, RoutedEventArgs e) { if (srcDbHelper == null) { MessageBox.Show("请先设置数据源"); return; } var list = this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "U"); if (list.Count() == 0) { MessageBox.Show("请选择要清空的表!", "错误"); return; } if (MessageBox.Show("确定要清空吗?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.No) { return; } this.logList1.Items.Clear(); ThreadPool.QueueUserWorkItem(h => { this.logList1.AddItem("清空表数据..."); StringBuilder sb = new StringBuilder(); foreach (TreeModel node in list) { sb.Append("truncate table " + node.Name + ";" + Environment.NewLine); } srcDbHelper.ExecuteSql(sb.ToString()); this.logList1.AddItem("处理完成"); this.txtStates.Text = "完成"; }); } #endregion #region 删除对象 private void btnDeleteObject_Click(object sender, RoutedEventArgs e) { if (srcDbHelper == null) { MessageBox.Show("请先设置数据源"); return; } if (this.objectTree.CheckedItemsIgnoreRelation().Count() == 0) { MessageBox.Show("请选择要删除的对象!", "错误"); return; } if (MessageBox.Show("确定要删除吗?", "提示", MessageBoxButton.YesNo) == MessageBoxResult.No) { return; } StringBuilder sb = new StringBuilder(); var list = this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "U"); foreach (TreeModel node in list) { sb.AppendLine("drop table " + node.Name + ";"); } list = this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "P"); foreach (TreeModel node in list) { sb.AppendLine("drop PROCEDURE " + node.Name + ";"); } list = this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "V"); foreach (TreeModel node in list) { sb.AppendLine("drop view " + node.Name + ";"); } list = this.objectTree.CheckedItemsIgnoreRelation().Where(c => (string)c.Tag == "FN" || (string)c.Tag == "IF" || (string)c.Tag == "TF"); foreach (TreeModel node in list) { sb.AppendLine("drop function " + node.Name + ";"); } string sqltext = sb.ToString(); if (sqltext != "") { srcDbHelper.ExecuteSql(sqltext); } var selected = objectTree.CheckedItemsIgnoreRelation(); foreach (TreeModel node in selected) { if (node.Parent != null) { node.Parent.Children.Remove(node); node.SetParentChecked(); } } MessageBox.Show("操作成功"); } #endregion private void men数据表传输_Click(object sender, RoutedEventArgs e) { ColumnsTransWindow window = new ColumnsTransWindow(); window.Show(); } private void men行数据浏览_Click(object sender, RoutedEventArgs e) { TableViewWindow window = new TableViewWindow(); window.Show(); } private void menuOracle创建用户_Click(object sender, RoutedEventArgs e) { OracleCreateUserSqlWindow window = new OracleCreateUserSqlWindow(); window.Show(); } } }