EnterpriseLibrary20企業(yè)庫(kù)常用方法_第1頁(yè)
EnterpriseLibrary20企業(yè)庫(kù)常用方法_第2頁(yè)
EnterpriseLibrary20企業(yè)庫(kù)常用方法_第3頁(yè)
EnterpriseLibrary20企業(yè)庫(kù)常用方法_第4頁(yè)
EnterpriseLibrary20企業(yè)庫(kù)常用方法_第5頁(yè)
已閱讀5頁(yè),還剩34頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、EnterpriseLibrary20企業(yè)庫(kù)常用方法下面我們重點(diǎn)來(lái)說(shuō)一下DataAccess Application Block操作數(shù)據(jù) 庫(kù)的方法。1、創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)實(shí)例DataBase創(chuàng)建一個(gè)默認(rèn)的數(shù)據(jù)庫(kù)實(shí)例Database defaultdb = DatabaseFactory.CreateDatabase();創(chuàng)建一個(gè)名為Connection String的數(shù)據(jù)庫(kù)實(shí)例Database db = DatabaseFactory.CreateDatabase(nConnectio n String");上面兩種創(chuàng)建數(shù)據(jù)庫(kù)實(shí)例的方法的數(shù)據(jù)庫(kù)能夠是任何類型的數(shù)據(jù) 庫(kù),取決于Config

2、文件中的配置信息下面的這種方面展現(xiàn)了創(chuàng)建一個(gè)SQL數(shù)據(jù)庫(kù)的實(shí)例需引用Micros oft.Practices.EnterpriseLibrary.Data.Sql 程序集SqlDatabase dbsql = DatabaseFactory.CreateDatabase(HCon nection String") as SqlDatabase;我們同樣也能夠不通過(guò)配置文件來(lái)創(chuàng)建數(shù)據(jù)庫(kù)實(shí)例,如下string connString = nserver=.;database=EnterpriseLibrary;uid =sa;pwd = "SqlDatabase nocofigd

3、b = new SqlDatabase(connString);2、創(chuàng)建 DbCommandDbCommand的創(chuàng)建方式有兩種:一種是為了調(diào)用儲(chǔ)備過(guò)程創(chuàng)建,一種是為了執(zhí)行一樣的SQL語(yǔ)句而創(chuàng)建,具體用哪一種方式,就要 看你需要執(zhí)行的是儲(chǔ)備過(guò)程依舊SQL語(yǔ)句了。創(chuàng)建一個(gè)SQL語(yǔ)句的DbCommandDatabase db = DatabaseFactory.CreateDatabase(); string sql = "Select * from person"DbCommand Sqldbcomm = db.GetSqlStringCommand(sql);創(chuàng)建一個(gè)儲(chǔ)備過(guò)程

4、的DbCommand 儲(chǔ)備過(guò)程名稱為GetAIIPersonByName string procName = "GetAIIPersonByName"DbCommand Procdbcomm = db.GetStoredProcCommand (procName);3、儲(chǔ)備過(guò)程中的參數(shù)處理DataBase類中包含多種傳遞儲(chǔ)備過(guò)程參數(shù)的方法,也包含了得到和 設(shè)置參數(shù)值的方法,如下:AddParameter,為儲(chǔ)備過(guò)程傳遞一個(gè)參數(shù)(輸入型或輸出型) AddlnParameter,為儲(chǔ)備過(guò)程傳遞一個(gè)輸入型參數(shù) AddOutParameter.為儲(chǔ)備過(guò)程傳遞一個(gè)輸出型參數(shù) GetP

5、arameterValue.獵取某儲(chǔ)備過(guò)程指定參數(shù)的值 SetParameterValue,為儲(chǔ)備過(guò)程的某個(gè)參數(shù)賦值,當(dāng)你需要用同一 個(gè)方法來(lái)執(zhí)行插入多條記錄時(shí),就可參數(shù)給參數(shù)賦值的方法來(lái)進(jìn)行 創(chuàng)建一個(gè)儲(chǔ)備過(guò)程的DbCommand 儲(chǔ)備過(guò)程名稱為GetAIIPersonByName string procName = "GetAIIPersonByName"DbCommand Procdbcomm = db.GetStoredProcCommand(p rocName);添加一個(gè)輸入型的參數(shù)db.AddlnParameteProcdbcomm, "sex"

6、;, DbType.String);添加一個(gè)輸出型的參數(shù)db.AddOutParameter(Procdbcomm/ "©name", DbType.Strin g,20);設(shè)置參數(shù)的值db.SetPararrieterValue(Procdbcomm/ nsex", "男");執(zhí)行儲(chǔ)備過(guò)程db.ExecuteNonQuery(Procdbcomm);得到輸出參數(shù)的值,注意轉(zhuǎn)化返回值類型string outvalue = (string) db.GetParameterValue(Procdbco mm, "name&quo

7、t;);4、執(zhí)行ExecuteReader方法返回一個(gè)IDataReader類型的數(shù)據(jù)集 因?yàn)镋xecuteReader方法在一開(kāi)始執(zhí)行時(shí)就打開(kāi)了一個(gè)與數(shù)據(jù)庫(kù)的 連接,因此我們必須注意在使用終止時(shí)關(guān)閉連接,而用using(用語(yǔ) 句塊就能確保ExecuteReader方法在執(zhí)行完畢時(shí)關(guān)閉與數(shù)據(jù)庫(kù)的連 接。什么時(shí)候使用此方法:返回的數(shù)據(jù)僅僅用來(lái)顯示,并不對(duì)其進(jìn)行修改,刪除等操作;綁定返回的數(shù)據(jù)到WebForm Control不需要緩存返回的數(shù)據(jù),用完就開(kāi)釋using (IDataReader reader = db.ExecuteReader(CommandTy pe.Text, "se

8、lect * from person11)DataGridl.DataSource = reader;DataGridl.DataBind();)5、執(zhí)行 ExecuteDataSet 方法返回一個(gè) DataSetpublic DataSet GetPersonList() (Database db = DatabaseFactory.CreateDatabase();DbCommand dbcomm = db.GetSqlStringCommand("select* from person");return db.ExecuteDataSet(dbcomm);)什么時(shí)候使

9、用此方法:需要返回多表數(shù)據(jù);假如你需要逐一訪問(wèn)數(shù)據(jù)庫(kù)的每一條記錄,那個(gè)時(shí)候假如使用Data Reader的話,則會(huì)使與數(shù)據(jù)庫(kù)的連接一直處于打開(kāi)的狀態(tài),長(zhǎng)此以 往,就會(huì)使應(yīng)用程序的性育濟(jì)口可伸縮性大大降低;需要和其他的應(yīng)用程序交換數(shù)據(jù);6、執(zhí)行 ExecuteNonQuery該方法返回的是SQL語(yǔ)句執(zhí)行阻礙的行數(shù),我們能夠利用該方法來(lái)執(zhí)行一些沒(méi)有返回i 的操作(Insert, U pdate, Delete) public void AddPersonQDatabase db = DatabaseFactory.CreateDatabaseQ;DbCommand dbcomm = db.GetS

10、qlStringCommand(Hinsert i nto person valuesQ'shy1,女1234561)db.ExecuteNonQuery(dbcomm);)7、執(zhí)行ExecuteScalar返回單值public string GetPersonNameQDatabase db = DatabaseFactory.CreateDatabase();DbCommand dbcomm = db.GetSqlStringCommand(Hselect name from person");return (string)db.ExecuteScalar(dbcomm)

11、; ) 8、事務(wù)處理public void UseTransactionQDatabase db = DatabaseFactory<reateDatabase();DbCommand dbcomml = db.GetSqlStringCommand(nupdat e person set name=,pw,n);DbCommand dbcomm2 = db.GetSqlStringCommand(ndelet e from person where id = lH);using (DbConnection conn = db.CreateConnectionQ)(打開(kāi)連接conn.Op

12、en();創(chuàng)建事務(wù)DbTransaction trans = conn.BeginTransaction();try(db.ExecuteNonQuery(dbcomml);db.ExecuteNonQuery(dbcomm2);都執(zhí)行成功則提交事務(wù)trans mit();)catch(Exception)(發(fā)生專門(mén),事務(wù)回滾trans.Rollback();)關(guān)閉連接conn.Close();)9、執(zhí)行 ExecuteXmlReader 返回 XML 數(shù)據(jù)支持Sql Server 2000及以后版本的數(shù)據(jù)庫(kù),對(duì)微軟以外的數(shù)據(jù)庫(kù) 應(yīng)該不支持。public void UseXMLReaderQS

13、qlDatabase dbSQL = DatabaseFactory.CreateDatabase(nCo nnection String'1) as SqlDatabase;/要返回XML數(shù)據(jù)需要在SQL語(yǔ)句后加FOR XML AUTO string sqlCommand = "SELECT ProductID, ProductName FROM Products FOR XML AUTO11;DbCommand dbCommand = dbSQL.GetSqIStringCommand (sqlCommand);XmlReader personReader = null;

14、StringBuilder personlist = new StringBuilderQ;trypersonReader = dbSQL.ExecuteXmlReader(dbCommand);/循環(huán)向XML中寫(xiě)入我們查詢得到的數(shù)據(jù)while (ipersonReader.EOF)if (personReader.IsStartElement() personlist.Append(personReader.ReadOuterXml();personlist.Append(Environment.NewLine);)finally(/ 關(guān)閉 Reader.if (personReader !

15、= null)(personReader.Close();)/關(guān)閉數(shù)據(jù)庫(kù)連接if (dbCommand.Connection != null)(dbCommand.Connection.Close();)10、用DataSet批量的添加,修改,刪除數(shù)據(jù)public void UpdateDataBase()(Database db = DatabaseFactory.CreateDatabase();DataSet personDataSet = new DataSet();string sqlCommand = "Select * from person11;DbCommand d

16、bCommand = db.GetSqlStringCommand(sql Command);string personTable = "person”;/得到初始化數(shù)據(jù)db.LoadDataSet(dbCommand, personDataSet, personTable);/得到未修改前的數(shù)據(jù)集DataTable table = personDataSet.TablespersonTable;/往DataSet中添加一行數(shù)據(jù)Data Row addedRow = table.Rows«Add(new object 18, "New person“,“男“,“6

17、54321” );/修改table.Rows0"ProductName" = "Modified product"/下面分別創(chuàng)建添加,修改,刪除的操作DbCommand insertCommand = db«GetStoredProcComm andC'AddPerson1');db.AddInParameter(insertCommand, "Name", DbType.String, “Name”,DataRowVersion.Current);db.AddInParameter(insertComman

18、d/ “Sex”,DbType.String, ” Sext DataRowVersion.Current);db.AddInParameter(insertCommand, "ID", DbType.Int32, "ID ”, DataRowVersion.Current);DbCommand deleteCommand = db.GetStoredProcComm and(HDeletePerson");db.AddInParameter(deleteCommand, "ID"Z DbType.Int32, "I D”,

19、DataRowVersion.Current);DbCommand updateCommand = db.GetStoredProcCom mandCpdatePerson");db.AddlnParameteupdateCommand, “Name”,DbType.Strin g, "Name: DataRowVersion.Current);db.AddInParameter(updateCommand, "Sex”,DbType.String, “Sex",DataRowVersion.Current);db.AddInParameter(inse

20、rtCommandf “ID”, DbType.Int32f "ID ",DataRowVersion.Current);/提交對(duì)DataSet的修改,并返回阻礙的行int rowsAffected = db<UpdateDataSet(productsDataSetf11 Productsinsertcommand, updateCommand, deleteCo mmand, Microsoft.Practices.EnterpriseLibrary.Data.Updat e Be havi o r.Standa rd);G->Enterprise Libr

21、ary2.0數(shù)據(jù)庫(kù)常用操作2 (不同版本的說(shuō)明)今天學(xué)習(xí)了 Enterprise Library2.0 的 Data Access Applicatio n Block z Data Access Application Block 提供了通用的數(shù)據(jù)訪問(wèn) 的功能,隨著2.0版本的推出有了專門(mén)大變化。俺就多寫(xiě)了對(duì)SQL 和ACCESS數(shù)據(jù)庫(kù)自由切換的一些代碼出來(lái)共享。先看完原文再接 俺的代碼吧。一改進(jìn)在DAAB1.1里面我們明白Database方法返回或者創(chuàng)建一個(gè)DB CommandWrapper 對(duì)象,而在 DAAB2.0 里面移除了 DBComma ndWrapper類,用ADO.NET2.

22、0里面的DBCommand類代替實(shí)現(xiàn) 類似的功能,如此使得DAAB跟我們的.NET類庫(kù)的結(jié)合更加緊密, 回憶一下我們?cè)?.1里面用DBCommandWrapper來(lái)訪問(wèn)數(shù)據(jù)時(shí)的 代碼:二.使用示例Database db = DatabaseFactory.CreateDatabase();DBCommandWrapper dbCommand = db.GetStoredPro cCommandWrapper(,GetProductsByCategory,);dbCommand.AddInParameter(,CategoryIDn/ DbType.Int 32, Category);DataS

23、et productDataSet = db.ExecuteDataSet(dbComm and);而用了新的DBCommand類之后則變成了 :Database db = DatabaseFactory.CreateDatabase();DbCommand dbCommand = db.GetStoredProcComma ndCGetProductsByCategory'1);db.AddInParameter(dbCommand/ "CategorylD", DbTyp e.Int32z Category);DataSet productDataSet = d

24、b.ExecuteDataSet(dbComm and);數(shù)據(jù)庫(kù)連接字符串在我們基于數(shù)據(jù)庫(kù)的開(kāi)發(fā)永久是少不了的,然 而在DAAB1.1下,它所使用的字符串跟我們?cè)?NET類庫(kù)中使用的連 接字符串卻是不能共享的,它們分別儲(chǔ)存在不同的位置。而在2.0的 Data Access Application Block 使用了 ADO.NET2.0 里面connectionStrings配置區(qū),如此帶來(lái)的一個(gè)好處是連接字符串能夠在 Application Block 和定義的.NET類之間共享使用該配置區(qū),如:< connectionstrings ><addname = >lDa

25、taAccessQuickStart>lproviderName=vvSystem.Data.SqlClientlvconnectionstring = "server= (local)SQLEXPRES S;database = EntLibQuickStarts;Integrated Security二true” /></connectionStrings>在.NET2.0下,泛型編程差不多成為了一個(gè)核心,而2.0版的D AAB中也新增了一個(gè)GenericDatabase對(duì)象。DAAB中盡管差不多 包含了 SqlDatabase和OrcaleDatabas

26、e ,然而假如我們需要使用 其他的像DB2等數(shù)據(jù)庫(kù)時(shí),就需要用到GenericDatabase ,它能 夠用于任HNET類庫(kù)中的數(shù)據(jù)提供者,包括OdbcProvider和Ole DbProvideroDAAB2.0的配置專門(mén)簡(jiǎn)單,要緊有以下幾方面的配置:配置連接字符串配置默認(rèn)數(shù)據(jù)庫(kù)添加相關(guān)的命名空間:using Microsoft.Practices.EnterpriseLibrary.Data;using System.Data;使用Data Access Application Block進(jìn)行數(shù)據(jù)的讀取和操作, 一樣分為三步:1 .創(chuàng)建Database對(duì)象2 .提供命令參數(shù),假如需要的話下

27、面分別看一下DataAccessQuickStart中提供的一些例子:執(zhí)行靜態(tài)的SQL語(yǔ)句public string GetCustomerList()(/創(chuàng)建Database對(duì)象Database db = DatabaseFactory.CreateDatabase();使用SQL語(yǔ)句創(chuàng)建DbCommand對(duì)象string sqlCommand = nSelect CustomerlD, Name, Addr ess, City, Country, PostalCode " +"From Customers1'DbCommand dbCommand = db.Ge

28、tSqIStringCommand (sqlCommand);StringBuilder readerData = new StringBuilder();/ 調(diào)用 ExecuteReader 方法using (IDataReader dataReader = db.ExecuteReader(dbC ommand)while (dataReader.ReadQ)/ Get the value of the 'Name' column in the Dat aReaderreaderData.Append(dataReader"Name");readerDa

29、ta.Append(Environment.NewLine);)return readerData.ToString();)執(zhí)行儲(chǔ)備過(guò)程并傳遞參數(shù),返回DataSetpublic DataSet GetProductsInCategory(int Category)(/ Create the Database object, using the default database service. The/ default database service is determined through c onfiguration.Database db = DatabaseFactory.Cre

30、ateDatabase();string sqlCommand = "GetProductsByCategory11;DbCommand dbCommand = db.GetStoredProcComm and(sqlCommand);/ Retrieve products from the specified category.db.AddInParameter(dbCommand, "CategorylD", DbTy pe.Int32/ Category);/ DataSet that will hold the returned resultsDataSe

31、t productsDataSet = null;productsDataSet = db.ExecuteDataSet(dbCommand);/ Note: connection was closed by ExecuteDataSet method callreturn productsDataSet;)利用DataSet更新數(shù)據(jù)public int UpdateProducts()/ Create the Database object, using the default dat abase service. The/ default database service is deter

32、mined through c onfiguration.Database db = DatabaseFactory.CreateDatabase();DataSet productsDataSet = new DataSet();string sqlCommand = "Select ProductID, ProductNa me, CategorylD, UnitPrice, LastUpdate " +"From Products"DbCommand dbCommand = db.GetSqIStringComma nd(sqlCommand);s

33、tring productsTable = "Products1"/ Retrieve the initial datadb.LoadDataSet(dbCommand, productsDataSet, productsTable);/ Get the table that will be modifiedDataTable table = productsDataSet.TablesproductsTa ble;/ Add a new product to existing DataSetDataRow addedRow = table.Rows.Add(new obj

34、ect DBNulLValue, "New product; 11, 25);/ Modify an existing producttable.Rows0nProductName" = "Modified product"/ Establish our Insert, Delete, and Update command sDbCommand insertCommand = db.GetStoredProcCo mmand("AddProductn);db.AddInParameter(insertCommand/ "Product

35、Name", DbType.String, "ProductName DataRowVersion.Current);db.AddlnParameteinsertCommand, "CategorylD", DbType.Int32, "CategorylD", DataRowVersion.Current);db.AddInParameter(insertCommandz "UnitPrice11, DbTy pe.Currency, "UnitPrice", DataRowVersion.Curren

36、t);DbCommand deleteCommand = db.GetStoredProcCo mmand("DeleteProduct");db.AddInParameter(deleteCommand, "ProductID", Db Type.Int32, "ProductID", DataRowVersion.Current);DbCommand updateCommand = db.GetStoredProcC ommand(" UpdateProduct");db.AddInParameter(upda

37、teCommand/ "ProductID", Db Type.Int32z "ProductlD", DataRowVersion.Current);db.AddInParameter(updateCommandz TroductName", DbType.String, TroductName DataRowVersion.Current);db.AddInParameter(updateCommandz nLastUpdate'1, D bType.DateTime, "LastUpdate", DataRow

38、Version.Current);/ Submit the DataSet, capturing the number of row s that were affectedint rowsAffected = db.UpdateDataSet(productsDataSet, "Products'1, insertCommand, updateCommand,deleteCommand, UpdateBehavior. Standard);return rows Affected;)通過(guò)ID獵取記錄詳細(xì)信息public string GetProductDetails(in

39、t productID)(/ Create the Database object, using the default dat abase service. The/ default database service is determined through c onfiguration.Database db = DatabaseFactory.CreateDatabase();string sqlCommand = "GetProductDetails"DbCommand dbCommand = db.GetStoredProcComm and(sqlCommand

40、);/ Add paramters / Input parameters can specify the input valuedb.AddlnParameterCdbCommand, TroductlD", DbTyp e.Int32, productID);/ Output parameters specify the size of the return datadb.AddOutRarameter(dbCommand, "ProductName", D bType.String, 50);db.AddOutParametedbCommand, "

41、UnitPrice", DbTy pe.Currency, 8);db.ExecuteNonQuery(dbCommand);/ Row of data is captured via output parametersstring results = string.Format(CultureInfo.CurrentCultu re0, 1, 2:C ”,db.GetRarameterValue(dbCommandz "ProductlD"),db.GetParameterValue(dbCommandz "ProductName"),db.

42、GetParameterValue(dbCommand/nUnitPricen);return results;)以XML格式返回?cái)?shù)據(jù)public string GetProductList()(/ Use a named database instance that refers to a S QL Server database.SqlDatabase dbSQL = DatabaseFactory.CreateDatabas e() as SqlDatabase;/ Use "FOR XML AUTO" to have SQL return XML d atastri

43、ng sqlCommand = "Select ProductID, ProductNa me, CategorylD, UnitPrice, LastUpdate " +"From Products FOR XML AUTO"DbCommand dbCommand = dbSQL.GetSqlStringCom mand(sqlCommand);XmlReader productsReader = null;StringBuilder productList = new StringBuilderQ;productsReader = dbSQL.Exe

44、cuteXmlReader(dbCommand);/ Iterate through the XmlReader and put the dat a into our results.while (iproductsReader.EOF)if (productsReader.IsStartElement()productList.Append (productsReader. ReadOuterXmlQ);productList.Append(Environment.NewLine);)finally(/ Close the Reader.if (productsReader != null)

45、(productsReader.Close();)/ Explicitly close the connection. The connection is not closed/ when the XmlReader is closed.if (dbCommand.Connection != null)dbCommand.Connection.CloseQ;)return productList.ToString();)使用事務(wù)public bool Transfer(int transaction Amount, int sourceA ccount, int destinationAcco

46、unt)(bool result = false;/ Create the Database object, using the default dat abase service. The/ default database service is determined through c onfiguration.Database db = DatabaseFactory.CreateDatabase();/ Two operations, one to credit an account, and on e to debit another/ account.string sqlComma

47、nd = "CreditAccount'1;DbCommand creditCommand = db.GetStoredProcCo mmand(sqlCommand);db.AddlnParametecreditCommand, "AccountID", Db Type.Int32, sourceAccount);db.AddInParameter(creditCommand, "Amount",DbTy pe.Int32f transactionAmount);sqlCommand = nDebitAccountn;DbComman

48、d debitCommand = db.GetStoredProcCo mmand(sqlCommand);db.AddlnParametedebitCommand, "AccountID"z DbT ype.Int32, destinationAccount);db.AddInParameter(debitCommandz "Amount”, DbTyp e.Int32, transactionAmount);using (DbConnection connection = db.CreateConnect ionQ)connection.Open();DbTr

49、ansaction transaction = connection.BeginTransaction();try(/ Credit the first accountdb.ExecuteNonQuery(creditCommandz transactio n);/ Debit the second accountdb.ExecuteNonQueryfdebitCommand, transaction);/ Commit the transactiontransaction mit();result = true;) catch Rollback transactiontransaction.

50、Rollback();)connection.Close();return result;)3 .常見(jiàn)功能1 .創(chuàng)建Database對(duì)象創(chuàng)建一個(gè)默認(rèn)的Database對(duì)象Database dbSvc = DatabaseFactory.CreateDatabase();默認(rèn)的數(shù)據(jù)庫(kù)在配置文件中:<dataConfiguration defaultDatabase ="DataAccessQuickS tartn />創(chuàng)建一個(gè)實(shí)例Database對(duì)象/ Use a named database instance that refers to an ar bitrary da

51、tabase type,/ which is determined by configuration information.Database myDb = DatabaseFactory.CreateDatabase(HDat aAccessQuickStart");創(chuàng)建一個(gè)具體的類型的數(shù)據(jù)庫(kù)對(duì)象/ Create a SQL database.SqlDatabase dbSQL = DatabaseFactory.CreateDatabase(" DataAccessQuickStartn) as SqlDatabase;2 .倉(cāng)(J建 DbCommand 對(duì)象靜態(tài)的SQ

52、L語(yǔ)句創(chuàng)建一個(gè)DbCommandDatabase db = DatabaseFactory.CreateDatabase();string sqlCommand = "Select CustomerlD, LastName, Fi rstName From Customers1,;DbCommand dbCommand = db.GetSqIStringCommand (sqlCommand);儲(chǔ)備過(guò)程創(chuàng)建一個(gè)DbCommandDatabase db = DatabaseFactory.CreateDatabase();DbCommand dbCommand = db.GetStor

53、edProcComman d("GetProductsByCategory");3 .治理對(duì)象當(dāng)連接對(duì)象打開(kāi)后,不需要再次連接Database db = DatabaseFactory.CreateDatabase();string sqlCommand = "Select ProductID, ProductName From Products11;DbCommand dbCommand = db.GetSqIStringCommand (sqlCommand);/ No need to open the connection; just make the ca

54、ll.DataSet customerDataSet = db.ExecuteDataSet(dbCom mand);使用Using及早開(kāi)釋對(duì)象Database db = DatabaseFactory.CreateDatabaseQ;DbCommand dbCommand = db.GetSqIStringCommand ("Select Name, Address From Customers");using (IDataReader dataReader = db.ExecuteReader(dbC ommand)(/ Process results4 .參數(shù)處理Da

55、tabase類提供了如下的方法,用于參數(shù)的處理:AddParameter.傳遞參數(shù)給儲(chǔ)備過(guò)程AddlnParameter.傳遞輸入?yún)?shù)給儲(chǔ)備過(guò)程AddOutParameter.傳遞輸出參數(shù)給儲(chǔ)備過(guò)程GetParameterValue.得到指定參數(shù)的值SetParameterValue.設(shè)定參數(shù)值使用示例如下:Database db = DatabaseFactory.CreateDatabaseQ;string sqlCommand = "GetProductDetails0;DbCommand dbCommand = db.GetStoredProcComman d(sqlComm

56、and);db.AddlnParametedbCommand, "ProductID", DbType.I nt32z 5);db.AddOutParameter(dbCommand, "ProductName", DbT ype.String, 50);db.AddOutParameter(dbCommand, "UnitPrice", DbType.Currency, 8);Database db = DatabaseFactory.CreateDatabase();DbCommand insertCommand = db.Get

57、StoredProcCom mand(" Add Product");db.AddInParameter(insertCommand, nProductName,/ Db Type.String, TroductName11, DataRowVersion.Current);db.AddInParameter(insertCommand/ "CategorylD11, DbTy pe.Int32, "CategoryID"z DataRowVersion.Current);db.AddlnParameteinsertCommand, '

58、'UnitPrice", DbType.Currency, ,UnitPrice,/ DataRowVersion.Current);4 .使用場(chǎng)景DAAB2.0是對(duì)ADO.NET2.0的補(bǔ)充,它承諾你使用相同的數(shù)據(jù) 訪問(wèn)代碼來(lái)支持不同的數(shù)據(jù)庫(kù),您通過(guò)改變配置文件就在不同的數(shù)據(jù) 庫(kù)之間切換。目前盡管只提供SQLServer和Oracle的支持,然而能 夠通過(guò) GenericDatabase 和 ADO.NET 2.0 下的 DbProviderFact。 ry對(duì)象來(lái)增加對(duì)其他數(shù)據(jù)庫(kù)的支持。假如想要編寫(xiě)出來(lái)的數(shù)據(jù)庫(kù)訪問(wèn) 程序具有更好的移植性,則DAAB2.0是一個(gè)不錯(cuò)的選擇,然

59、而假如 您想要針對(duì)特定數(shù)據(jù)庫(kù)的特性進(jìn)行編程,就要用ADO.NET 了。參考:Enterprise Libaray -January 2006 關(guān)心文檔及 QuickS tart好,看到那個(gè)地點(diǎn)俺應(yīng)該差不多明白得使用了,俺就動(dòng)手試一下 SQL和ACCESS數(shù)據(jù)庫(kù)自由切換的方法,因俺平常的適應(yīng)是使用vb. net寫(xiě)東西,因此只寫(xiě)出的代碼出來(lái),有愛(ài)好的自己改成C# 好了,看以下html代碼:<%. Page Language="VB" AutoEventWireup="false" CodeFile= "sql,aspx.vb" Inherits="sql" %><!DOCTYPE html PUBLIC "-/W3C/DTD XHTML 1.0 Tra nsitional/ENn ":/ /TR/xhtmll/DTD/xhtmll-tra nsitional.dtd"><html

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論