/// <summary> /// 测试连接 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(cbbServer.Text)) { MessageBox.Show("服务器不能为空!"); return; } if (string.IsNullOrEmpty(txtUserName.Text)) { MessageBox.Show("登陆名不能为空!"); return; } try { IDbObject dbObejct = new Dos.DbObjects.MySQL.DbObject(false, cbbServer.Text, txtUserName.Text, txtPassword.Text, txtport.Text); DataTable DBNameTable = dbObejct.GetDBList(); cbbDatabase.Items.Clear(); cbbDatabase.Items.Add("全部"); foreach (DataRow dr in DBNameTable.Rows) { cbbDatabase.Items.Add(dr[0].ToString()); } cbbDatabase.Enabled = true; cbbDatabase.SelectedIndex = 0; MessageBox.Show("连接成功!"); } catch (Exception ex) { MessageBox.Show("连接失败!\n\r" + ex.Message); cbbDatabase.Enabled = false; } }
/// <summary> /// 加载 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ContentForm_Load(object sender, EventArgs e) { IDbObject dbObject = null; if (ConnectionModel.DbType.Equals(Dos.ORM.DatabaseType.SqlServer.ToString())) { dbObject = new Dos.DbObjects.SQL2000.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(Dos.ORM.DatabaseType.SqlServer9.ToString())) { dbObject = new Dos.DbObjects.SQL2005.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(Dos.ORM.DatabaseType.MsAccess.ToString())) { dbObject = new Dos.DbObjects.OleDb.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(Dos.ORM.DatabaseType.Oracle.ToString())) { dbObject = new Dos.DbObjects.Oracle.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(Dos.ORM.DatabaseType.Sqlite3.ToString())) { dbObject = new Dos.DbObjects.SQLite.DbObject(ConnectionModel.ConnectionString); } else if (ConnectionModel.DbType.Equals(Dos.ORM.DatabaseType.MySql.ToString())) { dbObject = new Dos.DbObjects.MySQL.DbObject(ConnectionModel.ConnectionString); } else { MessageBox.Show("未知数据库类型!"); return; } columnsdt = dbObject.GetColumnInfoList(DatabaseName, TableName); gridColumns.DataSource = columnsdt; DataTable primarykeydt = dbObject.GetKeyName(DatabaseName, TableName); cbPrimarykey.Items.Clear(); if (null != primarykeydt && primarykeydt.Rows.Count > 0) { foreach (DataRow dr in primarykeydt.Rows) { cbPrimarykey.Items.Add(dr["ColumnName"].ToString()); } cbPrimarykey.SelectedIndex = 0; } txtClassName.Text = TableName.Trim().Replace(' ', '_'); txtnamespace.Text = Utils.ReadNamespace(); }
/// <summary> /// 确定 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(cbbServer.Text)) { MessageBox.Show("服务器不能为空!"); return; } if (string.IsNullOrEmpty(txtUserName.Text)) { MessageBox.Show("登陆名不能为空!"); return; } IDbObject dbObejct = new Dos.DbObjects.MySQL.DbObject(false, cbbServer.Text, txtUserName.Text, txtPassword.Text, txtport.Text); string tempconnectionstring = dbObejct.DbConnectStr; try { using (MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(tempconnectionstring)) { connection.Open(); } } catch (Exception ex) { MessageBox.Show("连接失败!\n\r" + ex.Message); return; } Model.Connection connectionModel = new Dos.Tools.Model.Connection(); connectionModel.Database = cbbDatabase.SelectedIndex == 0 ? "all" : cbbDatabase.Text; connectionModel.ID = Guid.NewGuid(); connectionModel.Name = cbbServer.Text + "(MySql)[" + connectionModel.Database + "]"; connectionModel.ConnectionString = tempconnectionstring; connectionModel.DbType = Dos.ORM.DatabaseType.MySql.ToString(); Utils.AddConnection(connectionModel); this.DialogResult = DialogResult.OK; this.Close(); }
/// <summary> /// 刷新数据库表和视图 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void 刷新ToolStripMenuItem2_Click(object sender, EventArgs e) { TreeNode node = tview.SelectedNode; node.Nodes.Clear(); Model.Connection conModel = list.Find(delegate(Model.Connection con) { return(con.ID.ToString().Equals(node.Parent.Tag.ToString())); }); IDbObject dbObject; if (conModel.DbType.Equals(Dos.ORM.DatabaseType.MsAccess.ToString())) { dbObject = new Dos.DbObjects.OleDb.DbObject(conModel.ConnectionString); gettables(node, dbObject.GetTables(""), dbObject.GetVIEWs("")); } else if (conModel.DbType.Equals(Dos.ORM.DatabaseType.SqlServer.ToString())) { dbObject = new Dos.DbObjects.SQL2000.DbObject(conModel.ConnectionString); gettables(node, dbObject.GetTables(node.Text), dbObject.GetVIEWs(node.Text)); } else if (conModel.DbType.Equals(Dos.ORM.DatabaseType.SqlServer9.ToString())) { dbObject = new Dos.DbObjects.SQL2005.DbObject(conModel.ConnectionString); gettables(node, dbObject.GetTables(node.Text), dbObject.GetVIEWs(node.Text)); } else if (conModel.DbType.Equals(Dos.ORM.DatabaseType.Oracle.ToString())) { dbObject = new Dos.DbObjects.Oracle.DbObject(conModel.ConnectionString); gettables(node, dbObject.GetTables(node.Text), dbObject.GetVIEWs(node.Text)); } else if (conModel.DbType.Equals(Dos.ORM.DatabaseType.MySql.ToString())) { dbObject = new Dos.DbObjects.MySQL.DbObject(conModel.ConnectionString); gettables(node, dbObject.GetTables(node.Text), dbObject.GetVIEWs(node.Text)); } }
/// <summary> /// /// </summary> private void getDatabaseinfo() { TreeNode node = tview.SelectedNode; Model.Connection conModel = list.Find(delegate(Model.Connection con) { return(con.ID.ToString().Equals(node.Tag.ToString())); }); IDbObject dbObject; if (conModel.DbType.Equals(Dos.ORM.DatabaseType.MsAccess.ToString())) { dbObject = new Dos.DbObjects.OleDb.DbObject(conModel.ConnectionString); TreeNode tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conModel.ConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); gettables(tnode, dbObject.GetTables(""), dbObject.GetVIEWs("")); } else if (conModel.DbType.Equals(Dos.ORM.DatabaseType.Sqlite3.ToString())) { dbObject = new Dos.DbObjects.SQLite.DbObject(conModel.ConnectionString); TreeNode tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conModel.ConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); gettables(tnode, dbObject.GetTables(""), dbObject.GetVIEWs("")); } else if (conModel.DbType.Equals(Dos.ORM.DatabaseType.SqlServer.ToString()) || conModel.DbType.Equals(Dos.ORM.DatabaseType.SqlServer9.ToString())) { if (conModel.DbType.Equals(Dos.ORM.DatabaseType.SqlServer.ToString())) { dbObject = new Dos.DbObjects.SQL2000.DbObject(conModel.ConnectionString); } else { dbObject = new Dos.DbObjects.SQL2005.DbObject(conModel.ConnectionString); } if (conModel.Database.Equals("all")) { DataTable dt = dbObject.GetDBList(); foreach (DataRow dr in dt.Rows) { TreeNode tnode = new TreeNode(dr[0].ToString(), 1, 1); tnode.Tag = conModel.ConnectionString.Replace("master", dr[0].ToString()); tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); gettables(tnode, dbObject.GetTables(tnode.Text), dbObject.GetVIEWs(tnode.Text)); } } else { TreeNode tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conModel.ConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); gettables(tnode, dbObject.GetTables(tnode.Text), dbObject.GetVIEWs(tnode.Text)); } } else if (conModel.DbType.Equals(Dos.ORM.DatabaseType.Oracle.ToString())) { dbObject = new Dos.DbObjects.Oracle.DbObject(conModel.ConnectionString); TreeNode tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conModel.ConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); gettables(tnode, dbObject.GetTables(tnode.Text), dbObject.GetVIEWs(tnode.Text)); } else if (conModel.DbType.Equals(Dos.ORM.DatabaseType.MySql.ToString())) { dbObject = new Dos.DbObjects.MySQL.DbObject(conModel.ConnectionString); if (conModel.Database.Equals("all")) { DataTable dt = dbObject.GetDBList(); foreach (DataRow dr in dt.Rows) { TreeNode tnode = new TreeNode(dr[0].ToString(), 1, 1); tnode.Tag = conModel.ConnectionString.Replace("master", dr[0].ToString()); tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); gettables(tnode, dbObject.GetTables(tnode.Text), dbObject.GetVIEWs(tnode.Text)); } } else { TreeNode tnode = new TreeNode(conModel.Database, 1, 1); tnode.Tag = conModel.ConnectionString; tnode.ContextMenuStrip = contextMenuStripOneDataBase; node.Nodes.Add(tnode); gettables(tnode, dbObject.GetTables(tnode.Text), dbObject.GetVIEWs(tnode.Text)); } } }
static void Main(string[] args) { //关于RazorEngine的问题 //我已经尽最大努力希望能够成功迁移RazorEngine,更好地实现模板生成的功能。 //但最终无法完成,原因: // RazorEngine不支持DotNet Core,运行时报错,引用了system.web.razor // 找到RazorEngine.NetCore,开始感觉很惊喜,最后很失望,抛出异常,无解,连Demo都过不了 // 下载源码,发现它还引用了System.Runtime.Remoting,查资料被DotNet Core废弃 // 但微软资料上3.0版本貌似可以查得到文档,以下版本直接提示不支持 //根据实践经验,模板几乎不用调整,所以干脆手写拼接。。。将模板功能彻底放弃 //本源码基于Dotnet Core2.2,Ubutun19.04开发。 //string TplContent = ""; //模板内容 string databaseName = ""; //数据库名 Connection connectionModel = null; Dictionary <string, bool> tableview = new Dictionary <string, bool>(); //所有表和视图 string txtTableStar = ""; //读取目录中所有模板文件列表 //FileInfo[] tpls = new DirectoryInfo(Path.Combine(AppContext.BaseDirectory, "Template")).GetFiles("*.tplx", SearchOption.AllDirectories); //选择第一个模板 //var tpl = tpls[0].FullName; //TplContent = FileHelper.Read(tpl); #region 获取服务器列表,仅使用第一个 List <Connection> list = Utils.GetConnectionList(); connectionModel = list.ElementAt(0); databaseName = connectionModel.Database; Console.WriteLine("服务器信息:"); Console.WriteLine(" 主机:" + connectionModel.Name); Console.WriteLine(" 数据库:" + connectionModel.Database); #endregion IDbObject dbObject = null; try { dbObject = new Dos.DbObjects.MySQL.DbObject(connectionModel.ConnectionString); Sysconfig sysconfigModel; sysconfigModel = Utils.GetSysconfigModel(); string txtNamaspace = sysconfigModel.Namespace; DataTable tablesDT = dbObject.GetTables(databaseName); DataRow[] drs = tablesDT.Select("", "name asc"); int tcnt = 0; if (null != drs && drs.Length > 0) { tcnt = drs.Length; foreach (DataRow dr in drs) { tableview.Add(dr[0].ToString(), false); } } tablesDT = dbObject.GetVIEWs(databaseName); int vcnt = 0; drs = tablesDT.Select("", "name asc"); if (null != drs && drs.Length > 0) { vcnt = drs.Length; foreach (DataRow dr in drs) { tableview.Add(dr[0].ToString(), true); } } Console.WriteLine("将导出此库所有表和视图的实体定义,表:" + tcnt + "、视图:" + vcnt); #region 开始导出所有表和视图模型 EntityBuilder builder; Console.WriteLine("********************************"); foreach (var dr in tableview) { string o = dr.Key; var ro = !string.IsNullOrWhiteSpace(txtTableStar.Trim()) ? o.Trim().Replace(' ', '_').Replace(txtTableStar.Trim(), "") : o.Trim().Replace(' ', '_'); builder = new EntityBuilder(o, txtNamaspace, ro, Utils.GetColumnInfos(dbObject.GetColumnInfoList(databaseName, o)), dr.Value, false, //首字母大写 connectionModel.DbType); var path = AppContext.BaseDirectory + "DBModels/"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } using (StreamWriter sw = new StreamWriter(Path.Combine(path, ro + ".cs"), false, Encoding.UTF8)) { Console.WriteLine("导出" + ro + "......OK!"); sw.Write(builder.Builder()); sw.Close(); } System.Threading.Thread.Sleep(1); } Console.WriteLine("********************************"); #endregion Console.WriteLine("实体文件生成完毕!\n按任意健退出"); Console.ReadKey(); } catch (Exception ex) { Console.WriteLine("异常:" + ex.Message); } }