Exemple #1
0
        public void GetDbList(bool systemdbEnabled)
        {
            if (IsOnline != true)
            {
                MessageBox.Show("Can't connect now to " + _instance + ", please try again.");
                return;
            }
            DatabasesCollection = new List <SqlDatabase>();
            const string qry = @"SELECT name, state_desc, user_access, 
                            CASE is_distributor
	                            WHEN 1 then 'true'
	                            ELSE 'false'
                            END
                            FROM sys.databases ORDER BY name";

            using (var conn = GetConnection())
            {
                using (var cmd = new SqlCommand(qry, conn))
                {
                    try
                    {
                        conn.Open();
                        var rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {
                            var name        = rdr.GetString(0);
                            var status      = rdr.GetString(1);
                            var userAccess  = (sbyte)rdr.GetByte(2);
                            var distributor = bool.Parse(rdr.GetString(3));

                            if (SystemNames.Any(name.Contains) || distributor)
                            {
                                if (systemdbEnabled && name != "tempdb")
                                {
                                    var oDatabase = new SqlSystemDatabase(this, name, status, userAccess);
                                    DatabasesCollection.Add(oDatabase);
                                }
                            }
                            else
                            {
                                var oDatabase = new SqlDatabase(this, name, status, userAccess);
                                DatabasesCollection.Add(oDatabase);
                            }
                        }
                    }
                    catch (SqlException ex)
                    {
                        Utils.WriteLog(ex.Message);
                    }
                    catch (Exception ex)
                    {
                        Utils.WriteLog(ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }
Exemple #2
0
        public void GetDbList(bool systemdbEnabled)
        {
            if (IsOnline != true)
            {
                MessageBox.Show("Can't connect now to " + _instance + ", please try again.");
                return;
            }
            DatabasesCollection = new List<SqlDatabase>();
            const string qry = @"SELECT name, state_desc, user_access, 
                            CASE is_distributor
	                            WHEN 1 then 'true'
	                            ELSE 'false'
                            END
                            FROM sys.databases ORDER BY name";

            using (var conn = GetConnection())
            {
                using (var cmd = new SqlCommand(qry, conn))
                {
                    try
                    {
                        conn.Open();
                        var rdr = cmd.ExecuteReader();
                        while (rdr.Read())
                        {

                            var name = rdr.GetString(0);
                            var status = rdr.GetString(1);
                            var userAccess = (sbyte)rdr.GetByte(2);
                            var distributor = bool.Parse(rdr.GetString(3));

                            if (SystemNames.Any(name.Contains) || distributor)
                            {
                                if (systemdbEnabled && name != "tempdb")
                                {
                                    var oDatabase = new SqlSystemDatabase(this, name, status, userAccess);
                                    DatabasesCollection.Add(oDatabase);
                                }

                            }
                            else
                            {
                                var oDatabase = new SqlDatabase(this, name, status, userAccess);
                                DatabasesCollection.Add(oDatabase);
                            }
                        }

                    }
                    catch (SqlException ex)
                    {
                        Utils.WriteLog(ex.Message);
                    }
                    catch (Exception ex)
                    {
                        Utils.WriteLog(ex.Message);
                    }
                    finally
                    {
                        conn.Close();
                    }
                }
            }
        }