1 using System; 2 using System.Data; 3 using System.Data.SqlClient; 4 using System.Collections; 5 6 namespace Product 7 { 8 /// <summary> 9 /// DigitDataProvider 的摘要说明 10 /// </summary> 11 /// 创 建 人: Aero 12 /// 创建日期: 2006-3-22 13 /// 修 改 人: 14 /// 修改日期: 15 /// 修改内容: 16 /// 版 本: 17 public class DigitDataProvider 18 { 19 /// <summary> 20 /// 定义数据库连接 21 /// </summary> 22 private SqlConnection _dbConn; 23 public SqlConnection Connection 24 { 25 get { return this._dbConn; } 26 set { this._dbConn = value; } 27 } 28 29 #region 构造函数 30 /// <summary> 31 /// 默认无参构造函数 32 /// </summary> 33 /// 创 建 人: Aero 34 /// 创建日期: 2006-3-22 35 /// 修 改 人: 36 /// 修改日期: 37 /// 修改内容: 38 public DigitDataProvider() 39 { 40 // 41 // TODO: 在此处添加构造函数逻辑 42 // 43 } 44 45 public DigitDataProvider(SqlConnection conn) 46 { 47 this._dbConn = conn; 48 } 49 50 #endregion 51 52 #region 成员函数定义 53 54 /// <summary> 55 /// retrieve all Digits in the database 56 /// </summary> 57 /// <returns></returns> 58 public ArrayList GetAllDigits() 59 { 60 // retrieve all digit record in database 61 SqlCommand command = this._dbConn.CreateCommand(); 62 command.CommandText = "SELECT * FROM digits"; 63 SqlDataAdapter adapter = new SqlDataAdapter(command); 64 DataSet results = new DataSet(); 65 adapter.Fill(results); 66 67 // convert rows to digits collection 68 ArrayList digits = null; 69 70 if (results != null && results.Tables.Count > 0) 71 { 72 DataTable table = results.Tables[0]; 73 digits = new ArrayList(table.Rows.Count); 74 75 foreach (DataRow row in table.Rows) 76 { 77 digits.Add(new Digit(row)); 78 } 79 } 80 81 return digits; 82 } 83 84 /// <summary> 85 /// remove all digits from the database 86 /// </summary> 87 /// <returns></returns> 88 public int RemoveAllDigits() 89 { 90 // retrieve all digit record in database 91 SqlCommand command = this._dbConn.CreateCommand(); 92 command.CommandText = "DELETE FROM digits"; 93 94 return command.ExecuteNonQuery(); 95 } 96 97 /// <summary> 98 /// retrieve and return the entity of given value 99 /// </summary> 100 /// <exception cref="System.NullReferenceException">entity not exist in the database</exception> 101 /// <param name="value"></param> 102 /// <returns></returns> 103 public Digit GetDigit(int value) 104 { 105 // retrieve entity of given value 106 SqlCommand command = this._dbConn.CreateCommand(); 107 command.CommandText = "SELECT * FROM digits WHERE Value='" + value + "'"; 108 SqlDataAdapter adapter = new SqlDataAdapter(command); 109 DataSet results = new DataSet(); 110 adapter.Fill(results); 111 112 // convert rows to digits collection 113 Digit digit = null; 114 115 if (results != null && results.Tables.Count > 0 116 && results.Tables[0].Rows.Count > 0) 117 { 118 digit = new Digit(results.Tables[0].Rows[0]); 119 } 120 else 121 { 122 throw new NullReferenceException("not exists entity of given value"); 123 } 124 125 return digit; 126 } 127 128 /// <summary> 129 /// remove prime digits from database 130 /// </summary> 131 /// <returns></returns> 132 public int RemovePrimeDigits() 133 { 134 throw new NotImplementedException(); 135 } 136 137 #endregion 138 } 139 } 140 3)新建测试数据库: CREATE TABLE [dbo].[digits] ( [DigitID] [uniqueidentifier] NOT NULL , [Value] [int] NOT NULL ) ON [PRIMARY] GO |