Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
# Wednesday, June 07, 2006

There are alot of ways to accomplish this. The simplest one is to use the sp_databases procedure. Here's some example that uses this stored procedure in conjunction with ADO.NET to consume the returned data.

The sp_databases procedure lists databases that either reside in an instance of the SQL Server 2000/2005 Database Engine or are accessible through a database gateway. You can read this article from MSDN for more information about the sp_databases procedure.

//######################################
//C# EXAMPLE
//######################################

//using SqlAdapter and DataTable
using (SqlDataAdapter adapter = new SqlDataAdapter("sp_databases", connectionString))
{
   adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
   DataTable dataTable = new DataTable();

   adapter.Fill(dataTable);

   foreach (DataRow dataRow in dataTable.Rows)
   {
      Console.WriteLine(dataRow["DATABASE_NAME"]);
   }
}

//using SqlCommand and SqlReader
using (SqlCommand sqlCommand = new SqlCommand("sp_databases", new SqlConnection(connectionString)))
{
   sqlCommand.CommandType = CommandType.StoredProcedure;
   sqlCommand.Connection.Open();
   SqlDataReader sqlReader = sqlCommand.ExecuteReader();

   while (sqlReader.Read())
   {
      Console.WriteLine(sqlReader["DATABASE_NAME"]);
   }

   sqlReader.Close();
   sqlCommand.Connection.Close();
}


'######################################
'VB.NET EXAMPLE
'######################################

'using SqlAdapter and DataTable
Using adapter As SqlDataAdapter = New SqlDataAdapter("sp_databases", connectionString)

    adapter.SelectCommand.CommandType = CommandType.StoredProcedure

    Dim table As New DataTable()

    adapter.Fill(table)

    For Each dr As DataRow In table.Rows
    Console.WriteLine(dr("DATABASE_NAME"))
    Next

End Using


'using SqlAdapter and DataTable
Using sqlCommand As SqlCommand = New SqlCommand("sp_databases", New SqlConnection(connectionString)))

    sqlCommand.CommandType = CommandType.StoredProcedure

    sqlCommand.Connection.Open()
    Dim sqlReader As SqlDataReader = sqlCommand.ExecuteReader()

    While (sqlReader.Read())
        Console.WriteLine(sqlReader("DATABASE_NAME"))
    End While

    sqlReader.Close()
    sqlCommand.Connection.Close()

End Using

*btw, you can also do this by using SMO or DMO. Read this article for more info.

Wednesday, June 07, 2006 10:58:02 PM (GMT Daylight Time, UTC+01:00)  #    Comments [0] -
.NET
Tracked by:
"bay area photographer" (bay area photographer) [Trackback]
"her first audition" (her first audition) [Trackback]
"buyphentermineprozac" (buyphentermineprozac) [Trackback]
"annuncio affitti isernia" (annuncio affitti isernia) [Trackback]
"paul posey tallahassee florida" (paul posey tallahassee florida) [Trackback]
"sauna mista milano" (sauna mista milano) [Trackback]
"una sera nel parco" (una sera nel parco) [Trackback]
"Executive Office Furniture" (Executive Office Furniture) [Trackback]
"georgia bulldog" (georgia bulldog) [Trackback]
"pacifica armoire" (pacifica armoire) [Trackback]
"vecchie mature grasse donne" (vecchie mature grasse donne) [Trackback]
"cd organizers" (cd organizers) [Trackback]
"gadget audi" (gadget audi) [Trackback]
"female body builders posing" (female body builders posing) [Trackback]
"congelatore da incasso" (congelatore da incasso) [Trackback]
"cameriere in autoreggenti" (cameriere in autoreggenti) [Trackback]
"Sports Betting Rss Feed" (Sports Betting Rss Feed) [Trackback]
"albuquerque respiratory jobs va medical center" (albuquerque respiratory jobs v... [Trackback]
"tiaras and headpieces" (tiaras and headpieces) [Trackback]
"used kountry aire camper trailer" (used kountry aire camper trailer) [Trackback]
"bollente fantastico nubile" (bollente fantastico nubile) [Trackback]
"invisibile pulcino dildo" (invisibile pulcino dildo) [Trackback]
"bitches getting fuck" (bitches getting fuck) [Trackback]
"fotografico" (fotografico) [Trackback]
"how to make a sword" (how to make a sword) [Trackback]
"sms gratis cellulare" (sms gratis cellulare) [Trackback]
"teen dating forum" (teen dating forum) [Trackback]
"west virginia dui law" (west virginia dui law) [Trackback]
"poor girls for marriage" (poor girls for marriage) [Trackback]
"viaggi abruzzo" (viaggi abruzzo) [Trackback]
Archive
<June 2006>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
2526272829301
2345678
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2009
Keith Rull
Sign In
Statistics
Total Posts: 269
This Year: 8
This Month: 0
This Week: 0
Comments: 182
Themes
Pick a theme:
All Content © 2009, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)