public static string[] GetDatabases(string serverName, string userName, string password, BLL.ConnectionStringHelper.DatabaseTypes dbType, bool trustedConnection) { List <string> databaseNames = new List <string>(); Microsoft.SqlServer.Management.Smo.Server server = new Server(new ServerConnection(serverName, userName, password)); foreach (Microsoft.SqlServer.Management.Smo.Database database in server.Databases) { databaseNames.Add(database.Name); } return((string[])databaseNames.ToArray()); }
public static string[] GetDatabases(string serverName, string userName, string password, BLL.ConnectionStringHelper.DatabaseTypes dbType, bool trustedConnection) { List <string> databases = new List <string>(); SQLDMO.SQLServer sqlServer = new SQLServerClass(); sqlServer.Connect(serverName, userName, password); foreach (SQLDMO.Database database in sqlServer.Databases) { databases.Add(database.Name); } return((string[])databases.ToArray()); }
public static string[] GetDatabases(string serverName, string userName, string password, BLL.ConnectionStringHelper.DatabaseTypes databaseType, bool trustedConnection) { SqlCommand cmd = new SqlCommand(); SqlConnection conn = null; if (trustedConnection) { conn = new SqlConnection(string.Format("Server={0};Database=master;Trusted_Connection=True", serverName)); } else { conn = new SqlConnection(string.Format("Server={0};Database=master;User ID={1};Password={2};Trusted_Connection=False", serverName, userName, password)); } List <string> databaseNames = new List <string>(); try { conn.Open(); cmd.Connection = conn; cmd.CommandText = "SELECT name FROM master..sysdatabases"; cmd.CommandType = CommandType.Text; SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); while (rdr.Read()) { databaseNames.Add(rdr.GetString(0)); } } catch { conn.Close(); throw; } return((string[])databaseNames.ToArray()); }