1 public class DBUtil 2 { 3 ///4 /// 数据库连接字符串 5 /// 6 private static string DataBase_Connection = System.Configuration.ConfigurationManager.ConnectionStrings["DNT"].ToString(); 7 8 ///9 /// 脚本文件存放路径 10 /// 11 private static string Data_File_Path = System.Configuration.ConfigurationManager.AppSettings["Data_File_Path"].ToString(); 12 13 ///14 /// 全局配置文件列表 15 /// 16 public static DictionaryDataConfigList; 17 18 /// 19 /// 加载配置文件 20 /// 21 public static void InitConfig() 22 { 23 var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,Data_File_Path); 24 25 DirectoryInfo TheFolder = new DirectoryInfo(filePath); 26 foreach (FileInfo NextFile in TheFolder.GetFiles().Where(f=>f.Extension == ".config")) 27 { 28 Spring.Context.Support.XmlApplicationContext context = new Spring.Context.Support.XmlApplicationContext(NextFile.FullName); 29 string[] fileNames = context.ObjectFactory.GetObjectDefinitionNames(); 30 31 foreach (var fileName in fileNames) 32 { 33 var mObj = context.GetObject(fileName); 34 35 if (DataConfigList == null) 36 { 37 DataConfigList = new Dictionary(); 38 } 39 40 string keyName = NextFile.Name.Split('.')[0].ToString() + "." + fileName; 41 42 if (!DataConfigList.ContainsKey(keyName)) 43 { 44 DataConfigList.Add(keyName, mObj as DataConfig); 45 } 46 else 47 { 48 throw new ApplicationException("The existence of multiple identical configuration node,plseae check!"); 49 } 50 } 51 } 52 53 } 54 55 /// 56 /// 获取指定名称SQL 57 /// 58 /// 59 ///60 public static string GetSql(string commandName) 61 { 62 InitConfig(); 63 64 if (!DataConfigList.ContainsKey(commandName)) 65 { 66 throw new ApplicationException("Data file can't be found!"); 67 } 68 else 69 { 70 return DataConfigList[commandName].commandText; 71 } 72 73 } 74 75 /// 76 /// 获取数据库连接 77 /// 78 ///79 public static SqlConnection GetConnection() 80 { 81 SqlConnection connection = new SqlConnection(DataBase_Connection); 82 connection.Open(); 83 84 return connection; 85 } 86 87 /// 88 /// 获取Command 89 /// 90 /// 91 ///92 public static IDbCommand CreateCommand(string commandName) 93 { 94 var connection = GetConnection(); 95 96 using (IDbCommand command = connection.CreateCommand()) 97 { 98 command.CommandText = GetSql(commandName); 99 return command;100 }101 }102 103 /// 104 /// 执行查询动作105 /// 106 ///107 /// 108 /// 109 /// 110 public static List Query (string commandName, dynamic pars)111 {112 using (IDbCommand command = DBUtil.CreateCommand(commandName))113 {114 return SqlMapper.Query (command.Connection, command.CommandText, pars);115 }116 }117 }
SQL配置文件管理
- 支持多文件 (Order.config, Item.config....), DBUtil 会在调用时加配置文件加入内存中缓存,待下次时候,直接读取内存数据.
1 26 15 16 25
Demo代码
class Program { static void Main(string[] args) { ListOrderList = GetOrderList(1000); OrderList.ForEach(f => { Console.WriteLine(f.OrderNumber); }); Console.ReadLine(); } public static List GetOrderLIST(int size) { return DBUtil.Query ("Order.GetOrderList", new { Size = size }).ToList(); }
public static ListGetOrderDetail(int size,int orderId) { return DBUtil.Query ("Order.GetOrderDetail", new { Size = size,OrderID = orderId }).ToList(); }
}