/// <summary> /// 移植数据库脚本 /// </summary> public static string run() { string result = ""; tableInfos = UtilSqlserver.TableinfoList(); //生成数据库主体部分 result = CreateDbDefine(); return(result); }
/// <summary> /// 点选数据库列表框默认从数据库读一次所有数据库 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cbDatabases_MouseClick(object sender, MouseEventArgs e) { //if (cbDatabases.Items.Count <= 0) //{ cbDatabases.Items.Clear(); List <string> databases; databases = (cbDbType.SelectedIndex == 0) ? UtilSqlserver.AllDatabaseNames() : UtilMysql.AllDatabaseNames(); foreach (string database in databases) { this.cbDatabases.Items.Add(database); } //this.cbDatabases.SelectedIndex = 0; //} }
/// <summary> /// 查看指定表列名,如果没有指定表,默认指定第一张表 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnColumns_Click(object sender, EventArgs e) { btnColumns.Enabled = false; this.listResult.Clear(); string tablename; if (cbTables.Items.Count <= 0) { Dictionary <string, string> tables; tables = (cbDbType.SelectedIndex == 0) ? UtilSqlserver.TableList() : UtilMysql.TableList(); tablename = tables.Values.First(); foreach (string cur_tablename in tables.Values) { this.cbTables.Items.Add(cur_tablename); } this.cbTables.SelectedIndex = 0; } else { tablename = (string)cbTables.SelectedItem; } Dictionary <string, Dictionary <string, string> > columnInfos; columnInfos = (cbDbType.SelectedIndex == 0) ? UtilSqlserver.FieldInfoList(tablename) : UtilMysql.FieldInfoList(tablename); this.listResult.AppendText("显示指定表的信息:" + tablename + "\r\n"); string columnResult, comment; foreach (Dictionary <string, string> columnInfo in columnInfos.Values) { comment = ""; if (columnInfo.ContainsKey("Comment")) { comment = columnInfo["Comment"]; string[] commentArr = comment.Split(new char[2] { '\r', '\n' }); comment = commentArr[0]; comment = "|备注:" + comment; } columnResult = "列名:" + columnInfo["Field"] + comment + "|类型:" + columnInfo["Type"] + "|是否允许为空:" + columnInfo["Null"] + "\r\n"; this.listResult.AppendText(columnResult); } btnColumns.Enabled = true; }
/// <summary> /// 查询所有表信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnViewDbInfo_Click(object sender, EventArgs e) { btnViewDbInfo.Enabled = false; Dictionary <string, string> tables; tables = (cbDbType.SelectedIndex == 0)?UtilSqlserver.TableList():UtilMysql.TableList(); string tablenames = ""; this.listResult.Clear(); foreach (string tablename in tables.Values) { tablenames += tablename + "\r\n"; } this.listResult.AppendText(tablenames); Console.WriteLine(tablenames); btnViewDbInfo.Enabled = true; }
/// <summary> /// 点选表列表框选择默认从数据库读一次所有表 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cbTables_MouseClick(object sender, MouseEventArgs e) { //if (cbTables.Items.Count <= 0) //{ cbTables.Items.Clear(); Dictionary <string, string> tables; tables = (cbDbType.SelectedIndex == 0) ? UtilSqlserver.TableList() : UtilMysql.TableList(); foreach (string cur_tablename in tables.Values) { this.cbTables.Items.Add(cur_tablename); } //this.cbTables.SelectedIndex = 0; btnColumns.Enabled = true; //} }
/// <summary> /// 选择不同的数据库 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cbDatabases_SelectionChangeCommitted(object sender, EventArgs e) { if (isDbChanged) { if (cbDbType.SelectedIndex == (int)DbType.SqlServer) { UtilSqlserver.SetDatabase(cbDatabases.SelectedItem.ToString()); } else { UtilMysql.SetDatabase(cbDatabases.SelectedItem.ToString()); } isDbChanged = false; listResult.Clear(); cbTables.Items.Clear(); cbTables.Text = ""; btnColumns.Enabled = false; } }
/// <summary> /// 查看所有的表信息包括表注释 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnTableComment_Click(object sender, EventArgs e) { btnTableComment.Enabled = false; Dictionary <string, Dictionary <string, string> > tableInfos; tableInfos = (cbDbType.SelectedIndex == 0) ? UtilSqlserver.TableinfoList():UtilMysql.TableinfoList(); string tableComment, tablenames = ""; this.listResult.Clear(); foreach (Dictionary <string, string> tablename in tableInfos.Values) { tableComment = tablename["Comment"]; string[] tableCommentArr = tableComment.Split(new char[2] { '\r', '\n' }); tableComment = tableCommentArr[0]; tableComment = tablename["Name"] + ":" + tableComment + "\r\n"; tablenames += tableComment; } this.listResult.AppendText(tablenames); Console.WriteLine(tablenames); btnTableComment.Enabled = true; }
/// <summary> /// 选择不同数据库类型 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void cbDbType_SelectionChangeCommitted(object sender, EventArgs e) { if (isDbTypeChanged) { cbDatabases.Text = (cbDbType.SelectedIndex == (int)DbType.SqlServer)?UtilSqlserver.Database_Name:UtilMysql.Database_Name; if (cbDbType.SelectedIndex == (int)DbType.SqlServer) { UtilSqlserver.SetDatabase(UtilSqlserver.Database_Name); //btnMigrantScript.Enabled = false; btnMigrantScript.Text = "移植数据库脚本[SQLServer->Mysql]"; btnDropAllTables.Enabled = true; } else { UtilMysql.SetDatabase(UtilMysql.Database_Name); // btnMigrantScript.Enabled = true; btnMigrantScript.Text = "移植数据库脚本[Mysql->SQLServer]"; btnDropAllTables.Enabled = false; } cbDatabases.Text = ""; isDbTypeChanged = false; listResult.Clear(); } }
/// <summary> /// 初始化工作 /// </summary> protected void Init() { UtilSqlserver.Database_Name = Database_Name; if (string.IsNullOrEmpty(App_Dir)) { App_Dir = Directory.GetCurrentDirectory(); App_Dir = App_Dir + Path.DirectorySeparatorChar + "Model" + Path.DirectorySeparatorChar; } if (TableInfoList == null) { TableInfoList = UtilSqlserver.TableinfoList(); TableList = UtilSqlserver.TableList().Keys.ToList(); } if (FieldInfos == null) { FieldInfos = new Dictionary <string, Dictionary <string, Dictionary <string, string> > >(); foreach (string Table_Name in TableList) { FieldInfos.Add(Table_Name, UtilSqlserver.FieldInfoList(Table_Name)); } } if (OneHasManyDefine == null) { OneHasManyDefine = new Dictionary <string, List <string> >(); string Column_Name, Relation_ClassName; List <string> lRelation_TableName = null; foreach (string Table_Name in TableList) { Dictionary <string, Dictionary <string, string> > FieldInfo = FieldInfos[Table_Name]; foreach (KeyValuePair <String, Dictionary <string, string> > entry in FieldInfo) { Column_Name = entry.Key; if (Column_Name.Contains("_ID")) { Relation_ClassName = Column_Name.Replace("_ID", ""); if (TableList.Contains(Relation_ClassName)) { if (lRelation_TableName == null) { lRelation_TableName = new List <string>(); } if (OneHasManyDefine.Keys.Contains(Relation_ClassName)) { lRelation_TableName = OneHasManyDefine[Relation_ClassName]; } else { lRelation_TableName = new List <string>(); OneHasManyDefine[Relation_ClassName] = lRelation_TableName; } if (!lRelation_TableName.Contains(Table_Name)) { lRelation_TableName.Add(Table_Name); } } } } } } Same_Column_Names = UtilSqlserver.Same_Column_Name_In_TableName(); //UtilObjectDump.WriteLine(Same_Column_Names); }
/// <summary> /// 点击生成数据库说明Excel /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnExportexcel_Click(object sender, EventArgs e) { btnExportexcel.Enabled = false; UtilExcelCom.Release(); ExcelBE be = null; Dictionary <string, Dictionary <string, string> > tableInfos; tableInfos = (cbDbType.SelectedIndex == 0) ? UtilSqlserver.TableinfoList() : UtilMysql.TableinfoList(); int rowHeight = 25; //数据库表说明 UtilExcelCom.Current().SetSheet("Summary"); be = new ExcelBE(1, 1, "编号", "A1", "A1", "GRAYDARK", false, 10, 13.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 2, "表名称", "B1", "B1", "GRAYDARK", false, 10, 32.63, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 3, "说明", "C1", "C1", "GRAYDARK", false, 10, 24.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); int rowno = 2; int index = 1; int line = 1; string idIndex = ""; string tablet_name = "", table_comment = ""; string[] commentArr; ArrayList commentList; foreach (Dictionary <string, string> tablename in tableInfos.Values) { table_comment = ""; idIndex = index.ToString(); if (idIndex.Length < 2) { idIndex = "0" + idIndex; } be = new ExcelBE(rowno, 1, "A" + idIndex, "A" + rowno, "A" + rowno, null, false, 10, 13.50, rowHeight, 2, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); tablet_name = tablename["Name"]; be = new ExcelBE(rowno, 2, tablet_name, "B" + rowno, "B" + rowno, null, false, 10, 32.63, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); //添加链接 UtilExcelCom.Current().AddLink(be, tablet_name, "列名"); table_comment = tablename["Comment"]; table_comment = table_comment.Trim(); line = 1; rowHeight = 25; if (UtilString.Contains(table_comment, "\r", "\n")) { rowHeight = 16; commentArr = table_comment.Split(new char[2] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); rowHeight = rowHeight * line; commentList = new ArrayList(commentArr); table_comment = ""; foreach (var commenti in commentList) { table_comment += commenti + "\r\n"; line += 1; } line -= 1; table_comment = table_comment.Substring(0, table_comment.Length - 2); rowHeight = rowHeight * line + 8; } be = new ExcelBE(rowno, 3, table_comment, "C" + rowno, "C" + rowno, null, false, 10, 24.50, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); rowno++; index++; } //数据库各表详细说明 Dictionary <string, Dictionary <string, string> > columnInfos; string comment = "", dicComment = ""; foreach (Dictionary <string, string> tablename in tableInfos.Values) { rowHeight = 16; columnInfos = (cbDbType.SelectedIndex == 0) ? UtilSqlserver.FieldInfoList(tablename["Name"]) : UtilMysql.FieldInfoList(tablename["Name"]); UtilExcelCom.Current().AddSheet(tablename["Name"]); be = new ExcelBE(1, 1, "列名", "A1", "A1", "GRAYDARK", false, 10, 36.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 2, "数据类型", "B1", "B1", "GRAYDARK", false, 10, 24.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 3, "长度", "C1", "C1", "GRAYDARK", false, 10, 24.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 4, "允许NULL", "D1", "D1", "GRAYDARK", false, 10, 24.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 5, "键值", "E1", "E1", "GRAYDARK", false, 10, 24.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(1, 6, "说明", "F1", "F1", "GRAYDARK", false, 10, 39, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); bool isDicComment = false; foreach (Dictionary <string, string> columnInfo in columnInfos.Values) { if (columnInfo.Keys.Contains("Comment")) { comment = columnInfo["Comment"]; } else { comment = ""; } if (UtilString.Contains(comment, "\r", "\n")) { if (columnInfo["Type"].Equals("char")) { isDicComment = true; break; } } } if (isDicComment) { be = new ExcelBE(1, 7, "数据字典定义", "G1", "G1", "GRAYDARK", false, 10, 48.50, rowHeight, 2, null, "Century Gothic", 10, true, null); UtilExcelCom.Current().InsertData(be); } int tablerowno = 2; bool isColumnDicComment = false; foreach (Dictionary <string, string> columnInfo in columnInfos.Values) { rowHeight = 16; if (columnInfo.Keys.Contains("Comment")) { comment = columnInfo["Comment"]; } else { comment = ""; } isColumnDicComment = false; dicComment = ""; line = 1; comment = comment.Trim(); if (isDicComment) { if (UtilString.Contains(comment, "\r", "\n")) { commentArr = comment.Split(new char[2] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); comment = commentArr[0]; commentList = new ArrayList(commentArr); commentList.Remove(0); foreach (var commenti in commentList) { dicComment += commenti + "\r\n"; line += 1; } line -= 1; dicComment = dicComment.Substring(0, dicComment.Length - 2); rowHeight = rowHeight * line + 8; if (columnInfo["Type"].Equals("char")) { isColumnDicComment = true; } else { comment = dicComment; } } else { comment = comment.Trim(); if (UtilString.Contains(comment, "\r", "\n")) { commentArr = comment.Split(new char[2] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); commentList = new ArrayList(commentArr); comment = ""; foreach (var commenti in commentList) { comment += commenti + "\r\n"; line += 1; } line -= 1; comment = comment.Substring(0, comment.Length - 2); rowHeight = rowHeight * line + 8; } } } else { comment = comment.Trim(); if (UtilString.Contains(comment, "\r", "\n")) { commentArr = comment.Split(new char[2] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries); commentList = new ArrayList(commentArr); comment = ""; foreach (var commenti in commentList) { comment += commenti + "\r\n"; line += 1; } line -= 1; comment = comment.Substring(0, comment.Length - 2); rowHeight = rowHeight * line + 8; } } be = new ExcelBE(tablerowno, 1, columnInfo["Field"], "A" + tablerowno, "A" + tablerowno, null, false, 10, 18, rowHeight, 2, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); string Type_Only = ""; if (cbDbType.SelectedIndex == 0) { Type_Only = columnInfo["Type"]; } else { Type_Only = columnInfo["Type_Only"]; } be = new ExcelBE(tablerowno, 2, Type_Only, "B" + tablerowno, "B" + tablerowno, null, false, 10, 15, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(tablerowno, 3, columnInfo["Length"], "C" + tablerowno, "C" + tablerowno, null, false, 10, 9, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(tablerowno, 4, columnInfo["Null"], "D" + tablerowno, "D" + tablerowno, null, false, 10, 9, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(tablerowno, 5, columnInfo["Fkpk"], "E" + tablerowno, "E" + tablerowno, null, false, 10, 9, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); be = new ExcelBE(tablerowno, 6, comment, "F" + tablerowno, "F" + tablerowno, null, false, 10, 39, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); if (isDicComment) { if (!isColumnDicComment) { dicComment = ""; } be = new ExcelBE(tablerowno, 7, dicComment, "G" + tablerowno, "G" + tablerowno, null, false, 10, 45, rowHeight, 1, null, "Century Gothic", 10, false, null); UtilExcelCom.Current().InsertData(be); } tablerowno++; } } UtilExcelCom.Current().SetActivateSheet(1); //显示Excel UtilExcelCom.Current().DoExport(); string sitename = ConfigurationManager.AppSettings["SiteName"]; UtilExcelCom.Current().Save(Path.Combine(System.Environment.CurrentDirectory, sitename + "数据模型.xlsx")); btnExportexcel.Enabled = true; }
/// <summary> /// 生成删除数据库所有表的脚本 /// </summary> /// <returns></returns> public static string DropAllTables() { tableInfos = UtilSqlserver.TableinfoList(); string result = "/****** 删除数据库所有表的脚本 Script Date:" + DateTime.Now + " ******/\r\n"; string tablename; string sql_template, sql_refer_tempalte, sql_df_template; Dictionary <string, Dictionary <string, string> > columnInfos; sql_template = @" /****** Object: Table [dbo].[{0}] Script Date: 08/03/2013 21:46:21 ******/ IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[{0}]') AND type in (N'U')) DROP TABLE [dbo].[{0}] GO "; sql_refer_tempalte = @" IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[{0}]') AND parent_object_id = OBJECT_ID(N'[dbo].[{1}]')) ALTER TABLE [dbo].[{1}] DROP CONSTRAINT [{0}] GO "; sql_df_template = @" IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[DF_{0}_{1}]') AND type = 'D') BEGIN ALTER TABLE [dbo].[{0}] DROP CONSTRAINT [DF_{0}_{1}] END GO "; Dictionary <string, Dictionary <string, string> > table_fk = UtilSqlserver.Table_Foreign_Key(); string fkname, ftablename; foreach (Dictionary <string, string> table_fkInfo in table_fk.Values) { fkname = table_fkInfo["fkname"]; ftablename = table_fkInfo["ftablename"]; //fkcol = table_fkInfo["fkcol"]; //rtable = table_fkInfo["rtable"]; result += string.Format(sql_refer_tempalte, fkname, ftablename); } ArrayList allTbNames = new ArrayList(tableInfos.Keys.Count()); foreach (Dictionary <string, string> tableInfo in tableInfos.Values) { //获取表名 tablename = tableInfo["Name"]; tablename = UtilString.UcFirst(tablename); columnInfos = UtilSqlserver.FieldInfoList(tablename); allTbNames.Add(tablename); string column_name; //获取主键名称 foreach (Dictionary <string, string> columnInfo in columnInfos.Values) { column_name = columnInfo["Field"]; column_name = UtilString.UcFirst(column_name); if ((column_name.ToUpper().Equals("COMMITTIME")) || (column_name.ToUpper().Equals("UPDATETIME"))) { result += string.Format(sql_df_template, tablename, column_name); } } //result += string.Format(sql_template, tablename); } allTbNames.Sort(); allTbNames.Reverse(); foreach (var tbname in allTbNames) { result += string.Format(sql_template, tbname); } return(result); }
/// <summary> /// 生成数据库主体部分 /// </summary> private static string CreateDbDefine() { string now = DateTime.Now.ToString(); string result = @" /* Betterlife.Net Sqlserver Convert to Mysql Source Server : localhost_3306 Source Server Version : 50520 Source Host : localhost:3306 Source Database : {0} Target Server Type : MYSQL Target Server Version : 50520 File Encoding : 65001 Date: {1} */ SET FOREIGN_KEY_CHECKS=0; "; result = string.Format(result, UtilSqlserver.Database_Name, now); string tablename, tableComment, columnDefine; string sqlTemplate, column_name, column_comment, column_type, column_default, column_pkeys; List <string> primary_keys; Dictionary <string, Dictionary <string, string> > columnInfos; string[] type_length, p_keys, enum_comments; foreach (Dictionary <string, string> tableInfo in tableInfos.Values) { //获取表名 tablename = tableInfo["Name"]; tablename = UtilString.UcFirst(tablename); tableComment = tableInfo["Comment"]; tableComment = tableComment.Replace("\r", "\\r"); tableComment = tableComment.Replace("\n", "\\n"); columnInfos = UtilSqlserver.FieldInfoList(tablename); columnDefine = ""; column_default = ""; primary_keys = new List <string>(); //获取主键名称 foreach (Dictionary <string, string> columnInfo in columnInfos.Values) { column_name = columnInfo["Field"]; column_name = UtilString.UcFirst(column_name); if (columnInfo.Keys.Contains("Comment")) { column_comment = columnInfo["Comment"]; } else { column_comment = column_name; } if (column_name.ToUpper().Equals("ID")) { columnDefine += " `ID` int(11) NOT NULL AUTO_INCREMENT COMMENT '" + column_comment + "',"; primary_keys.Add("`ID`"); } else { //获取列名|列类型|是否Null column_type = columnInfo["Type"]; //column_default = columnInfo["Default"]; if (column_name.ToUpper().Contains("_ID")) { column_type = "int(11) NOT NULL "; primary_keys.Add("`" + column_name + "`"); column_default = "DEFAULT '0'"; } else { if (columnInfo["Null"].Equals("YES") || columnInfo["Null"].Equals("是")) { column_default = "DEFAULT NULL"; } else { column_default = "NOT NULL"; } if (UtilString.Contains(column_name.ToUpper(), "TIME", "DATE")) { if (UtilString.Contains(column_name.ToUpper(), "TIMES")) { column_type = "int"; column_default = "DEFAULT '0'"; } else { column_type = "datetime"; } } } type_length = column_type.Split(new char[4] { '[', ']', '(', ')' }, StringSplitOptions.RemoveEmptyEntries); if (type_length.Length == 1) { column_type = type_length[0]; if (column_type.Equals("nvarchar")) { column_type = "varchar"; } if (column_type.Equals("tinyint")) { enum_comments = Enum_ColumnDefine(column_comment); if ((enum_comments != null) && (enum_comments.Length > 1)) { column_type = "enum(" + string.Join(",", enum_comments) + ")"; } } else { if (!column_type.Equals("datetime") && !type_length.Contains("date")) { string length = columnInfo["Length"]; int i_len = UtilNumber.Parse(length); column_type += "(" + i_len + ")"; } } } column_comment = column_comment.Replace("'", "\""); column_comment = column_comment.Replace("\r", "\\r"); column_comment = column_comment.Replace("\n", "\\n"); column_comment = "COMMENT '" + column_comment + "'"; columnDefine += string.Format(" `{0}` {1} {2} {3},", column_name, column_type, column_default, column_comment); } columnDefine += "\r\n"; } if (columnDefine.Length > 2) { columnDefine = columnDefine.Substring(0, columnDefine.Length - 2); } //生成表脚本 sqlTemplate = @" -- ---------------------------- -- Table structure for `{0}` -- ---------------------------- DROP TABLE IF EXISTS `{0}`; CREATE TABLE `{0}` ( {1} ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='{2}'; "; p_keys = primary_keys.ToArray(); column_pkeys = "\r\n PRIMARY KEY (" + string.Join(",", p_keys) + ")"; columnDefine += column_pkeys; result += string.Format(sqlTemplate, tablename, columnDefine, tableComment); } return(result); }