[置頂] C#解決 動態(tài)創(chuàng)建庫、動態(tài)創(chuàng)建表
來源:程序員人生 發(fā)布時間:2014-12-09 08:03:35 閱讀次數(shù):2662次
在11月10號。我們開始著手做高校平臺這個項目,這個項目里面,有很多的子系統(tǒng)。權限系統(tǒng)、基礎系統(tǒng)、考試系統(tǒng)……
既然是高校平臺,固然不只是1個學校用的,可以很多的學校公用。既然如此,每一個學校所要保存的數(shù)據(jù)固然不可能寄存在1個庫中。1個學校1個庫,那末有幾個學校用呢?未知。庫的數(shù)量得視情況而定。動態(tài)創(chuàng)建庫可以解決這類問題。
固然,系統(tǒng)有很多。但是個人只是參與子系統(tǒng)的開發(fā)。在之前1直在考試系統(tǒng)里面打雜。這個項目分配下來了以后,我也就開始由打雜換了1種方式去打雜.
在做項目的進程中,任務的分配是非常明確的。甚么時間干甚么事情,每天的任務等等。加上模塊的劃分,各系統(tǒng)之間的耦合要低等原則。這就要求底層的東西要封裝的特別好,重用性很高。
由于之前1直都是在做題型和題庫
這幾個模塊。相對其他的模塊來講,題型這里更加抽象。由于每一個題型都有不同的特點。需要保存的方式也不同。至于題庫,那更是得需要題型提供支持啦。
總不能每一個題型寫1遍。那還不得累死。
在做需求的時候,就已知道,題型的這1部份是活的。這就需要1種新的方法――動態(tài)創(chuàng)建表。
動態(tài)生成庫,生成表。只知道用這些能解決以上的問題。
在沒有接觸這些東西之前,畫題型的原型……不會畫。畫題型的類圖……把每一個題型都畫出來?累死不說,還不能擴大。
這樣做出來的系統(tǒng),要想添加題型,改代碼那是肯定的了。
碰巧,前1陣子被組長調出去了,所要做的任務,也非常的合適。就是要把創(chuàng)建數(shù)據(jù)庫的方法和創(chuàng)建數(shù)據(jù)庫表的方法封裝起來。
這東西做完,對理解題型需求是非常有用的。
任務很模糊,先做1個Demo,能實現(xiàn)就行。
1、提供數(shù)據(jù)庫名,然后去創(chuàng)建。
2、指定數(shù)據(jù)庫和數(shù)據(jù)表。固然還必須提供表字段和數(shù)據(jù)類型。去創(chuàng)建1張表。
在研究了1番以后,也就先用拼接sql語句的方法來做吧。有其他方法的請指出。
這些東西都被封裝在底層了。所以動態(tài)創(chuàng)建庫的方法
都需要用到SqlHelper。這里就不寫了。如果需要的話,文章末尾有下載地址。
#region 判斷
數(shù)據(jù)庫是不是存在
/// <summary>
/// 判斷
數(shù)據(jù)庫是不是存在
/// </summary>
/// <param name="db">
數(shù)據(jù)庫的名稱</param>
/// <param name="connKey">
數(shù)據(jù)庫的連接Key</param>
/// <returns>true:表示
數(shù)據(jù)庫已存在;false,表示
數(shù)據(jù)庫不存在</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 判斷
數(shù)據(jù)庫中,指定表是不是存在
/// <summary>
/// 判斷
數(shù)據(jù)庫表是不是存在
/// </summary>
/// <param name="db">
數(shù)據(jù)庫</param>
/// <param name="tb">數(shù)據(jù)表名</param>
/// <param name="connKey">連接
數(shù)據(jù)庫的key</param>
/// <returns>true:表示數(shù)據(jù)表已存在;false,表示數(shù)據(jù)表不存在</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'";
//在指定的
數(shù)據(jù)庫中 查找 該表是不是存在
DataTable dt = helper.ExecuteQuery(createDbStr, CommandType.Text);
if (dt.Rows.Count == 0)
{
return false;
}
else
{
return true;
}
}
#endregion
#region 創(chuàng)建
數(shù)據(jù)庫
/// <summary>
/// 創(chuàng)建
數(shù)據(jù)庫
/// </summary>
/// <param name="db">
數(shù)據(jù)庫名稱</param>
/// <param name="connKey">連接
數(shù)據(jù)庫的key</param>
public void CreateDataBase(string db, string connKey)
{
SQLHelper helper = SQLHelper.GetInstance();
//符號變量,判斷
數(shù)據(jù)庫是不是存在
Boolean flag = IsDBExist(db, connKey);
//如果
數(shù)據(jù)庫存在,則拋出
if (flag == true)
{
throw new Exception("
數(shù)據(jù)庫已存在!");
}
else
{
//
數(shù)據(jù)庫不存在,創(chuàng)建
數(shù)據(jù)庫
string connToMaster = ConfigurationManager.ConnectionStrings[connKey].ToString();
string createDbStr = "Create database " + db;
helper.ExecuteNonQuery(createDbStr, CommandType.Text);
}
}
#endregion
#region 創(chuàng)建
數(shù)據(jù)庫表
/// <summary>
/// 在指定的
數(shù)據(jù)庫中,創(chuàng)建數(shù)據(jù)表
/// </summary>
/// <param name="db">指定的
數(shù)據(jù)庫</param>
/// <param name="dt">要創(chuàng)建的數(shù)據(jù)表</param>
/// <param name="dic">數(shù)據(jù)表中的字段及其數(shù)據(jù)類型</param>
/// <param name="connKey">
數(shù)據(jù)庫的連接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();
//判斷
數(shù)據(jù)庫是不是存在
if (IsDBExist(db, connKey) == false)
{
throw new Exception("
數(shù)據(jù)庫不存在!");
}
//如果
數(shù)據(jù)庫表存在,則拋出毛病
if (IsTableExist(db, dt, connKey) == true)
{
throw new Exception("
數(shù)據(jù)庫表已存在!");
}
else//數(shù)據(jù)表不存在,創(chuàng)建數(shù)據(jù)表
{
//拼接字符串,(該串為創(chuàng)建內容)
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]];
}
//其后判斷數(shù)據(jù)表是不是存在,然后創(chuàng)建數(shù)據(jù)表
string createTableStr = "use " + db + " create table " + dt + " (" + content + ")";
helper.ExecuteNonQuery(createTableStr, CommandType.Text);
}
}
#endregion
以上就是與動態(tài)創(chuàng)建庫,創(chuàng)建表相干的函數(shù)。
下面這里是調用
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//創(chuàng)建1個名為testQuestion的庫
CreateDataBase("questionType", "MSSql2012");
//用1個dictionary類型,來保存
數(shù)據(jù)庫表的字段 和 數(shù)據(jù)類型
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("questionName", "varchar(20)");
dic.Add("content", "varchar(20)");
//在questionType庫中創(chuàng)建1張名為xuanzeti的表
CreateDataTable("questionType", "xuanzeti", dic, "MSSql2012");
}
}
下面是履行前后的效果:

用拼接Sql語句的方式封裝非常容易理解。作為底層的東西,重用性是非常高的。所以這1塊也就作為初版了。
如果有更好的方式,請聯(lián)系我。
Demo下載地址:http://download.csdn.net/detail/zc474235918/8207857
生活不易,碼農辛苦
如果您覺得本網(wǎng)站對您的學習有所幫助,可以手機掃描二維碼進行捐贈