#眉標=Enterprise Library #副標=設計模型套件系列(3) #大標=好用的資料庫存取模組 #作者=文/王寧疆 ==程式1 =========== Database myDb = DatabaseFactory.CreateDatabase("定義在設定檔的連線設定名稱"); ================ ==程式2 =========== SqlDatabase db = new SqlDatabase("定義在設定檔的連線設定名稱"); ================ ==程式3 =========== DbCommand dbCommand = Database物件.GetSqlStringCommand("SQL敘述"); ================ ==程式4 =========== DbCommand dbCommand = Database物件.GetStoredProcCommand("Stored Procedure名稱"); ================ ==程式5 =========== Database物件.AddInParameter(DbCommand物件, "參數名稱", DbType.String, 50); ================ ==程式6 =========== Database物件.AddOutParameter(DbCommand物件, "參數名稱", DbType.String, 50); ================ ==程式7 =========== //使用DatabaseFactory類別的CreateDatabase方法連線到指定的資料庫 Database db=DatabaseFactory.CreateDatabase("定義在設定檔的連線設定名稱"); //指定要執行名稱為GetProductsByCategory的預存程序 DbCommand dbCommand = db.GetStoredProcCommand("GetProductsByCategory"); //將名稱為CategoryID的參數的內容值設定為7 db.AddInParameter(dbCommand, "CategoryID", DbType.Int32, 7); //執行資料庫操作,並將查詢所得到的結果存放到名稱為productDataSet的DataSet DataSet productDataSet = db.ExecuteDataSet(dbCommand); ================ ==程式8 =========== using (IDataReader dataReader = Database物件.ExecuteReader(DbCommand物件)) { //使用查詢資料庫得到的結果記錄 } ================ ==程式9 ===========
<%# Eval("CategoryID")%>
<%# Eval("CategoryName")%>
<%# Eval("Description")%>
================ ==程式10 =========== ================ ==程式11 =========== using Microsoft.Practices.EnterpriseLibrary.Data.Sql; using System.Data.Common; ================ ==程式12 =========== protected void Page_Load(object sender, EventArgs e) { byte[] buf = null; //宣告存放圖形資料的byte陣列 //從專案的web.config設定檔中讀出名稱為NorthwindConnectionString的資料庫連線資訊 string strConn = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString; //依據CategoryID參數的內容值查詢Categories資料表中名稱為Picture的圖形欄位的SQL敘述 string strSQL = "Select Picture from Categories where CategoryID=@CategoryID"; SqlDatabase db = new SqlDatabase(strConn); //建立SqlDatabase類別的物件 DbCommand cmd = db.GetSqlStringCommand(strSQL); //建立負責執行上述SQL敘述的DbCommand db.AddInParameter(cmd, "CategoryID", DbType.Int32, Request["ID"]); //將傳給網頁的ID參數的內容值指定給DbCommand使用 buf = (byte[])db.ExecuteScalar(cmd); //執行查詢圖形欄位的SQL敘述並填入到名稱為buf的byte陣列 Response.BinaryWrite(buf); //將buf陣列輸出到使用者的瀏覽器 } ================ ==程式13 =========== System.IO.MemoryStream ms=new System.IO.MemoryStream(); //建立MemoryStream類別的物件 ms.Write(buf, 78, buf.Length-78); //將buf陣列的內容寫入MemoryStream類別的物件 System.Drawing.Bitmap bmp= new System.Drawing.Bitmap(ms); //依據MemoryStream物件的內容建立Bitmap類別的物件 //將Bitmap類別的物件以Jpeg格式輸出到使用者的瀏覽器 bmp.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); ms.Close(); //關閉MemoryStream類別的物件 ================