//判断是否包含管理数据库 public bool existGuard() { string sql = "select * from master..sysdatabases where name='" + NameUnit.getName(NameUnit.guradDb) + "'"; SqlConnection conn = getConn(); conn.Open(); SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = sql; SqlDataReader reader = cmd.ExecuteReader(); if (reader.HasRows) { reader.Close(); conn.Close(); return(true); } else { reader.Close(); conn.Close(); return(false); } }
//创建项目数据库 public string createDataBase(string proName) { projectName = NameUnit.getName(proName); string currentDirectory = System.Environment.CurrentDirectory; SqlConnection conn = DbHelper.getInstance().getConn(); string dataBaseName = NameUnit.getName(proName); string mdfName = currentDirectory + "\\database\\" + dataBaseName + ".mdf"; string ldfName = currentDirectory + "\\database\\" + dataBaseName + ".ldf"; //创建数据库 string sql = "CREATE DATABASE " + dataBaseName + " on primary" + "(name=" + dataBaseName + ",filename='" + mdfName + "' ,size=3, maxsize=5,filegrowth=10%)"; sql += "log on" + "(name=" + dataBaseName + "_log,filename='" + ldfName + "',size=3,filegrowth=1)"; conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); try { cmd.ExecuteNonQuery(); conn.Close(); return(dataBaseName); } catch (Exception e) { MessageBox.Show(e.Message); conn.Close(); return(""); } }
private void main_Load(object sender, EventArgs e) { //2 auto.GetInitialFormSize(this); auto.GetAllCrlLocation(this); auto.GetAllCrlSize(this); //初始化创建数据库参数 NameUnit.initParams(); }
public ProjectDelete() { InitializeComponent(); db = DbHelper.getInstance(); conn = db.getConn(db.getConnStr(NameUnit.getName(NameUnit.guradDb))); connDb = db.getConn(); connDb.Open(); initArea(); }
private void button_create_Click(object sender, EventArgs e) { projectName = this.comboBox_projectName.Text.Trim(); projectArea = this.comboBox_projectArea.Text.Trim(); projectPerson = this.textBox_person.Text.Trim(); projectDate = DateTime.Now.ToString(); if (projectName == "") { this.comboBox_projectName.Focus(); MessageBox.Show("项目名称不能为空"); return; } if (projectArea == "") { this.comboBox_projectArea.Focus(); MessageBox.Show("地区名称不能为空"); return; } if (projectPerson == "") { this.textBox_person.Focus(); MessageBox.Show("项目负责人不能为空"); return; } if (NameUnit.existProject(projectName, projectArea)) { MessageBox.Show("项目已经存在,不能重新创建"); return; } dbName = projectArea + "_" + projectName; //项目不存在的话创建数据库 TableHelper table = new TableHelper(); string baseName = table.createDataBase(dbName); if (baseName != "") { //创建数据表 table.createTable(); //将项目信息添加进数据管理表 string sql = "insert into " + NameUnit.projectTable + "(PROJECT_ID,VERSION_DATE,DB_NAME,CITY,PRINCIPAL)values('" + projectName + "','" + projectDate + "','" + baseName + "','" + projectArea + "','" + projectPerson + "')"; NameUnit.insertProject(sql); MessageBox.Show("项目创建成功"); this.Close(); } }
//将项目添加到projectTable中 public static void insertProject(string sql) { DbHelper db = DbHelper.getInstance(); SqlConnection conn = db.getConn(db.getConnStr(NameUnit.getName(NameUnit.guradDb))); if (db.execSql(sql, conn)) { conn.Close(); return; } else { conn.Close(); return; } }
//创建管理数据库并建立响应的表结构 public void createGuardDb() { createDataBase(NameUnit.guradDb); // DbHelper db = DbHelper.getInstance(); SqlConnection conn = db.getConn(db.getConnStr(NameUnit.getName(NameUnit.guradDb))); try { db.execSql(NameUnit.tbProjectManagement, conn); conn.Close(); } catch (Exception e) { MessageBox.Show(e.Message); } }
//判断项目是否存在 public static bool existProject(string proName, string proArea) { string sql = "select * from " + projectTable + " where PROJECT_ID = '" + proName + "' and CITY='" + proArea + "'"; DbHelper db = DbHelper.getInstance(); SqlConnection conn = db.getConn(db.getConnStr(NameUnit.getName(NameUnit.guradDb))); if (db.existSql(conn, sql)) { conn.Close(); return(true); } else { conn.Close(); return(false); } }
//初始化控件 private void InitCombox() { db = DbHelper.getInstance(); conn = db.getConn(db.getConnStr(NameUnit.getName(NameUnit.guradDb))); string sql = "select distinct PROJECT_ID from " + NameUnit.projectTable; this.comboBox_projectName.DataSource = db.getTable(sql, conn); this.comboBox_projectName.DisplayMember = "PROJECT_ID"; this.comboBox_projectName.ValueMember = "PROJECT_ID"; string sql1 = "select distinct CITY from " + NameUnit.projectTable; this.comboBox_projectArea.DataSource = db.getTable(sql1, conn); this.comboBox_projectArea.DisplayMember = "CITY"; this.comboBox_projectArea.ValueMember = "CITY"; }