Enterprise-Library20-企業(yè)庫常用方法_第1頁
Enterprise-Library20-企業(yè)庫常用方法_第2頁
Enterprise-Library20-企業(yè)庫常用方法_第3頁
Enterprise-Library20-企業(yè)庫常用方法_第4頁
Enterprise-Library20-企業(yè)庫常用方法_第5頁
已閱讀5頁,還剩28頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

/EnterpriseLibrary2.0--企業(yè)庫常用方法下面我們重點來說一下DataAccessApplicationBlock操作數(shù)據(jù)庫的方法。

1、創(chuàng)立一個數(shù)據(jù)庫實例DataBase

//創(chuàng)立一個默認的數(shù)據(jù)庫實例

Databasedefaultdb=DatabaseFactory.CreateDatabase();

//創(chuàng)立一個名為ConnectionString的數(shù)據(jù)庫實例

Databasedb=DatabaseFactory.CreateDatabase("ConnectionString");

//上面兩種創(chuàng)立數(shù)據(jù)庫實例的方法的數(shù)據(jù)庫可以是任何類型的數(shù)據(jù)庫,取決于Config文件中的配置信息

//下面的這種方面展示了創(chuàng)立一個SQL數(shù)據(jù)庫的實例,需引用Microsoft.Practices.EnterpriseLibrary.Data.Sql程序集

SqlDatabasedbsql=DatabaseFactory.CreateDatabase("ConnectionString")asSqlDatabase;

//我們同樣也可以不通過配置文件來創(chuàng)立數(shù)據(jù)庫實例,如下

stringconnString="server=.;database=EnterpriseLibrary;uid=sa;pwd=";

SqlDatabasenocofigdb=newSqlDatabase(connString);

2、創(chuàng)立DbCommand

DbCommand的創(chuàng)立方式有兩種:一種是為了調(diào)用存儲過程創(chuàng)立,一種是為了執(zhí)行一般的SQL語句而創(chuàng)立,具體用哪一種方式,就要看你需要執(zhí)行的是存儲過程還是SQL語句了。

//創(chuàng)立一個SQL語句的DbCommand

Databasedb=DatabaseFactory.CreateDatabase();

stringsql="Select*fromperson";

DbCommandSqldbcomm=db.GetSqlStringCommand(sql);

//創(chuàng)立一個存儲過程的DbCommand

//存儲過程名稱為GetAllPersonByName

stringprocName="GetAllPersonByName";

DbCommandProcdbcomm=db.GetStoredProcCommand(procName);

3、存儲過程中的參數(shù)處理

DataBase類中包含多種傳遞存儲過程參數(shù)的方法,也包含了得到和設置參數(shù)值的方法,如下:

AddParameter.為存儲過程傳遞一個參數(shù)(輸入型或輸出型)

AddInParameter.為存儲過程傳遞一個輸入型參數(shù)

AddOutParameter.為存儲過程傳遞一個輸出型參數(shù)

GetParameterValue.獲取某存儲過程指定參數(shù)的值

SetParameterValue.為存儲過程的某個參數(shù)賦值,當你需要用同一個方法來執(zhí)行插入多條記錄時,就可參數(shù)給參數(shù)賦值的方法來進行

//創(chuàng)立一個存儲過程的DbCommand

//存儲過程名稱為GetAllPersonByName

stringprocName="GetAllPersonByName";

DbCommandProcdbcomm=db.GetStoredProcCommand(procName);

//添加一個輸入型的參數(shù)

db.AddInParameter(Procdbcomm,"@sex",DbType.String);

//添加一個輸出型的參數(shù)

db.AddOutParameter(Procdbcomm,"@name",DbType.String,20);

//設置參數(shù)的值

db.SetParameterValue(Procdbcomm,"@sex","男");

//執(zhí)行存儲過程

db.ExecuteNonQuery(Procdbcomm);

//得到輸出參數(shù)的值,注意轉(zhuǎn)化返回值類型

stringoutvalue=(string)db.GetParameterValue(Procdbcomm,"@name");

4、執(zhí)行ExecuteReader方法返回一個IDataReader類型的數(shù)據(jù)集

因為ExecuteReader方法在一開始執(zhí)行時就翻開了一個與數(shù)據(jù)庫的連接,所以我們必須注意在使用結(jié)束時關閉連接,而用using(){}語句塊就能確保ExecuteReader方法在執(zhí)行完畢時關閉與數(shù)據(jù)庫的連接。

什么時候使用此方法:

返回的數(shù)據(jù)僅僅用來顯示,并不對其進行修改,刪除等操作;

綁定返回的數(shù)據(jù)到WebFormControl

不需要緩存返回的數(shù)據(jù),用完就釋放

using(IDataReaderreader=db.ExecuteReader(CommandType.Text,"select*fromperson"))

{

DataGrid1.DataSource=reader;

DataGrid1.DataBind();

}

5、執(zhí)行ExecuteDataSet方法返回一個DataSet

publicDataSetGetPersonList()

{

Databasedb=DatabaseFactory.CreateDatabase();

DbCommanddbcomm=db.GetSqlStringCommand("select*fromperson");

returndb.ExecuteDataSet(dbcomm);

}

什么時候使用此方法:

需要返回多表數(shù)據(jù);

如果你需要逐一訪問數(shù)據(jù)庫的每一條記錄,這個時候如果使用DataReader的話,則會使與數(shù)據(jù)庫的連接一直處于翻開的狀態(tài),長此以往,就會使應用程序的性能和可伸縮性大大降低;

需要和其他的應用程序交換數(shù)據(jù);

6、執(zhí)行ExecuteNonQuery

該方法返回的是SQL語句執(zhí)行影響的行數(shù),我們可以利用該方法來執(zhí)行一些沒有返回值的操作(Insert,Update,Delete)

publicvoidAddPerson()

{

Databasedb=DatabaseFactory.CreateDatabase();

DbCommanddbcomm=db.GetSqlStringCommand("insertintopersonvalues(1,'shy','女','123456')");

db.ExecuteNonQuery(dbcomm);

}

7、執(zhí)行ExecuteScalar返回單值

publicstringGetPersonName()

{

Databasedb=DatabaseFactory.CreateDatabase();

DbCommanddbcomm=db.GetSqlStringCommand("selectnamefromperson");

return(string)db.ExecuteScalar(dbcomm);

}

8、事務處理

publicvoidUseTransaction()

{

Databasedb=DatabaseFactory.CreateDatabase();

DbCommanddbcomm1=db.GetSqlStringCommand("updatepersonsetname='pw'");

DbCommanddbcomm2=db.GetSqlStringCommand("deletefrompersonwhereid=1");

using(DbConnectionconn=db.CreateConnection())

{

//翻開連接

conn.Open();

//創(chuàng)立事務

DbTransactiontrans=conn.BeginTransaction();

try

{

db.ExecuteNonQuery(dbcomm1);

db.ExecuteNonQuery(dbcomm2);

//都執(zhí)行成功則提交事務

trans.Commit();

}

catch(Exception)

{

//發(fā)生異常,事務回滾

trans.Rollback();

}

//關閉連接

conn.Close();

}

}

9、執(zhí)行ExecuteXmlReader返回XML數(shù)據(jù)

支持SqlServer2000及以后版本的數(shù)據(jù)庫,對微軟以外的數(shù)據(jù)庫應該不支持。

publicvoidUseXMLReader()

{

SqlDatabasedbSQL=DatabaseFactory.CreateDatabase("ConnectionString")asSqlDatabase;

//要返回XML數(shù)據(jù)需要在SQL語句后加FORXMLAUTO

stringsqlCommand="SELECTProductID,ProductNameFROMProductsFORXMLAUTO";

DbCommanddbCommand=dbSQL.GetSqlStringCommand(sqlCommand);

XmlReaderpersonReader=null;

StringBuilderpersonlist=newStringBuilder();

try

{

personReader=dbSQL.ExecuteXmlReader(dbCommand);

//循環(huán)向XML中寫入我們查詢得到的數(shù)據(jù)

while(!personReader.EOF)

{

if(personReader.IsStartElement())

{

personlist.Append(personReader.ReadOuterXml());

personlist.Append(Environment.NewLine);

}

}

}

finally

{

//關閉Reader.

if(personReader!=null)

{

personReader.Close();

}

//關閉數(shù)據(jù)庫連接

if(dbCommand.Connection!=null)

{

dbCommand.Connection.Close();

}

}

}

10、用DataSet批量的添加,修改,刪除數(shù)據(jù)

publicvoidUpdateDataBase()

{

Databasedb=DatabaseFactory.CreateDatabase();

DataSetpersonDataSet=newDataSet();

stringsqlCommand="Select*fromperson";

DbCommanddbCommand=db.GetSqlStringCommand(sqlCommand);

stringpersonTable="person";

//得到初始化數(shù)據(jù)

db.LoadDataSet(dbCommand,personDataSet,personTable);

//得到未修改前的數(shù)據(jù)集

DataTabletable=personDataSet.Tables[personTable];

//往DataSet中添加一行數(shù)據(jù)

DataRowaddedRow=table.Rows.Add(newobject[]{18,"Newperson","男","654321"});

//修改

table.Rows[0]["ProductName"]="Modifiedproduct";

//下面分別創(chuàng)立添加,修改,刪除的操作

DbCommandinsertCommand=db.GetStoredProcCommand("AddPerson");

db.AddInParameter(insertCommand,"Name",DbType.String,"Name",DataRowVersion.Current);

db.AddInParameter(insertCommand,"Sex",DbType.String,"Sex",DataRowVersion.Current);

db.AddInParameter(insertCommand,"ID",DbType.Int32,"ID",DataRowVersion.Current);

DbCommanddeleteCommand=db.GetStoredProcCommand("DeletePerson");

db.AddInParameter(deleteCommand,"ID",DbType.Int32,"ID",DataRowVersion.Current);

DbCommandupdateCommand=db.GetStoredProcCommand("UpdatePerson");

db.AddInParameter(updateCommand,"Name",DbType.String,"Name",DataRowVersion.Current);

db.AddInParameter(updateCommand,"Sex",DbType.String,"Sex",DataRowVersion.Current);

db.AddInParameter(insertCommand,"ID",DbType.Int32,"ID",DataRowVersion.Current);

//提交對DataSet的修改,并返回影響的行數(shù)

introwsAffected=db.UpdateDataSet(productsDataSet,"Products",insertCommand,updateCommand,deleteCommand,Microsoft.Practices.EnterpriseLibrary.Data.UpdateBehavior.Standard);

}//-->EnterpriseLibrary2.0數(shù)據(jù)庫常用操作2(不同版本的解釋)今天學習了EnterpriseLibrary2.0的DataAccessApplicationBlock,DataAccessApplicationBlock提供了通用的數(shù)據(jù)訪問的功能,隨著2.0版本的推出有了很大變化。俺就多寫了對SQL和ACCESS數(shù)據(jù)庫自由切換的一些代碼出來共享。先看完原文再接俺的代碼吧。一.改良在DAAB1.1里面我們知道Database方法返回或者創(chuàng)立一個DBCommandWrapper對象,而在DAAB2.0里面移除了DBCommandWrapper類,用ADO.NET2.0里面的DBCommand類代替實現(xiàn)類似的功能,這樣使得DAAB跟我們的.NET類庫的結(jié)合更加緊密,回憶一下我們在1.1里面用DBCommandWrapper來訪問數(shù)據(jù)時的代碼:二.使用例如Databasedb=DatabaseFactory.CreateDatabase();DBCommandWrapperdbCommand=db.GetStoredProcCommandWrapper("GetProductsByCategory");dbCommand.AddInParameter("CategoryID",DbType.Int32,Category);DataSetproductDataSet=db.ExecuteDataSet(dbCommand);而用了新的DBCommand類之后則變成了:Databasedb=DatabaseFactory.CreateDatabase();DbCommanddbCommand=db.GetStoredProcCommand("GetProductsByCategory");db.AddInParameter(dbCommand,"CategoryID",DbType.Int32,Category);DataSetproductDataSet=db.ExecuteDataSet(dbCommand);數(shù)據(jù)庫連接字符串在我們基于數(shù)據(jù)庫的開發(fā)永遠是少不了的,但是在DAAB1.1下,它所使用的字符串跟我們在.NET類庫中使用的連接字符串卻是不能共享的,它們分別保存在不同的位置。而在2.0的DataAccessApplicationBlock使用了ADO.NET2.0里面<connectionStrings>配置區(qū),這樣帶來的一個好處是連接字符串可以在ApplicationBlock和自定義的.NET類之間共享使用該配置區(qū),如:<connectionStrings>

<add

name="DataAccessQuickStart"

providerName="System.Data.SqlClient"

connectionString="server=(local)\SQLEXPRESS;database=EntLibQuickStarts;IntegratedSecurity=true"/></connectionStrings>在.NET2.0下,泛型編程已經(jīng)成為了一個核心,而2.0版的DAAB中也新增了一個GenericDatabase對象。DAAB中雖然已經(jīng)包含了SqlDatabase和OrcaleDatabase,但是如果我們需要使用其他的像DB2等數(shù)據(jù)庫時,就需要用到GenericDatabase,它可以用于任何.NET類庫中的數(shù)據(jù)提供者,包括OdbcProvider和OleDbProvider。DAAB2.0的配置非常簡單,主要有以下幾方面的配置:配置連接字符串配置默認數(shù)據(jù)庫添加相關的命名空間:usingMicrosoft.Practices.EnterpriseLibrary.Data;usingSystem.Data;使用DataAccessApplicationBlock進行數(shù)據(jù)的讀取和操作,一般分為三步:1.創(chuàng)立Database對象2.提供命令參數(shù),如果需要的話3.執(zhí)行命令下面分別看一下DataAccessQuickStart中提供的一些例子:執(zhí)行靜態(tài)的SQL語句publicstringGetCustomerList(){//創(chuàng)立Database對象Databasedb=DatabaseFactory.CreateDatabase();//使用SQL語句創(chuàng)立DbCommand對象stringsqlCommand="SelectCustomerID,Name,Address,City,Country,PostalCode"+

"FromCustomers";DbCommanddbCommand=db.GetSqlStringCommand(sqlCommand);StringBuilderreaderData=newStringBuilder();//調(diào)用ExecuteReader方法using(IDataReaderdataReader=db.ExecuteReader(dbCommand)){

while(dataReader.Read())

{

//Getthevalueofthe'Name'columnintheDataReader

readerData.Append(dataReader["Name"]);

readerData.Append(Environment.NewLine);

}}returnreaderData.ToString();}執(zhí)行存儲過程并傳遞參數(shù),返回DataSetpublicDataSetGetProductsInCategory(intCategory){

//CreatetheDatabaseobject,usingthedefaultdatabaseservice.The

//defaultdatabaseserviceisdeterminedthroughconfiguration.

Databasedb=DatabaseFactory.CreateDatabase();

stringsqlCommand="GetProductsByCategory";

DbCommanddbCommand=db.GetStoredProcCommand(sqlCommand);

//Retrieveproductsfromthespecifiedcategory.

db.AddInParameter(dbCommand,"CategoryID",DbType.Int32,Category);

//DataSetthatwillholdthereturnedresults

DataSetproductsDataSet=null;

productsDataSet=db.ExecuteDataSet(dbCommand);

//Note:connectionwasclosedbyExecuteDataSetmethodcall

returnproductsDataSet;}利用DataSet更新數(shù)據(jù)publicintUpdateProducts(){

//CreatetheDatabaseobject,usingthedefaultdatabaseservice.The

//defaultdatabaseserviceisdeterminedthroughconfiguration.

Databasedb=DatabaseFactory.CreateDatabase();

DataSetproductsDataSet=newDataSet();

stringsqlCommand="SelectProductID,ProductName,CategoryID,UnitPrice,LastUpdate"+

"FromProducts";

DbCommanddbCommand=db.GetSqlStringCommand(sqlCommand);

stringproductsTable="Products";

//Retrievetheinitialdata

db.LoadDataSet(dbCommand,productsDataSet,productsTable);

//Getthetablethatwillbemodified

DataTabletable=productsDataSet.Tables[productsTable];

//AddanewproducttoexistingDataSet

DataRowaddedRow=table.Rows.Add(newobject[]{DBNull.Value,"Newproduct",11,25});

//Modifyanexistingproduct

table.Rows[0]["ProductName"]="Modifiedproduct";

//EstablishourInsert,Delete,andUpdatecommands

DbCommandinsertCommand=db.GetStoredProcCommand("AddProduct");

db.AddInParameter(insertCommand,"ProductName",DbType.String,"ProductName",DataRowVersion.Current);

db.AddInParameter(insertCommand,"CategoryID",DbType.Int32,"CategoryID",DataRowVersion.Current);

db.AddInParameter(insertCommand,"UnitPrice",DbType.Currency,"UnitPrice",DataRowVersion.Current);

DbCommanddeleteCommand=db.GetStoredProcCommand("DeleteProduct");

db.AddInParameter(deleteCommand,"ProductID",DbType.Int32,"ProductID",DataRowVersion.Current);

DbCommandupdateCommand=db.GetStoredProcCommand("UpdateProduct");

db.AddInParameter(updateCommand,"ProductID",DbType.Int32,"ProductID",DataRowVersion.Current);

db.AddInParameter(updateCommand,"ProductName",DbType.String,"ProductName",DataRowVersion.Current);

db.AddInParameter(updateCommand,"LastUpdate",DbType.DateTime,"LastUpdate",DataRowVersion.Current);

//SubmittheDataSet,capturingthenumberofrowsthatwereaffected

introwsAffected=db.UpdateDataSet(productsDataSet,"Products",insertCommand,updateCommand,

deleteCommand,UpdateBehavior.Standard);

returnrowsAffected;}通過ID獲取記錄詳細信息publicstringGetProductDetails(intproductID){

//CreatetheDatabaseobject,usingthedefaultdatabaseservice.The

//defaultdatabaseserviceisdeterminedthroughconfiguration.

Databasedb=DatabaseFactory.CreateDatabase();

stringsqlCommand="GetProductDetails";

DbCommanddbCommand=db.GetStoredProcCommand(sqlCommand);

//Addparamters

//Inputparameterscanspecifytheinputvalue

db.AddInParameter(dbCommand,"ProductID",DbType.Int32,productID);

//Outputparametersspecifythesizeofthereturndata

db.AddOutParameter(dbCommand,"ProductName",DbType.String,50);

db.AddOutParameter(dbCommand,"UnitPrice",DbType.Currency,8);

db.ExecuteNonQuery(dbCommand);

//Rowofdataiscapturedviaoutputparameters

stringresults=string.Format(CultureInfo.CurrentCulture,"{0},{1},{2:C}",

db.GetParameterValue(dbCommand,"ProductID"),

db.GetParameterValue(dbCommand,"ProductName"),

db.GetParameterValue(dbCommand,"UnitPrice"));

returnresults;}以XML格式返回數(shù)據(jù)publicstringGetProductList(){

//UseanameddatabaseinstancethatreferstoaSQLServerdatabase.

SqlDatabasedbSQL=DatabaseFactory.CreateDatabase()asSqlDatabase;

//Use"FORXMLAUTO"tohaveSQLreturnXMLdata

stringsqlCommand="SelectProductID,ProductName,CategoryID,UnitPrice,LastUpdate"+

"FromProductsFORXMLAUTO";

DbCommanddbCommand=dbSQL.GetSqlStringCommand(sqlCommand);

XmlReaderproductsReader=null;

StringBuilderproductList=newStringBuilder();

try

{

productsReader=dbSQL.ExecuteXmlReader(dbCommand);

//IteratethroughtheXmlReaderandputthedataintoourresults.

while(!productsReader.EOF)

{

if(productsReader.IsStartElement())

{

productList.Append(productsReader.ReadOuterXml());

productList.Append(Environment.NewLine);

}

}

}

finally

{

//ClosetheReader.

if(productsReader!=null)

{

productsReader.Close();

}

//Explicitlyclosetheconnection.Theconnectionisnotclosed

//whentheXmlReaderisclosed.

if(dbCommand.Connection!=null)

{

dbCommand.Connection.Close();

}

}

returnproductList.ToString();}使用事務publicboolTransfer(inttransactionAmount,intsourceAccount,intdestinationAccount){

boolresult=false;

//CreatetheDatabaseobject,usingthedefaultdatabaseservice.The

//defaultdatabaseserviceisdeterminedthroughconfiguration.

Databasedb=DatabaseFactory.CreateDatabase();

//Twooperations,onetocreditanaccount,andonetodebitanother

//account.

stringsqlCommand="CreditAccount";

DbCommandcreditCommand=db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(creditCommand,"AccountID",DbType.Int32,sourceAccount);

db.AddInParameter(creditCommand,"Amount",DbType.Int32,transactionAmount);

sqlCommand="DebitAccount";

DbCommanddebitCommand=db.GetStoredProcCommand(sqlCommand);

db.AddInParameter(debitCommand,"AccountID",DbType.Int32,destinationAccount);

db.AddInParameter(debitCommand,"Amount",DbType.Int32,transactionAmount);

using(DbConnectionconnection=db.CreateConnection())

{

connection.Open();

DbTransactiontransaction=connection.BeginTransaction();

try

{

//Creditthefirstaccount

db.ExecuteNonQuery(creditCommand,transaction);

//Debitthesecondaccount

db.ExecuteNonQuery(debitCommand,transaction);

//Committhetransaction

transaction.Commit();

result=true;

}

catch

{

//Rollbacktransaction

transaction.Rollback();

}

connection.Close();

returnresult;

}}三.常見功能1.創(chuàng)立Database對象創(chuàng)立一個默認的Database對象DatabasedbSvc=DatabaseFactory.CreateDatabase();默認的數(shù)據(jù)庫在配置文件中:<dataConfigurationdefaultDatabase="DataAccessQuickStart"/>創(chuàng)立一個實例Database對象//Useanameddatabaseinstancethatreferstoanarbitrarydatabasetype,//whichisdeterminedbyconfigurationinformation.DatabasemyDb=DatabaseFactory.CreateDatabase("DataAccessQuickStart");創(chuàng)立一個具體的類型的數(shù)據(jù)庫對象//CreateaSQLdatabase.SqlDatabasedbSQL=DatabaseFactory.CreateDatabase("DataAccessQuickStart")asSqlDatabase;2.創(chuàng)立DbCommand對象靜態(tài)的SQL語句創(chuàng)立一個DbCommandDatabasedb=DatabaseFactory.CreateDatabase();stringsqlCommand="SelectCustomerID,LastName,FirstNameFromCustomers";DbCommanddbCommand=db.GetSqlStringCommand(sqlCommand);存儲過程創(chuàng)立一個DbCommandDatabasedb=DatabaseFactory.CreateDatabase();DbCommanddbCommand=db.GetStoredProcCommand("GetProductsByCategory");3.管理對象當連接對象翻開后,不需要再次連接Databasedb=DatabaseFactory.CreateDatabase();stringsqlCommand="SelectProductID,ProductNameFromProducts";DbCommanddbCommand=db.GetSqlStringCommand(sqlCommand);//Noneedtoopentheconnection;justmakethecall.DataSetcustomerDataSet=db.ExecuteDataSet(dbCommand);使用Using及早釋放對象Databasedb=DatabaseFactory.CreateDatabase();DbCommanddbCommand=db.GetSqlStringCommand("SelectName,AddressFromCustomers");using(IDataReaderdataReader=db.ExecuteReader(dbCommand)){//Processresults}4.參數(shù)處理Database類提供了如下的方法,用于參數(shù)的處理:AddParameter.傳遞參數(shù)給存儲過程AddInParameter.傳遞輸入?yún)?shù)給存儲過程AddOutParameter.傳遞輸出參數(shù)給存儲過程GetParameterValue.得到指定參數(shù)的值SetParameterValue.設定參數(shù)值使用例如如下:Databasedb=DatabaseFactory.CreateDatabase();stringsqlCommand="GetProductDetails";DbCommanddbCommand=db.GetStoredProcCommand(sqlCommand);db.AddInParameter(dbCommand,"ProductID",DbType.Int32,5);db.AddOutParameter(dbCommand,"ProductName",DbType.String,50);db.AddOutParameter(dbCommand,"UnitPrice",DbType.Currency,8);Databasedb=DatabaseFactory.CreateDatabase();DbCommandinsertCommand=db.GetStoredProcCommand("AddProduct");db.AddInParameter(insertCommand,"ProductName",DbType.String,"ProductName",DataRowVersion.Current);db.AddInParameter(insertCommand,"CategoryID",DbType.Int32,"CategoryID",DataRowVersion.Current);db.AddInParameter(insertCommand,"UnitPrice",DbType.Currency,"UnitPrice",DataRowVersion.Current);四.使用場景DAAB2.0是對ADO.NET2.0的補充,它允許你使用相同的數(shù)據(jù)訪問代碼來支持不同的數(shù)據(jù)庫,您通過改變配置文件就在不同的數(shù)據(jù)庫之間切換。目前雖然只提供SQLServer和Oracle的支持,但是可以通過GenericDatabase和ADO.NET2.0下的DbProviderFactory對象來增加對其他數(shù)據(jù)庫的支持。如果想要編寫出來的數(shù)據(jù)庫訪問程序具有更好的移植性,則DAAB2.0是一個不錯的選擇,但是如果您想要針對特定數(shù)據(jù)庫的特性進行編程,就要用ADO.NET了。參考:EnterpriseLibaray–January2006幫助文檔及QuickStart好,看到這里俺應該根本懂得使用了,俺就動手試一下SQL和ACCESS數(shù)據(jù)庫自由切換的方法,因俺平時的習慣是使用寫東西,所以只寫出的代碼出來,有興趣的自己改成C#好了,看以下html代碼:<%...@PageLanguage="VB"AutoEventWireup="false"CodeFile="sql.aspx.vb"Inherits="sql"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"""><html

溫馨提示

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

最新文檔

評論

0/150

提交評論