/// <summary> /// 根据模板设置 conn /// </summary> /// <param name="host"></param> /// <param name="port"></param> /// <param name="serverName"></param> /// <param name="userId"></param> /// <param name="userPwd"></param> public void SetConn(string host, string port, string serverName, string userId, string userPwd) { conn = connModel.Replace("@Host", host).Replace("@Port", port).Replace("@ServerName", serverName).Replace("@UserId", userId).Replace("@UserPwd", userPwd); string key = $"{userId}@{serverName}/{port}"; if (!Conns.ContainsKey(key)) { Conns.Add(key, conn); } }
/// <summary> /// Allow to initialise many Connection string of many Database and return them. /// </summary> /// <param name="connections">List Object of Connection class</param> /// <param name="connectionTypes">List Object of ConnectionType Enumeration</param> /// <returns>List Object IDbConnection</returns> public List <IDbConnection> InitialiseMultipleConnections(List <Connection> connections, List <ConnectionType> connectionTypes) { int i = 0; foreach (ConnectionType typeBD in connectionTypes) { switch (typeBD) { case ConnectionType.SQLServer: { Conn = new SqlConnection(String.Format("Data Source={0};Initial catalog={1};User ID={2};Password={3}", connections.ElementAt(i).Serveur, connections.ElementAt(i).Database, connections.ElementAt(i).User, connections.ElementAt(i).Password)); Conns.Add(Conn); break; } case ConnectionType.MySQL: { Conn = new MySqlConnection(String.Format("Server={0};Database={1};Uid={2};Pwd={3}", connections.ElementAt(i).Serveur, connections.ElementAt(i).Database, connections.ElementAt(i).User, connections.ElementAt(i).Password)); Conns.Add(Conn); break; } case ConnectionType.PostGreSQL: { Conn = new NpgsqlConnection(String.Format("Server={0};Database={1};UID={2};Password={3};Port={4};", connections.ElementAt(i).Serveur, connections.ElementAt(i).Database, connections.ElementAt(i).User, connections.ElementAt(i).Password, connections.ElementAt(i).Port)); Conns.Add(Conn); break; } //case ConnectionType.Oracle: //{ // //Conn = new OracleConnection(String.Format("Server={0}:{1}/{2};Uid={3};Password={4};", // // connection.Serveur, connection.Port, connection.Database, connection.User, connection.Password)); // Conn = new OracleConnection(String.Format("SERVER = (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = {0})(PORT = {1}))(CONNECT_DATA = (SERVICE_NAME = {2})));", // connections.ElementAt(i).Serveur, connections.ElementAt(i).Port, connections.ElementAt(i).Database, connections.ElementAt(i).User, connections.ElementAt(i).Password)); // Conns.Add(Conn); // break; //} case ConnectionType.Acces: { Conn = new OleDbConnection(String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}{1}", connections.ElementAt(i).Path, connections.ElementAt(i).Database)); Conns.Add(Conn); break; } } i++; } return(Conns); }