对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
来源:互联网 发布:mac系统有安卓模拟器吗 编辑:程序博客网 时间:2024/05/18 04:22
public class BookData : DataSet{ public BookData() { // // Create the tables in the dataset // BuildDataTables(); } private void BuildDataTables() { // // Create the Books table // DataTable table = new DataTable(BOOKS_TABLE); DataColumnCollection columns = table.Columns; columns.Add(PKID_FIELD, typeof(System.Int32)); columns.Add(TYPE_ID_FIELD, typeof(System.Int32)); columns.Add(PUBLISHER_ID_FIELD, typeof(System.Int32)); columns.Add(PUBLICATION_YEAR_FIELD, typeof(System.Int16)); columns.Add(ISBN_FIELD, typeof(System.String)); columns.Add(IMAGE_FILE_SPEC_FIELD, typeof(System.String)); columns.Add(TITLE_FIELD, typeof(System.String)); columns.Add(DESCRIPTION_FIELD, typeof(System.String)); columns.Add(UNIT_PRICE_FIELD, typeof(System.Decimal)); columns.Add(UNIT_COST_FIELD, typeof(System.Decimal)); columns.Add(ITEM_TYPE_FIELD, typeof(System.String)); columns.Add(PUBLISHER_NAME_FIELD, typeof(System.String)); this.Tables.Add(table); }………}
private SqlDataAdapter dsCommand;public BookData GetBookById(int bookId){ return FillBookData("GetBookById", "@BookId", bookId.ToString());}private BookData FillBookData(String commandText, String paramName, String paramValue){ if (dsCommand == null ) { throw new System.ObjectDisposedException( GetType().FullName ); } BookData data = new BookData(); SqlCommand command = dsCommand.SelectCommand; command.CommandText = commandText; command.CommandType = CommandType.StoredProcedure; // use stored proc for perf SqlParameter param = new SqlParameter(paramName, SqlDbType.NVarChar, 255); param.Value = paramValue; command.Parameters.Add(param); dsCommand.Fill(data); return data;}
public CustomerData GetCustomerByEmail(String emailAddress, String password){ // // Check preconditions // ApplicationAssert.CheckCondition(emailAddress != String.Empty, "Email address is required",ApplicationAssert.LineNumber); ApplicationAssert.CheckCondition(password != String.Empty, "Password is required", ApplicationAssert.LineNumber); // // Get the customer dataSet // CustomerData dataSet; using (DataAccess.Customers customersDataAccess = new DataAccess.Customers()) {dataSet = customersDataAccess.LoadCustomerByEmail(emailAddress); } // // Verify the customer's password // DataRowCollection rows = dataSet.Tables[CustomerData.CUSTOMERS_TABLE].Rows; if ( ( rows.Count == 1 ) && rows[0][CustomerData.PASSWORD_FIELD].Equals(password) ) { return dataSet; } else { return null; }}
dataSet = customersDataAccess.LoadCustomerByEmail(emailAddress);
public void RunProc(string procName, out SqlDataReader dataReader) {SqlCommand cmd = CreateCommand(procName, null);dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);}
public ProductResults[] GetList(string catid, int currentPage, int pageSize, ref int numResults) {numResults = 0;int index=0;SqlDataReader reader = GetList(catid);ProductResults[] results = new ProductResults[pageSize];// now loop through the list and pull out items of the specified pageint start = (int)((currentPage - 1) * pageSize);if (start <= 0) start = 1;// skip for (int i = 0; i < start - 1; i++) {if (reader.Read()) numResults++;}if (start > 1) reader.Read();// read the data we are interested inwhile (reader.Read()) {if (index < pageSize) {results[index] = new ProductResults();results[index].productid = reader.GetString(0);results[index].name = reader.GetString(1);index++;}numResults++; }reader.Close();// see if need to redim arrayif (index == pageSize)return results;else {// not a full page, redim arrayProductResults[] results2 = new ProductResults[index];Array.Copy(results, results2, index);return results2;}}
public class ProductResults {private string m_productid;private string m_name;// product propspublic string productid {get { return m_productid; }set { m_productid = value; } }public string name {get { return m_name; }set { m_name = value; } }}
- .NET PetShop和Duwamish简单介绍
- 结构简述
- Duwamish数据访问剖析
- .NET PetShop数据访问剖析
- 分析总结
public class BookData : DataSet{ public BookData() { // // Create the tables in the dataset // BuildDataTables(); } private void BuildDataTables() { // // Create the Books table // DataTable table = new DataTable(BOOKS_TABLE); DataColumnCollection columns = table.Columns; columns.Add(PKID_FIELD, typeof(System.Int32)); columns.Add(TYPE_ID_FIELD, typeof(System.Int32)); columns.Add(PUBLISHER_ID_FIELD, typeof(System.Int32)); columns.Add(PUBLICATION_YEAR_FIELD, typeof(System.Int16)); columns.Add(ISBN_FIELD, typeof(System.String)); columns.Add(IMAGE_FILE_SPEC_FIELD, typeof(System.String)); columns.Add(TITLE_FIELD, typeof(System.String)); columns.Add(DESCRIPTION_FIELD, typeof(System.String)); columns.Add(UNIT_PRICE_FIELD, typeof(System.Decimal)); columns.Add(UNIT_COST_FIELD, typeof(System.Decimal)); columns.Add(ITEM_TYPE_FIELD, typeof(System.String)); columns.Add(PUBLISHER_NAME_FIELD, typeof(System.String)); this.Tables.Add(table); }………}
private SqlDataAdapter dsCommand;public BookData GetBookById(int bookId){ return FillBookData("GetBookById", "@BookId", bookId.ToString());}private BookData FillBookData(String commandText, String paramName, String paramValue){ if (dsCommand == null ) { throw new System.ObjectDisposedException( GetType().FullName ); } BookData data = new BookData(); SqlCommand command = dsCommand.SelectCommand; command.CommandText = commandText; command.CommandType = CommandType.StoredProcedure; // use stored proc for perf SqlParameter param = new SqlParameter(paramName, SqlDbType.NVarChar, 255); param.Value = paramValue; command.Parameters.Add(param); dsCommand.Fill(data); return data;}
public CustomerData GetCustomerByEmail(String emailAddress, String password){ // // Check preconditions // ApplicationAssert.CheckCondition(emailAddress != String.Empty, "Email address is required",ApplicationAssert.LineNumber); ApplicationAssert.CheckCondition(password != String.Empty, "Password is required", ApplicationAssert.LineNumber); // // Get the customer dataSet // CustomerData dataSet; using (DataAccess.Customers customersDataAccess = new DataAccess.Customers()) {dataSet = customersDataAccess.LoadCustomerByEmail(emailAddress); } // // Verify the customer's password // DataRowCollection rows = dataSet.Tables[CustomerData.CUSTOMERS_TABLE].Rows; if ( ( rows.Count == 1 ) && rows[0][CustomerData.PASSWORD_FIELD].Equals(password) ) { return dataSet; } else { return null; }}
dataSet = customersDataAccess.LoadCustomerByEmail(emailAddress);
public void RunProc(string procName, out SqlDataReader dataReader) {SqlCommand cmd = CreateCommand(procName, null);dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);}
public ProductResults[] GetList(string catid, int currentPage, int pageSize, ref int numResults) {numResults = 0;int index=0;SqlDataReader reader = GetList(catid);ProductResults[] results = new ProductResults[pageSize];// now loop through the list and pull out items of the specified pageint start = (int)((currentPage - 1) * pageSize);if (start <= 0) start = 1;// skip for (int i = 0; i < start - 1; i++) {if (reader.Read()) numResults++;}if (start > 1) reader.Read();// read the data we are interested inwhile (reader.Read()) {if (index < pageSize) {results[index] = new ProductResults();results[index].productid = reader.GetString(0);results[index].name = reader.GetString(1);index++;}numResults++; }reader.Close();// see if need to redim arrayif (index == pageSize)return results;else {// not a full page, redim arrayProductResults[] results2 = new ProductResults[index];Array.Copy(results, results2, index);return results2;}}
public class ProductResults {private string m_productid;private string m_name;// product propspublic string productid {get { return m_productid; }set { m_productid = value; } }public string name {get { return m_name; }set { m_name = value; } }}
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
- 对比.Net PetShop和Duwamish来探讨Ado.Net的数据库编程模式
- 对比.Net PetShop和Duwamish来探讨Ado.Net的数据库编程模式
- 对比.Net PetShop和Duwamish来探讨Ado.Net的数据库编程模式(转载)
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式(转)
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式[转贴]
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式(转帖)
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
- 对比.Net PetShop和Duwamish来探讨Ado.Net的数据库编程模式
- 对比.Net PetShop和Duwamish来探讨Ado.Net的数据库编程模式
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
- 对比.Net PetShop和Duwamish来探讨Ado.Net的数据库编程模式
- StartService
- StartServiceCtrlDispatcher
- UnlockServiceDatabase
- 编写测试需求及测试用例
- [转]如果有那么一间学校
- 对比.NET PetShop和Duwamish来探讨Ado.NET的数据库编程模式
- Duwamish深入剖析-配置篇
- Duwamish深入剖析-结构篇
- 读《Design Patterns》
- tomcat5.0.27以上版本,在用lomboz+eclipse时,出现class loader exception的解决
- windows2000环境下的CVS建立
- Eclipse开发环境建立
- TX Text Control 11.0 本人最新破解 功能同word的编辑器 支持windows 任何版本 word所有功能 能用于BS CS开发
- 故乡的豆腐干