C#動態建庫建表學習
阿新 • • 發佈:2019-01-27
ITOO3.0的學習中為需要動態的建庫建表來滿足業務的靈活性,該功能使用過拼接建庫建表的SQL語句以及SQLHelper實現的。
這裡只展示操作類,至於SQLHelper不再贅述。
前臺呼叫操作類方法#region IsDBExist-判斷資料庫是否存在 /// <summary> /// 判斷資料庫是否存在 /// </summary> /// <param name="db">資料庫的名稱</param> /// <param name="connKey">資料庫的連線Key</param> /// <returns>true:表示資料庫已經存在;false,表示資料庫不存在</returns> public Boolean IsDBExist(string db, string connKey) { SQLHelper helper = SQLHelper.GetInstance(); string connToMaster = ConfigurationManager.ConnectionStrings[connKey].ToString(); string createDbStr = " select * from master.dbo.sysdatabases where name " + "= '" + db + "'"; DataTable dt = helper.ExecuteQuery(createDbStr, CommandType.Text); if (dt.Rows.Count == 0) { return false; } else { return true; } } #endregion #region IsTableExist-判斷資料庫表是否存在 /// <summary> /// 判斷資料庫表是否存在 /// </summary> /// <param name="db">資料庫</param> /// <param name="tb">資料庫表名</param> /// <param name="connKey">連線資料庫的key</param> /// <returns></returns> public Boolean IsTableExist(string db, string tb, string connKey) { SQLHelper helper = SQLHelper.GetInstance(); string connToMaster = ConfigurationManager.ConnectionStrings[connKey].ToString(); string createDbStr = "use " + db + " select 1 from sysobjects where id = object_id('" + tb + "') and type ='U'"; //在指定的資料庫中 查詢該表是否存在 DataTable dt = helper.ExecuteQuery(createDbStr, CommandType.Text); if (dt.Rows.Count == 0) { return false; } else { return true; } } #endregion #region CreateDataBase-建立資料庫 /// <summary> /// 建立資料庫 /// </summary> /// <param name="db">資料庫名稱</param> /// <param name="connKey">連線資料庫的key</param> public void CreateDataBase(string db, string connKey) { SQLHelper helper = SQLHelper.GetInstance(); //符號變數,判斷資料庫是否存在 Boolean flag = IsDBExist(db, connKey); //如果資料庫存在,則丟擲 if (flag == true) { throw new Exception("資料庫已經存在!"); } else { //資料庫不存在,建立資料庫 string connToMaster = ConfigurationManager.ConnectionStrings[connKey].ToString(); string createDbStr = "Create database " + db; helper.ExecuteNonQuery(createDbStr, CommandType.Text); } } #endregion #region CreateDataTable-在指定的資料庫中,建立資料庫表 /// <summary> ///在指定的資料庫中,建立資料庫表 /// </summary> /// <param name="db">指定的資料庫</param> /// <param name="dt">要建立的資料庫表</param> /// <param name="dic">資料表中的欄位及其資料型別</param> /// <param name="connKey">資料庫的連線Key</param> public void CreateDataTable(string db, string dt, Dictionary<string, string> dic, string connKey) { SQLHelper helper = SQLHelper.GetInstance(); string connToMaster = ConfigurationManager.ConnectionStrings[connKey].ToString(); //判斷資料庫是否存在 if (IsDBExist(db, connKey) == false) { throw new Exception("資料庫不存在!"); } //如果資料庫表存在,則丟擲錯誤 if (IsTableExist(db, dt, connKey) == true) { throw new Exception("資料庫表已經存在!"); } //資料庫表不存在,建立表 else { //拼接字串,該串為建立內容 string content = "serial int identity(1,1) primary key"; //取出dic中的內容,進行拼接 List<string> test = new List<string>(dic.Keys); for (int i = 0; i < dic.Count(); i++) { content = content + "," + test[i] + "" + dic[test[i]]; } //其後判斷資料庫表是否存在,然建立表 string createTableStr = "use " + db + "create table" + dt + "(" + content + ")"; helper.ExecuteNonQuery(createTableStr, CommandType.Text); } } #endregion #region DropDataTable-批量刪除資料表 /// <summary> /// 批量刪除資料庫 /// </summary> /// <param name="db">指定的資料庫</param> /// <param name="dt">要刪除的資料庫表集合</param> /// <param name="connKey">資料庫連線串</param> /// <returns>刪除是否成功,true表示刪除成功,false表示刪除失敗</returns> public bool DropDataTable(string db, string[] dt, string connKey) { SQLHelper helper = SQLHelper.GetInstance(); string connToMaster = ConfigurationManager.ConnectionStrings[connKey].ToString(); //判斷資料庫是否存在 if (IsDBExist(db, connKey) == false) { throw new Exception("資料庫不存在!"); } for (int i = 0; i < dt.Count(); i++) { //如果資料庫表存在,則丟擲錯誤 if (IsTableExist(db, dt[i], connKey) == false) { //如果資料庫表已經刪除,則跳過該表 continue; } else//資料表存在,則進行刪除資料表 { //其後判斷資料表是否存在,然後建立資料表 string createTableStr = "use " + db + " drop table " + dt[i] + " "; helper.ExecuteNonQuery(createTableStr, CommandType.Text); } } return true; } #endregion #region DropDataBase-刪除資料庫 /// <summary> /// 刪除資料庫 /// </summary> /// <param name="db">資料庫名</param> /// <param name="connKey">資料庫連線串</param> /// <returns>刪除成功為true,刪除失敗為false</returns> public bool DropDataBase(string db, string connKey) { SQLHelper helper = SQLHelper.GetInstance(); //符號變數,判斷資料庫是否存在 Boolean flag = IsDBExist(db, connKey); //如果資料庫不存在,則丟擲 if (flag == false) { return false; } else { //資料庫存在,刪除資料庫 string connToMaster = ConfigurationManager.ConnectionStrings[connKey].ToString(); string createDbStr = "Drop database " + db; helper.ExecuteNonQuery(createDbStr, CommandType.Text); return true; } } #endregion #region CreateDataTable-建立資料庫表(多張表) /// <summary> /// 在指定的資料庫中,建立資料表 /// </summary> /// <param name="db">指定的資料庫</param> /// <param name="dt">要建立的資料表集合</param> /// <param name="dic">資料表中的欄位及其資料型別 Dictionary集合</param> /// <param name="connKey">資料庫的連線Key</param> public void CreateDataTable(string db, string[] dt, List<Dictionary<string, string>> dic, string connKey) { SQLHelper helper = SQLHelper.GetInstance(); string connToMaster = ConfigurationManager.ConnectionStrings[connKey].ToString(); //判斷資料庫是否存在 if (IsDBExist(db, connKey) == false) { throw new Exception("資料庫不存在!"); } for (int i = 0; i < dt.Count(); i++) { //如果資料庫表存在,則丟擲錯誤 if (IsTableExist(db, dt[i], connKey) == true) { //如果資料庫表已經存在,則跳過該表 continue; } else//資料表不存在,建立資料表 { //其後判斷資料表是否存在,然後建立資料表 string createTableStr = PinjieSql(db, dt[i], dic[i]); helper.ExecuteNonQuery(createTableStr, CommandType.Text); } } } #endregion #region PinjieSql-拼接建立資料庫表的Sql語句 /// <summary> /// 拼接建立資料庫表的Sql語句 /// </summary> /// <param name="db">指定的資料庫</param> /// <param name="dt">要建立的資料表</param> /// <param name="dic">資料表中的欄位及其資料型別</param> /// <returns>拼接完的字串</returns> public string PinjieSql(string db, string dt, Dictionary<string, string> dic) { //拼接字串,(該串為建立內容) string content = "serial int identity(1,1) primary key "; //取出dic中的內容,進行拼接 List<string> test = new List<string>(dic.Keys); for (int i = 0; i < dic.Count(); i++) { content = content + " , " + test[i] + " " + dic[test[i]]; } //其後判斷資料表是否存在,然後建立資料表 string createTableStr = "use " + db + " create table " + dt + " (" + content + ")"; return createTableStr; } #endregion
該功能僅限於學習階段,是對他人部落格進行的實踐學習。if (!Page .IsPostBack) { dynamicCreateDB operateDB = new dynamicCreateDB(); //先刪除ceshi 資料庫 operateDB.DropDataBase("ceshi", "MSSql2012"); //建立一個名為test的庫 operateDB.CreateDataBase("ceshi", "MSSql2012"); //建立鍵值集合 List<Dictionary<string, string>> listDic = new List<Dictionary<string, string>>(); //用一個鍵值來儲存資料庫表的欄位和資料型別 Dictionary<string, string> dic1 = new Dictionary<string, string>(); dic1.Add("questionName", "varchar(20)"); dic1.Add("content", "varchar(20)"); Dictionary<string, string> dic2 = new Dictionary<string, string>(); dic2.Add("questionName", "varchar(20)"); dic2.Add("content", "varchar(20)"); listDic.Add(dic1); listDic.Add(dic2); //在test庫中建立一張紙名為xuanzeti的表 string[] listTable = { "xuanzeti", "tiankongti" }; operateDB.CreateDataTable("ceshi",listTable, listDic,"MSSql2012"); operateDB.DropDataTable("ceshi", listTable, "MSSql2012"); operateDB.DropDataBase("ceshi","MSSql2012");