達夢數(shù)據(jù)庫及在.Net下的增刪改查
一、簡介
偶然看到了國產(chǎn)數(shù)據(jù)庫——達夢數(shù)據(jù)庫。頓時起了興趣,搗鼓了一番。
下面這段簡介摘自百度百科
達夢數(shù)據(jù)庫是武漢華工達夢數(shù)據(jù)庫有限公司推出的具有完全自主知識產(chǎn)權(quán)的高性能數(shù)據(jù)庫產(chǎn)品。它采用“三權(quán)分立”的安全管理機制,安全級別達到B1級,并在大數(shù)據(jù)量存儲管理、并發(fā)控制、數(shù)據(jù)查詢優(yōu)化處理、事務(wù)處理、備份與恢復(fù)和支持SMP系統(tǒng)等諸多方面都有突破性進展和提高。更多介紹:http://baike.baidu.com/view/581717.htm
達夢數(shù)據(jù)庫有免費版和收費版(感覺國人就是有點急功近利)。
免費版下載地址:http://www.dameng.com/dmweb/article.do?type=category&articleid=57021
有Windouws版和Linux版。
二、安裝
我這里選擇Windows版下載,當(dāng)前版本6.2,UI還是做得很漂亮的。
安裝過程很簡單,一路“下一步”就行了:
2.1) 選擇語言版本
2.2) 選擇驗證Key文件,免費版的下載包里提供了驗證文件。
2.3)設(shè)置初始化參數(shù)
2.4)修改數(shù)據(jù)庫口令(密碼),當(dāng)然也可以無視它。默認密碼是: SYSDBA
三、使用
3.1) 打開管 “理工具Manager”登陸
輸入安裝時設(shè)置的口令,如果安裝時沒有修改口令,則輸入初試默認口令“SYSDBA”
3.2) 登陸進去后看到系統(tǒng)默認有兩個數(shù)據(jù)庫“SYSTEM”、“BOOKSHOP”。這里我自己新建了一個叫“Cnblogs”的數(shù)據(jù)庫。新建很簡單,和操作SQLServer差不多。
3.3) 點開“cnblogs”節(jié)點,會發(fā)現(xiàn),它不像SQLServer那樣,下面直接是“表”、“視圖”等。
它下面是“模式”,一個數(shù)據(jù)庫可以有多個模式,模式下面才會有“表”、“視圖”等等。
模式用來代表特定數(shù)據(jù)庫中的一個對象集,在概念上可將其看作是包含表、視圖、索引和權(quán)限定義的對象集合。一個模式只作用于一個數(shù)據(jù)庫,不同的數(shù)據(jù)庫可以有同名模式。
我在模式“SYSDBA”下面建了個“Users”表。
3.4) 達夢SQL
因為達夢數(shù)據(jù)庫有一個“模式”的概念,因此它的查詢語句和SQLServer 也有所不同,要在“表”前加上“模式”。如:
- select * from SYSDBA.Users
當(dāng)然你也可以不加,直接像在SQLServer 里那樣,寫成
- select * from Users
這時它會默認使用SYSDBA模式。
四、使用C#對達夢數(shù)據(jù)庫進行CRUD操作
達夢數(shù)據(jù)庫提供了很多驅(qū)動,包括.Net 。在達夢數(shù)據(jù)的安裝目錄找到 \dmdbms\bin\ DmProvider.dll 程序集,這就是.Net 操作達夢數(shù)據(jù)庫的驅(qū)動程序。我們用VS建個Demo 引用這個dll。增刪改查的演示代碼如下:
- using Dm;
- /// <summary>
- /// 獲得數(shù)據(jù)庫鏈接
- /// </summary>
- /// <returns></returns>
- public DmConnection GetConn()
- {
- string strconn = "server=localhost;database=Cnblogs;User Id=SYSDBA;PWD=SYSDBA";
- DmConnection conn = new DmConnection(strconn);
- conn.Open();
- return conn;
- }
- //插入數(shù)據(jù)
- public void Insert()
- {
- DmConnection conn = GetConn();
- string strCmd = "Insert into SYSDBA.Users(Name,Sex,Age)values('xumingxiang','man',25)";
- DmCommand cmd = new DmCommand(strCmd, conn);
- int effect = cmd.ExecuteNonQuery();
- conn.Close();//關(guān)閉數(shù)據(jù)庫鏈接
- }
- /// <summary>
- /// 刪除數(shù)據(jù)
- /// </summary>
- public void Update()
- {
- DmConnection conn = GetConn();
- string strCmd = "update SYSDBA.Users set Age=100 where Id=1";
- DmCommand cmd = new DmCommand(strCmd, conn);
- int effect = cmd.ExecuteNonQuery();
- conn.Close();
- }
- /// <summary>
- /// 刪除數(shù)據(jù)
- /// </summary>
- public void Delete()
- {
- DmConnection conn = GetConn();
- string strCmd = "delete from SYSDBA.Users where Id=1";
- DmCommand cmd = new DmCommand(strCmd, conn);
- int effect = cmd.ExecuteNonQuery();
- conn.Close();
- }
- /// <summary>
- /// 用ExecuteReader查詢數(shù)據(jù)
- /// </summary>
- public void QueryByExecuteReader()
- {
- DmConnection conn = GetConn();
- string strCmd = "select * from SYSDBA.Users ";
- DmCommand cmd = new DmCommand(strCmd, conn);
- DmDataReader dr = cmd.ExecuteReader();
- int id;
- string name;
- string sex;
- int age;
- while (dr.Read())
- {
- id = dr.GetInt32(0);
- name = dr.GetString(1);
- sex = dr.GetString(2);
- age = dr.GetInt32(3);
- }
- }
- /// <summary>
- ///用DataAdapter 查詢數(shù)據(jù),返回DataSet
- /// </summary>
- public DataSet QueryByDataAdapter()
- {
- DmConnection conn = GetConn();
- string strCmd = "select * from SYSDBA.Users ";
- DmDataAdapter da = new DmDataAdapter(strCmd,conn);
- DataSet ds = new DataSet();
- da.Fill(ds);
- return ds;
- }
在上面的代碼中,我們可看到對達夢數(shù)據(jù)庫進行增刪改查和操作SQLServer 差不多。要說區(qū)別,那就是多個“模式”的概念。
由于只是興趣使然,只是簡單的嘗嘗鮮,我沒有對其性能、負載等方面做測試,也沒打算用它做實際項目。等以后有空了在繼續(xù)搗鼓它吧。不說他好也不說他壞,在精神上支持一下國貨吧!
原文地址:http://www.cnblogs.com/xumingxiang/archive/2012/04/11/2442271.html
【編輯推薦】