anfd
anfd

Reputation: 121

get servername with sqlserver name in computer local or network

DataTable table = SqlDataSourceEnumerator.Instance.GetDataSources();
foreach (DataRow server in table.Rows)
{
    cmbshowallsqlserver.Items.Add(server[table.Columns["ServerName"]].ToString());
}

This code give me only name of computer. I want give me computername with sqlserver name. for example this code give me anfd-pc but I need for example anfd-pc\sqlexpress or anfd-pc\zarnegar

Upvotes: 2

Views: 7731

Answers (2)

David
David

Reputation: 218798

The ServerName column in the table you're getting from GetDataSources() has exactly that, the server name. You can also get the instance name from the InstanceName column. See MSDN for other available options.

So your code would become something like this:

cmbshowallsqlserver.Items.Add(
    string.Format(@"{0}\{1}",
                  server[table.Columns["ServerName"]],
                  server[table.Columns["InstanceName"]])
);

Upvotes: 3

Shafqat Masood
Shafqat Masood

Reputation: 2570

DataTable table = SqlDataSourceEnumerator.Instance.GetDataSources();

foreach (DataRow server in table.Rows)
{
    string servername = server[table.Columns["ServerName"]].ToString();

    // you can get that using the instanceName property 
    string instancename = server[table.Columns["InstanceName"].ToString();

    //and version property tells you the version of sql server i.e 2000, 2005, 2008 r2 etc
    string sqlversion = server[table.Columns["Version"].ToString();

    //to form the servername you can combine the server and instancenames as
    string sqlserverfullname = String.Format("{0}\{1}",servername, instancename);

    cmbshowallsqlserver.Items.Add(sqlserverfullname);
}

Upvotes: 1

Related Questions