0

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.

4
  • raw.githubusercontent.com/MicrosoftDocs/azure-docs/master/… Can you check this on your azure Commented Aug 6, 2019 at 9:47
  • Hey, I have seen that in connection string it asking me to give DB name in the string itself. here, I would like to get all the databases from the server once the user gives me id and password. Commented Aug 6, 2019 at 9:50
  • you can call some sql Like leon's answer. it will give you the list of db from the sql server Commented Aug 6, 2019 at 12:57
  • Connected to the instance, database master and then executed sys.databases query it gave me all the databases Commented Aug 7, 2019 at 6:38

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou Connected to the instance, database master and then executed sys.databases query it gave me all the databases.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.