Uday Teja
Uday Teja

Reputation: 263

Get all databases in Azure server using C#

I'm trying to get all databases names in the Azure server using C#, but I'm unable to find the connection string for it. I would like to get all the database names from the server once the user provides id and password.

Upvotes: 0

Views: 912

Answers (1)

Leon Yue
Leon Yue

Reputation: 16401

Please look at this tutorial: View a List of Databases on an Instance of SQL Server.

This topic describes how to view a list of databases on an instance of SQL Server by using SQL Server Management Studio or Transact-SQL.

For example:

USE AdventureWorks2012;  
GO  
SELECT name, database_id, create_date  
FROM sys.databases ;  
GO 

This also works for Azure SQL database.

First, you need to using the connection string to your Azure SQL. Then run the query in you c# APP, it will return all the database in your Azure SQL Server.

Or, you can reference the answer of this blog: Get list of database depends on chosen server:

To Get a List of databases from selected server:

List<String> databases = new List<String>();

SqlConnectionStringBuilder connection = new SqlConnectionStringBuilder();

 connection.DataSource = SelectedServer;
 // enter credentials if you want
 //connection.UserID = //get username;
// connection.Password = //get password;
 connection.IntegratedSecurity = true;

 String strConn = connection.ToString();

 //create connection
  SqlConnection sqlConn = new SqlConnection(strConn);

//open connection
sqlConn.Open();

 //get databases
DataTable tblDatabases = sqlConn.GetSchema("Databases");

//close connection
sqlConn.Close();

//add to list
foreach (DataRow row in tblDatabases.Rows) {
      String strDatabaseName = row["database_name"].ToString();

       databases.Add(strDatabaseName);


}     

Hope this helps.

Upvotes: 2

Related Questions