private void btnDel_Click(object sender, EventArgs e) { try { var id = Convert.ToInt32(txtId.Text); if (id > 0) { DialogResult re = MessageBox.Show("确定要删除该项吗?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); if (re == DialogResult.OK) { FormDbService.DelDb(id); Bind(); TxtSqlServer.Text = ""; TxtSqlDataBase.Text = ""; TxtSqlUserName.Text = ""; TxtSqlPwd.Text = ""; txtId.Text = ""; MessageBox.Show("操作成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception ex) { MessageBox.Show("操作异常:" + ex, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
private void btnSave_Click(object sender, EventArgs e) { try { var db = new FormDbService.DbInfo(); db.Id = Convert.ToInt32(txtId.Text); db.Server = TxtSqlServer.Text; db.UserName = TxtSqlUserName.Text; db.Password = TxtSqlPwd.Text; db.Database = TxtSqlDataBase.Text; if (db.Server.Length == 0 || db.UserName.Length == 0 || db.Password.Length == 0 || db.Database.Length == 0) { MessageBox.Show("抱歉,以上所有项均不能为空!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } btnSave.Enabled = false; string connection = GetConnectionString(); var thread = new Thread(() => { UpdateUI(() => { lblStatus.Text = "测试连接中,请稍后..."; }); if (CheckConnection(connection)) { FormDbService.EditDb(db); UpdateUI(() => { lblStatus.Text = ""; btnSave.Enabled = true; MessageBox.Show("操作成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); }); } else { UpdateUI(() => { lblStatus.Text = ""; btnSave.Enabled = true; MessageBox.Show("抱歉,数据库连接失败,数据未保存\n请检查配置参数准确性后再试!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); }); return; } UpdateUI(() => { btnSave.Enabled = true; Bind(); }); }); thread.IsBackground = true; thread.Start(); } catch (Exception ex) { MessageBox.Show("操作异常:" + ex, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
public void Bind() { listExsit.Items.Clear(); var dbs = FormDbService.GetDbs(); foreach (FormDbService.DbInfo db in dbs) { listExsit.Items.Add(db.Id + " " + db.Server.Trim() + "-" + db.Database.Trim()); } }
private void cmbDatabase_SelectedIndexChanged(object sender, EventArgs e) { if (cmbDatabase.SelectedItem != null && cmbDatabase.SelectedItem.ToString() != TipsDB) { //保存最后使用项 var config = ConfigHelper.GetConfig <AppConfig>(); config.LastDb = cmbDatabase.SelectedItem.ToString(); ConfigHelper.UpdateConfig(config); //联动加载表 var id = cmbDatabase.SelectedItem.ToString().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)[0]; listBox.Items.Clear(); //listBox.Items.Add(TipsDbTable); var thread = new Thread(() => { UpdateUI(() => { lblStatus.Text = "正在读取数据库架构,请稍候..."; }); var conn = FormDbService.GetConnectionString(Convert.ToInt32(id)); var tables = SqlserverHelper.GetTableNames(conn); if (tables.Count > 0) { if (tables[0] == "Error") { UpdateUI(() => { lblStatus.Text = "数据库连接失败,请检查数据连接配置是否正确"; }); } else { UpdateUI(() => { foreach (var s in tables) { listBox.Items.Add(s); } lblStatus.Text = "共" + listBox.Items.Count + "张表"; listBox.SelectedIndex = 0; }); } } }) { IsBackground = true }; thread.Start(); } }
private void listExsit_SelectedIndexChanged(object sender, EventArgs e) { if (listExsit.SelectedItem != null) { var id = listExsit.SelectedItem.ToString().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries)[0]; var db = FormDbService.GetDb(Convert.ToInt32(id)); if (db != null) { txtId.Text = db.Id.ToString(); TxtSqlServer.Text = db.Server; TxtSqlDataBase.Text = db.Database; TxtSqlUserName.Text = db.UserName; TxtSqlPwd.Text = db.Password; btnDel.Enabled = true; } else { btnDel.Enabled = false; } } }
public static string CreateCode(int dbId, string tableName, string templateFile) { var sw = new Stopwatch(); sw.Start(); var config = ConfigHelper.GetConfig <AppConfig>(); var con = FormDbService.GetConnectionString(dbId); var template = BuildManager.CreateTemplate(templateFile); #region 表实体 var className = tableName.Contains("_") ? tableName.Substring(tableName.LastIndexOf("_", StringComparison.Ordinal) + 1) : tableName; var classChineseName = SqlserverHelper.GetTableNote(con, tableName); var tableInfo = new TableInfo { TableNote = classChineseName, ClassName = className, TableName = tableName }; #endregion #region 字段 var fieldList = SqlserverHelper.GetFieldInfoList(con, tableName); var list = new List <FieldInfo>(); var except = config.ExceptFields ?? ""; var excepts = except.Replace(",", ",").ToLower().Trim().Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); foreach (var fieldInfo in fieldList) { if (!excepts.Contains(fieldInfo.Name.ToLower())) { list.Add(fieldInfo); } if (fieldInfo.IsPrimaryKey) { tableInfo.PrimaryKey = fieldInfo.Name; } } #endregion #region 加载模板内置对象 Dictionary <string, PresentBase> tempData = new Dictionary <string, PresentBase>(); if (tempData.Count <= 0) { Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly assembly in assemblies) { //反射调用展现层类库 if (assembly.FullName.Contains("RC.Software.Presentation")) { Type[] classes = assembly.GetTypes(); foreach (Type type in classes) { if (IsInherit(type, typeof(PresentBase))) { string name = type.Name.ToLower(); var instance = Activator.CreateInstance(type) as PresentBase; tempData.Add(name, instance); } } } } } foreach (var pair in tempData) { template.Context.TempData[pair.Key] = pair.Value; } #endregion template.Context.CurrentPath = templateFile; template.Context.TempData["datetime"] = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); template.Context.TempData["conn"] = con; template.Context.TempData["table"] = tableInfo; template.Context.TempData["fields"] = list; template.Context.TempData["author"] = config.Author ?? ""; template.Context.TempData["namespace"] = config.NameSpace ?? ""; #region 内置标签方法 (动态加载) object insideStaticLabel = null; try { const string insideClassName = "RC.Software.Presentation.InsideStaticLabel"; string fileName = Path.Combine(Thread.GetDomain().BaseDirectory, @"InsideStaticLabel.cs"); if (File.Exists(fileName)) { var sourceFile = new FileInfo(fileName); CodeDomProvider provider = new CSharpCodeProvider(); var cp = new CompilerParameters(); cp.ReferencedAssemblies.Add("System.dll"); //添加命名空间引用 cp.ReferencedAssemblies.Add("RC.Software.Presentation.dll"); //添加命名空间引用 cp.ReferencedAssemblies.Add("RC.Software.Framework.dll"); cp.GenerateExecutable = false; // 生成类库 cp.GenerateInMemory = true; // 保存到内存 cp.TreatWarningsAsErrors = false; // 不将编译警告作为错误 // 编译 CompilerResults results = provider.CompileAssemblyFromFile(cp, sourceFile.FullName); if (results.Errors.Count < 1) { Assembly asm = results.CompiledAssembly; // 加载 insideStaticLabel = asm.CreateInstance(insideClassName); //获取编译后的类型 } else { string msg = null; for (int index = 0; index < results.Errors.Count; index++) { CompilerError error = results.Errors[index]; msg += "【错误" + (index + 1) + "】" + Environment.NewLine; msg += "[文件] " + error.FileName + Environment.NewLine; msg += "[位置] 行" + error.Line + ",列" + error.Column + Environment.NewLine; msg += "[信息] " + error.ErrorText + Environment.NewLine; msg += Environment.NewLine; } MessageBox.Show(msg, "内置方法类编译错误"); } } } catch { //如果用户文件写的有问题,用系统内置的标签方法 if (insideStaticLabel == null) { insideStaticLabel = new InsideStaticLabel(); } } if (insideStaticLabel == null) { insideStaticLabel = new InsideStaticLabel(); } template.Context.TempData["rc"] = insideStaticLabel; #endregion var html = template.Render(); return(html); }