C# ADO.NET 数据库访问的测试类
1 using System; 2 using System.Data; 3 using System.Text; 4 5 namespace SQLServerAccess 6 { 7 /// <summary> 8 /// 数据表访问 9 /// </summary> 10 public class TableAccess : BaseAccess 11 { 12 /// <summary> 13 /// 构造函数 14 /// </summary> 15 /// <param name="connection"></param> 16 public TableAccess(DbConnection connection) 17 : base(connection) { } 18 19 /// <summary> 20 /// 构造函数 21 /// </summary> 22 /// <param name="connection"></param> 23 public TableAccess(IDbConnection connection) 24 : base(connection) { } 25 26 /// <summary> 27 /// 取得xxx记录数 28 /// </summary> 29 public int GetxxxCount(string aaa = null) 30 { 31 try 32 { 33 // Parameters置换 34 base.innerDBConnection.Command.Parameters.Clear(); 35 // SQL作成 36 StringBuilder sql = new StringBuilder(); 37 sql.Append("SELECT"); 38 sql.Append(" COUNT(*)"); 39 sql.Append(" FROM [ttt] WHERE [DeleteFlag] = 1"); 40 if (!string.IsNullOrEmpty(aaa)) 41 { 42 sql.Append(" AND aaa = @aaa"); 43 base.innerDBConnection.Command.Parameters.Add("@aaa", SqlDbType.NVarChar).Value = aaa; 44 } 45 // SQL执行 46 int resCount = Convert.ToInt32(base.Count(sql.ToString())); 47 return resCount; 48 } 49 catch (Exception ex) 50 { 51 throw ex; 52 } 53 } 54 55 /// <summary> 56 /// 取得xxx信息 57 /// </summary> 58 /// <returns></returns> 59 public DataTable Getxxx() 60 { 61 try 62 { 63 // SQL作成 64 StringBuilder sql = new StringBuilder(); 65 sql.Append("SELECT"); 66 sql.Append(" [aaa],"); 67 sql.Append(" [bbb]"); 68 sql.Append(" FROM [ttt]"); 69 sql.Append(" WHERE [aaa] = @aaa"); 70 // Parameters置换 71 base.innerDBCommand.Parameters.Clear(); 72 base.innerDBCommand.Parameters.Add("@aaa", SqlDbType.NVarChar).Value = 1111; 73 // SQL执行 74 DataTable result = base.Select(sql.ToString()).Tables[0]; 75 return result; 76 } 77 catch (Exception ex) 78 { 79 throw ex; 80 } 81 } 82 83 /// <summary> 84 /// 增删改xxx信息 85 /// </summary> 86 /// <returns></returns> 87 public int Updxxx(string aaa, string bbb) 88 { 89 try 90 { 91 // SQL作成 92 StringBuilder sql = new StringBuilder(); 93 sql.Append("UPDATE [ttt]"); 94 sql.Append(" SET aaa = @aaa"); 95 sql.Append(" WHERE [bbb] = @bbb"); 96 // Parameters置换 97 base.innerDBConnection.Command.Parameters.Clear(); 98 base.innerDBConnection.Command.Parameters.Add("@aaa", SqlDbType.NVarChar).Value = aaa; 99 base.innerDBConnection.Command.Parameters.Add("@bbb", SqlDbType.NVarChar).Value = bbb; 100 // SQL执行 101 int resCount = Convert.ToInt32(base.Execute(sql.ToString())); 102 return resCount; 103 } 104 catch (Exception ex) 105 { 106 throw ex; 107 } 108 } 109 } 110 }