也许这甚至不可能,但我有兴趣查看所有Azure SQL DB数据库中的所有表。
我可以使用sys.databases获取数据库和sys.tables列表以获取表的列表,但似乎无法找出正确的组合以返回每个数据库的表。
使用直接T-SQL在Azure中可以实现吗?如果没有,是否有可行的替代方案?
答案 0 :(得分:0)
我们可以在Azure SQL DB中使用直接T-SQL。
根据您的要求,我有一些演示供您参考:
获取指定数据库中的所有表格:
/// <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;
}
结果截图: