【问题标题】:Azure SQL DB find all Tables in all DatabasesAzure SQL DB 查找所有数据库中的所有表
【发布时间】:2018-06-13 14:30:35
【问题描述】:

也许这甚至是不可能的,但我有兴趣查看我所有 Azure SQL DB 数据库中的所有表。

我可以使用 sys.databases 获取数据库列表,使用 sys.tables 获取表列表,但似乎无法找出正确组合以返回每个数据库的表。

这在 Azure 中是否可以使用直接 T-SQL?如果没有,是否有可行的替代方案?

【问题讨论】:

  • 请看 David Makogon 的回答 stackoverflow.com/questions/17584084/…
  • 你的问题解决了吗?我正在检查这个问题的进展情况。
  • 是的,问题解决了。我是否正确理解您提供的解决方案使用 PowerShell?
  • 任何更新??????
  • @Lee Liu - 请看我两天前的回复。

标签: azure azure-sql-database


【解决方案1】:

我们可以在 Azure SQL DB 中直接使用 T-SQL。

根据你的要求,我有一些demo供你参考:

获取指定数据库中的所有表:

    /// <summary>
    /// get all Tables in a data base
    /// </summary>
    /// <param name="DataBaseName">For example: MyDataBase1</param>
    /// <returns></returns>
    public ActionResult QueryAllTables(string DataBaseName)
    {
        ContentResult content = new ContentResult();

        SqlConnection conn = new SqlConnection("Server=tcp:dotxxxxxxx.database.windows.net,1433;Initial Catalog="+ DataBaseName + ";Persist Security Info=False;User ID=xxxx;Password=xxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
        try
        {
            conn.Open();
            SqlCommand command = new SqlCommand();
            command.Connection = conn;
            command.CommandType = System.Data.CommandType.Text;
            command.CommandText = "select id,name from sysobjects where xtype='U'"; 
            SqlDataReader reader = command.ExecuteReader();
            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    string tableInfo  = "id:"+reader[0].ToString()+ " name:"+reader[1].ToString();
                    content.Content += tableInfo + "\r\n";
                }
            }
            else
            {
                content.Content = "No Table";
            }

        }
        catch (Exception ex)
        {
            content.Content = ex.Message;
        }
        return content;
    }

结果截图:

从每个数据库中获取所有表

    /// <summary>
    /// get all data bases' names
    /// </summary>
    /// <returns></returns>
    public List<string> QueryAllDbName()
    {
        ContentResult content = new ContentResult();
        List<string> list = new List<string>();
        using (SqlConnection conn = new SqlConnection("Server=tcp:xxxxxxx.windows.net,1433;Initial Catalog=DotNetAppSqlDb20180410043804_db;Persist Security Info=False;User ID=xxxxx;Password=xxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"))
        {
            try
            {
                conn.Open();
                SqlCommand command = new SqlCommand();
                command.Connection = conn;
                command.CommandType = System.Data.CommandType.Text;
                command.CommandText = "SELECT Name FROM SysDatabases ORDER BY Name";
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        string DbName = reader[0].ToString();
                        list.Add(DbName);
                    }
                }
                else
                {
                    list = null;
                }

            }
            catch (Exception ex)
            {
                content.Content = ex.Message;
            }
        }

        return list;
    }


    public ActionResult QueryAllTablesFromEachDb()
    {
        ContentResult content = new ContentResult();
        List<string> DbNames = QueryAllDbName();
        foreach (string DbName in DbNames)
        {
            using (SqlConnection conn = new SqlConnection("Server=tcp:xxxxxxxxx.database.windows.net,1433;Initial Catalog="+ DbName + ";Persist Security Info=False;User ID=xxxxxx;Password=xxxxxx;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"))
            {
                try
                {
                    conn.Open();
                    SqlCommand command = new SqlCommand();
                    command.Connection = conn;
                    command.CommandType = System.Data.CommandType.Text;

                    content.Content += "DataBase Name: " + DbName + "\r\n";
                    command.CommandText = "select id,name from sysobjects where xtype='U'";
                    SqlDataReader reader = command.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            string tableInfo = "     Table id:" + reader[0].ToString() + " Table name:" + reader[1].ToString();
                            content.Content += tableInfo + "\r\n";
                        }
                    }
                    reader.Close();

                }
                catch (Exception ex)
                {
                    content.Content = ex.Message;
                }
            }
        }
        return content;
    }

结果截图:

【讨论】:

    猜你喜欢
    • 2011-02-13
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多