public static void GetDataSource(CompositeTag dataSourceTag) { foreach (CompositeTag childConfig in dataSourceTag.SubTags) { DataSourceConfig dsConfig = new DataSourceConfig(); String id = childConfig.TryGetAttributeValueAt("id"); dsConfig.dataSourceName = id; dsConfig.url = GetTagValue(childConfig, "connectionString"); dsConfig.dialectClass = GetTagValue(childConfig, "dialectClass"); dsConfig.providerName = GetTagValue(childConfig, "providerName"); String tempValue = null; tempValue = GetTagValue(childConfig, "minSize"); if (tempValue != null) { dsConfig.minSize = Convert.ToInt32(tempValue); } tempValue = GetTagValue(childConfig, "maxSize"); if (tempValue != null) { dsConfig.maxSize = Convert.ToInt32(tempValue); } tempValue = GetTagValue(childConfig, "timeOut"); if (tempValue != null) { dsConfig.timeOut = Convert.ToInt32(tempValue); } tempValue = GetTagValue(childConfig, "usePool"); if (tempValue != null) { dsConfig.pooled = Convert.ToBoolean(tempValue); } if (DataSourceList.ContainsKey(id)) { DataSourceList[id] = dsConfig; } else { DataSourceList.Add(id, dsConfig); } } }
/// <summary> /// Create DBHelper /// </summary> /// <param name="dsName"></param> /// <param name="dsConfig"></param> static void CreateDBHelper(string dsName, DataSourceConfig dsConfig) { if (string.IsNullOrEmpty(dsConfig.dialectClass)) { throw new ArgumentNullException("配置文件错误:请检查[dsConfig]配置"); } try { string[] classNameArray = dsConfig.dialectClass.Split(new char[] { ':', '-' }); string className = classNameArray[1]; string assemblyName = classNameArray[0]; Type type = null; type = Assembly.Load(assemblyName).GetType(className, true); IDBHelper instance = Activator.CreateInstance(type, dsConfig.Parameters) as IDBHelper; DBHelperCache.Add(dsName, instance); } catch (ArgumentNullException) { throw new Exception("DBHelper添加失败:数据源名[dsName]不能为空"); } catch (ArgumentException) { throw new Exception("DBHelper添加失败:已存在同名的数据源"); } }
/// <summary> /// Init DataSource List; /// Init DBHelper Cache /// </summary> /// <param name="xnl"></param> /// <returns></returns> private static Dictionary<string, DataSourceConfig> InitDS(XmlNodeList xnl) { System.Collections.Generic.Dictionary<string, DataSourceConfig> dsTable = new Dictionary<string, DataSourceConfig>(); try { foreach (XmlNode xl in xnl) { DataSourceConfig dsConfig = new DataSourceConfig(); string dsName = xl.Attributes["name"].Value; string dialect = xl.Attributes["dialect"].Value; dsConfig.dataSourceName = dsName; dsConfig.dialectClass = dialect; XmlNodeList parmNodeList = xl.SelectNodes("parm"); int i = 0; object[] parmList = new object[parmNodeList.Count]; foreach (XmlNode parmNode in parmNodeList) { //参数个数要与构造函数对应 否则要进行转换 string id = parmNode.Attributes["id"].Value; string value = parmNode.Attributes["value"].Value; parmList[i] = value; i++; } dsConfig.Parameters = parmList; dsTable.Add(dsName, dsConfig); //添加到DBHelperCache string[] classNameArray = dsConfig.dialectClass.Split(new char[] { ':', '-' }); string className = classNameArray[1]; string assemblyName = classNameArray[0]; Type type = null; if (string.IsNullOrEmpty(dsConfig.dialectClass)) { throw new ArgumentNullException("配置文件错误:请检查Dialect配置"); } try { type = Assembly.Load(assemblyName).GetType(className, true); IDBHelper instance = Activator.CreateInstance(type, dsConfig.Parameters) as IDBHelper; DBHelperCache.Add(dsName, instance); } catch (Exception) { //如果Assembly.Load失败,不做处理 } } } catch (Exception ex) { throw new DBConfigException(ex); } return dsTable; }
private void button1_Click(object sender, EventArgs e) { XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.Load(AppDomain.CurrentDomain.BaseDirectory + "DBHelper.config"); } catch { throw new DBFileNotFoundException(); } System.Collections.Generic.Dictionary<string, DataSourceConfig> dsTable = new Dictionary<string, DataSourceConfig>(); XmlNodeList xnl = xmlDoc.DocumentElement.SelectNodes("DataSource"); try { foreach (XmlNode xl in xnl) { DataSourceConfig dsConfig = new DataSourceConfig(); string dsName = xl.Attributes["name"].Value; string dialect = xl.Attributes["dialect"].Value; dsConfig.dataSourceName = dsName; dsConfig.dialectClass = dialect; XmlNodeList parmNodeList = xl.SelectNodes("parm"); int i = 0; object[] parmList = new object[parmNodeList.Count]; foreach (XmlNode parmNode in parmNodeList) { //参数个数要与构造函数对应 否则要进行转换 string id = parmNode.Attributes["id"].Value; string value = parmNode.Attributes["value"].Value; parmList[i] = value; i++; } dsConfig.Parameters = parmList; dsTable.Add(dsName, dsConfig); } } catch (Exception ex) { throw new DBConfigException(ex); } }