using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Sql;
using Microsoft.Win32;

//using Microsoft.SqlServer.Management.Smo;


namespace DataSearchinginDB
{
    public partial class frmDataSearching : Form
    {
        public frmDataSearching()
        {
            InitializeComponent();

        }

        private void frmDataSearching_Load(object sender, EventArgs e)
        {
            SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
            System.Data.DataTable table = instance.GetDataSources();
            foreach (System.Data.DataRow row in table.Rows)
            {
                if (row["ServerName"] != DBNull.Value && Environment.MachineName.Equals(row["ServerName"].ToString()))
                {
                    string item = string.Empty;
                    item = row["ServerName"].ToString();
                    if (row["InstanceName"] != DBNull.Value || !string.IsNullOrEmpty(Convert.ToString(row["InstanceName"]).Trim()))
                    {
                        item += @"\" + Convert.ToString(row["InstanceName"]).Trim();
                    }
                    cmbServer.Items.Add(item);
                }
            }
        }
    }
}

I coded it in C# for load server name. But now I need to load database name with out any connection. Please help me to do this..

Recommended Answers

All 3 Replies

Please help and siggedt on it

Hi Not sure what you are looking for, Your code will give you something like:

<ServerName>
<ServerName>\<Instance1Name>
....
<ServerName>\<InstanceNName>

if you are allowing users to configure a database connection have you tried using the DataConnectionDialog class? It will handle all that interface for you and return the necessary connection string.

public static void GetDatabases()
        {
            String connString =  "Data Source=localhost;User ID=**;Password=**********;";
                DataTable tblDatabases ;
            using (SqlConnection sqlConn = new SqlConnection(connString))
            {
                sqlConn.Open();
                tblDatabases = sqlConn.GetSchema("Databases");
                sqlConn.Close();
                DataTable td = tblDatabases.Select("dbid>6").CopyToDataTable();
            }
            int i = 0;
            foreach (DataRow item in tblDatabases.Rows)
            {
                Console.WriteLine(item.Table.Rows[i][0]);
                i++;              
            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.