/// <summary> /// Create a ConnectionString to log in. /// </summary> /// <param name="sambaPath">The path of Samba shared folder; ex.: "\\server\folder"..</param> /// <param name="username">Username to log in.</param> /// <param name="domain">Domain of Username to log in.</param> /// <param name="password">Password to log in.</param> /// <returns>Encoded connection string</returns> public string Encode(string sambaPath, string username, string domain, string password) { var builder = new System.Data.Common.DbConnectionStringBuilder(); builder.Add(Constants.ConnectionString.SambaPathKey, sambaPath); builder.Add(Constants.ConnectionString.UsernameKey, username); builder.Add(Constants.ConnectionString.DomainKey, domain); builder.Add(Constants.ConnectionString.PasswordKey, password); return(builder.ConnectionString); }
private static void updateContextConnection(UserViewModel dbUser, ServiceSheetsEntities dbContext) { System.Data.Common.DbConnection connection = dbContext.Database.Connection; System.Data.Common.DbConnectionStringBuilder str = new System.Data.Common.DbConnectionStringBuilder(); str.ConnectionString = dbContext.Database.Connection.ConnectionString; str.Add("Password", dbUser.PasswordBoxObj.Password); dbContext.Database.Connection.ConnectionString = str.ConnectionString; }
public bool ValidateUserAndCacheService(string username, string password) { var builder = new System.Data.Common.DbConnectionStringBuilder(); builder.ConnectionString = _connectionString; builder.Add("RequireNewInstance", true); builder["Username"] = username; builder["Password"] = password; var conn = new CrmServiceClient(builder.ConnectionString); conn.OrganizationServiceProxy.Timeout = new TimeSpan(24, 0, 0); if (conn.IsReady) { _cachedServices[username] = conn.OrganizationServiceProxy; } return(conn.IsReady); }
public IDbConnection GetDBConnection(SQLMap map) { if (map.MyMapSign.StartsWith(BTulz.ModelsTransformer.Transformer.SQLs.SQLMapFactory.DEFAULT_DB_TYPE_SIGN)) { var constrBuider = new SqlConnectionStringBuilder(); constrBuider.DataSource = this.DBServer; constrBuider.UserID = this.DBUser; constrBuider.Password = this.DBPassword; constrBuider.InitialCatalog = this.DBName; constrBuider.ConnectTimeout = 600; //if (!string.IsNullOrEmpty(map.DefaultDatabase)) // constrBuider.InitialCatalog = map.DefaultDatabase; return(new SqlConnection(constrBuider.ConnectionString)); } else { var conBuilder = new System.Data.Common.DbConnectionStringBuilder(); DbConnectionItem dbNameItem = map.DbConnectionItems.FirstOrDefault(c => c.ItemName == DbConnectionItem.DB_CONNECTION_ITEM_DB_NAME); //如果设置了默认数据库,使用默认数据库为打开数据库 if (!string.IsNullOrEmpty(map.DefaultDatabase)) { if (dbNameItem != null) { dbNameItem.ItemValue = map.DefaultDatabase; } } //构建连接字符串项目 foreach (var dbItem in map.DbConnectionItems) { conBuilder.Add(dbItem.StringBuilderKey, dbItem.ItemValue); } if (!string.IsNullOrEmpty(map.DefaultDatabase)) { //修正回原始数据库 dbNameItem.ItemValue = this.DBName; } var tmps = map.DbConnetionClassName.Split(';'); if (tmps.Length != 2) { throw new Exception("无效的连接类设置。参考格式[Dbconnection.dll;Dbconnnection]"); } var dllFile = tmps[0]; var className = tmps[1]; foreach (System.Reflection.Assembly itemAbly in System.AppDomain.CurrentDomain.GetAssemblies()) { if (!string.Equals(dllFile, itemAbly.ManifestModule.ToString(), StringComparison.InvariantCultureIgnoreCase)) { continue; } var dbCon = itemAbly.CreateInstance(className) as System.Data.IDbConnection; if (dbCon != null) { if (map.MyMapSign == "HANA") { dbCon.ConnectionString = string.Format(@"{0};CS=""{1}""", conBuilder.ToString(), dbNameItem.ItemValue); } else if (map.MyMapSign == "MYSQL") { dbCon.ConnectionString = conBuilder.ToString() + ";Allow User Variables = True"; } else { dbCon.ConnectionString = conBuilder.ToString(); } return(dbCon); } } } throw new Exception("无法获取的数据库连接。"); }
private static string GetCS(DataBase db, System.Data.Common.DbProviderFactory df) { if (object.ReferenceEquals(df.GetType(), typeof(System.Data.SqlClient.SqlClientFactory))) { System.Data.SqlClient.SqlConnectionStringBuilder csb = new System.Data.SqlClient.SqlConnectionStringBuilder(); csb.DataSource = db.Server; if (db.Port.HasValue && db.Port.Value > 0) { csb.DataSource += "," + db.Port.Value.ToString(System.Globalization.CultureInfo.InvariantCulture); } csb.InitialCatalog = db.Database; if (db.IntegratedSecurity.HasValue) { csb.IntegratedSecurity = db.IntegratedSecurity.Value; } else { csb.IntegratedSecurity = false; } if (!csb.IntegratedSecurity) { csb.UserID = db.Username; try { csb.Password = new Des3EncryptionService().DeCrypt(db.Password); } catch (System.Exception) { // Password is unencrypted ... csb.Password = db.Password; } } // End if (!csb.IntegratedSecurity) csb.PacketSize = 4096; csb.PersistSecurityInfo = false; csb.ApplicationName = "LDAP-Service"; csb.MultipleActiveResultSets = false; csb.WorkstationID = System.Environment.MachineName; return(csb.ConnectionString); } // End if (object.ReferenceEquals(this.m_db.ProviderFactory.GetType(), typeof(System.Data.SqlClient.SqlClientFactory))) if (object.ReferenceEquals(df.GetType(), typeof(Npgsql.NpgsqlFactory))) { Npgsql.NpgsqlConnectionStringBuilder csb = new Npgsql.NpgsqlConnectionStringBuilder(); csb.Host = db.Server; if (db.Port.HasValue && db.Port.Value > 0) { csb.Port = db.Port.Value; } else { csb.Port = 5432; } csb.Database = db.Database; if (db.IntegratedSecurity.HasValue) { csb.IntegratedSecurity = db.IntegratedSecurity.Value; } else { csb.IntegratedSecurity = false; } if (!csb.IntegratedSecurity) { csb.Username = db.Username; try { csb.Password = new Des3EncryptionService().DeCrypt(db.Password); } catch (System.Exception) { // Password is unencrypted ... csb.Password = db.Password; } } // End if (!csb.IntegratedSecurity) csb.PersistSecurityInfo = false; csb.ApplicationName = "LDAP-Service"; csb.CommandTimeout = 30; csb.Timeout = 15; return(csb.ConnectionString); } // End if (object.ReferenceEquals(this.m_db.ProviderFactory.GetType(), typeof(Npgsql.NpgsqlFactory))) if (object.ReferenceEquals(df.GetType(), typeof(MySql.Data.MySqlClient.MySqlClientFactory))) { MySql.Data.MySqlClient.MySqlConnectionStringBuilder csb = new MySql.Data.MySqlClient.MySqlConnectionStringBuilder(); csb.Server = db.Server; if (db.Port.HasValue && db.Port.Value > 0) { csb.Port = (uint)db.Port.Value; } else { csb.Port = 3306u; } csb.Database = db.Database; csb.PersistSecurityInfo = false; csb.ApplicationName = "LDAP-Service"; csb.DefaultCommandTimeout = 30; csb.ConnectionTimeout = 15; return(csb.ConnectionString); } System.Data.Common.DbConnectionStringBuilder dbcsb = df.CreateConnectionStringBuilder(); dbcsb.Add("Server", db.Server); if (db.Port.HasValue && db.Port.Value > 0) { dbcsb.Add("Port", db.Port); } dbcsb.Add("Database", db.Database); if (db.IntegratedSecurity.HasValue) { dbcsb.Add("IntegratedSecurity", db.IntegratedSecurity); } else { dbcsb.Add("IntegratedSecurity", false); } if (db.IntegratedSecurity.HasValue && !db.IntegratedSecurity.Value) { dbcsb.Add("User Id", db.Username); try { dbcsb.Add("Password", new Des3EncryptionService().DeCrypt(db.Password)); } catch (System.Exception) { // Password is unencrypted ... dbcsb.Add("Password", db.Password); } } // End if (!csb.IntegratedSecurity) return(dbcsb.ConnectionString); }