Get list of database depends on chosen server

后端 未结 2 1884
不知归路
不知归路 2021-02-03 15:14

I\'m using C# with framework 4.0 and SQL server 2008 R2. I have listed the SQL server 2008 with this code:

 public static string[] GetSQLServerList()
        {
          


        
2条回答
  •  逝去的感伤
    2021-02-03 15:25

        using (var connection = new System.Data.SqlClient.SqlConnection("ConnectionString"))
        {
            connection.Open();
            var command = new System.Data.SqlClient.SqlCommand();
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.CommandText = "SELECT name FROM master.sys.databases";
    
            var adapter = new System.Data.SqlClient.SqlDataAdapter(command);
            var dataset = new DataSet();
            adapter.Fill(dataset);
            DataTable dtDatabases = dataset.Tables[0];
        }
    

提交回复
热议问题