public static IDbConnection GetConnection(string name, bool isRead = true) { ConnectionItem connectionConfig = DataSource.connStringManager.GetConnectionConfig(name, isRead); if (connectionConfig == null) { throw new Exception("数据库名称" + name + "在数据库配置中未找到,请检查数据库配置文件或联系DBA!"); } DbProviderFactory factory = DbProviderFactories.GetFactory(connectionConfig.Provider ?? "System.Data.SqlClient"); if (factory == null) { throw new Exception("数据库驱动名称" + connectionConfig.Provider + "无法初始化,请检查数据库驱动是否正常!"); } DbConnection connection = factory.CreateConnection(); if (connectionConfig.Encrypt) { int num = connectionConfig.ConnectionString.IndexOf("password=", 0, connectionConfig.ConnectionString.Length, StringComparison.CurrentCultureIgnoreCase) + 9; int startIndex = connectionConfig.ConnectionString.IndexOf(';', num, connectionConfig.ConnectionString.Length - num); string toDecrypt = connectionConfig.ConnectionString.Substring(num, startIndex - num); string str = connectionConfig.ConnectionString.Substring(0, num) + Pwd.Decrypt(toDecrypt, DataSource.GetPwdKey()) + connectionConfig.ConnectionString.Substring(startIndex, connectionConfig.ConnectionString.Length - startIndex); connection.ConnectionString = str; } else { connection.ConnectionString = connectionConfig.ConnectionString; } return((IDbConnection) new TCDbConnection(connection) { CommandTimeout = connectionConfig.CommandTimeOut, Provider = connectionConfig.Provider }); }
internal ConnectionItem GetConnectionConfig(string name, bool isRead) { DataBaseConfig dataBaseConfig = this.cache.DataBaseList.FirstOrDefault <DataBaseConfig>((Func <DataBaseConfig, bool>)(p => p.Name == name)); if (dataBaseConfig == null) { return((ConnectionItem)null); } ConnectionItem connectionItem = dataBaseConfig.GetConnectionItem(isRead); if (connectionItem == null) { throw new Exception("无法找到匹配的数据库连接,请求的数据库是" + name + ",请求的数据库类型为" + (isRead ? "读库" : "写库")); } return(connectionItem); }
public static ConnectionConfig XmlToConfig(string xml) { ConnectionConfig connectionConfig = new ConnectionConfig(); XElement xelement1 = XDocument.Load((TextReader) new StringReader(xml)).Element((XName)"tcbase.dataAccess"); if (xelement1 == null) { return(connectionConfig); } XAttribute xattribute1 = xelement1.Attribute((XName)"name"); if (xattribute1 != null) { connectionConfig.Name = xattribute1.Value; } foreach (XElement element1 in xelement1.Elements((XName)"database")) { DataBaseConfig dataBaseConfig = new DataBaseConfig(); connectionConfig.DataBaseList.Add(dataBaseConfig); XAttribute xattribute2 = element1.Attribute((XName)"name"); if (xattribute2 != null) { dataBaseConfig.Name = xattribute2.Value; } XAttribute xattribute3 = element1.Attribute((XName)"default"); if (xattribute3 != null) { dataBaseConfig.IsDefault = !(xattribute3.Value == "false"); } XElement xelement2 = element1.Element((XName)"read"); if (xelement2 != null) { foreach (XElement element2 in xelement2.Elements((XName)"server")) { ConnectionItem connectionItem = new ConnectionItem(); dataBaseConfig.ReadList.Add(connectionItem); XAttribute xattribute4 = element2.Attribute((XName)"enabled"); if (xattribute4 != null) { connectionItem.Enabled = !(xattribute4.Value.ToLower() == "false"); } XAttribute xattribute5 = element2.Attribute((XName)"encrypt"); connectionItem.Encrypt = xattribute5 == null || !(xattribute5.Value.ToLower() == "false"); XAttribute xattribute6 = element2.Attribute((XName)"weight"); if (xattribute6 != null) { connectionItem.Weight = Convert.ToInt32(xattribute6.Value); } XAttribute xattribute7 = element2.Attribute((XName)"connectionString"); if (xattribute7 != null) { connectionItem.ConnectionString = xattribute7.Value; } XAttribute xattribute8 = element2.Attribute((XName)"commandTimeOut"); if (xattribute8 != null) { connectionItem.CommandTimeOut = Convert.ToInt32(xattribute8.Value); } XAttribute xattribute9 = element2.Attribute((XName)"provider"); if (xattribute9 != null) { connectionItem.Provider = xattribute9.Value; } } } XElement xelement3 = element1.Element((XName)"write"); if (xelement3 != null) { foreach (XElement element2 in xelement3.Elements((XName)"server")) { ConnectionItem connectionItem = new ConnectionItem(); dataBaseConfig.WriteList.Add(connectionItem); XAttribute xattribute4 = element2.Attribute((XName)"enabled"); if (xattribute4 != null) { connectionItem.Enabled = !(xattribute4.Value == "false"); } XAttribute xattribute5 = element2.Attribute((XName)"encrypt"); connectionItem.Encrypt = xattribute5 == null || !(xattribute5.Value == "false"); XAttribute xattribute6 = element2.Attribute((XName)"weight"); if (xattribute6 != null) { connectionItem.Weight = Convert.ToInt32(xattribute6.Value); } XAttribute xattribute7 = element2.Attribute((XName)"connectionString"); if (xattribute7 != null) { connectionItem.ConnectionString = xattribute7.Value; } XAttribute xattribute8 = element2.Attribute((XName)"commandTimeOut"); if (xattribute8 != null) { connectionItem.CommandTimeOut = Convert.ToInt32(xattribute8.Value); } XAttribute xattribute9 = element2.Attribute((XName)"provider"); if (xattribute9 != null) { connectionItem.Provider = xattribute9.Value; } } } } return(connectionConfig); }