Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Getting Database Names from Server

Getting Database Names from Server

Scheduled Pinned Locked Moved Database
databasesql-serversysadmindata-structuressecurity
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PDTUM
    wrote on last edited by
    #1

    Good Afternoon, I used a nice SQL insert/update program for years with MSSQL 2000, but was never able to get it past security to make it work with 2005. This weekend, out of frustration, I decided to take a shot at writing one of my own. In doing so, I attempted to list the databases that were on the server. I first tested the command in the Query analyzer and it worked fine, returning all the names of the databases. However, when I tried to use the exact same code in the application, every database name comes up as "master" in the array, so I cannot continue to propagate the tables without a named database. I cannot understand how the identical code can return different results in different places. Here is the code. Appreciate your assistance....Thank you, Pat

    string Sql = "SELECT name FROM sys.databases";

                SqlCommand Comm = new SqlCommand(Sql, Conn);
    
                SqlDataAdapter da = new SqlDataAdapter(Comm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                
    	int cnt = ds.Tables\[0\].Rows.Count;
                string\[\] dbs = new string\[cnt\];
    
                for(int i = 0; i < cnt; i++)
                {
                 dbs\[i\] = ds.Tables\[0\].Rows\[0\]\["name"\].ToString();   
                }
    
                foreach(string s in dbs)
                {
                    databases.Items.Add(s);
                }
    
    L 1 Reply Last reply
    0
    • P PDTUM

      Good Afternoon, I used a nice SQL insert/update program for years with MSSQL 2000, but was never able to get it past security to make it work with 2005. This weekend, out of frustration, I decided to take a shot at writing one of my own. In doing so, I attempted to list the databases that were on the server. I first tested the command in the Query analyzer and it worked fine, returning all the names of the databases. However, when I tried to use the exact same code in the application, every database name comes up as "master" in the array, so I cannot continue to propagate the tables without a named database. I cannot understand how the identical code can return different results in different places. Here is the code. Appreciate your assistance....Thank you, Pat

      string Sql = "SELECT name FROM sys.databases";

                  SqlCommand Comm = new SqlCommand(Sql, Conn);
      
                  SqlDataAdapter da = new SqlDataAdapter(Comm);
                  DataSet ds = new DataSet();
                  da.Fill(ds);
                  
      	int cnt = ds.Tables\[0\].Rows.Count;
                  string\[\] dbs = new string\[cnt\];
      
                  for(int i = 0; i < cnt; i++)
                  {
                   dbs\[i\] = ds.Tables\[0\].Rows\[0\]\["name"\].ToString();   
                  }
      
                  foreach(string s in dbs)
                  {
                      databases.Items.Add(s);
                  }
      
      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Cross-post, answers go here[^] :)

      I are Troll :suss:

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups