How to Get SQl Server Instance name
-
Friday, August 26, 2011 12:47 PM
I am doing a Windows Application in C#.net (3.5) with SQL Server 2005 as Back end.
My task is i am required to get the SQL Server's Server Name in Combo box without logging in SQL Server/ or Instance Name.
I tried to access via Registry Keys but was unsucessfull as i am hardcoding the key values.
But is there any was i can get Servername of SQL Server in Combobox.
OSQL -L did not work, how should i approach this
All Replies
-
Friday, August 26, 2011 1:11 PM
Check with this
http://msdn.microsoft.com/en-us/library/system.data.sql.sqldatasourceenumerator.getdatasources.aspx
If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful". -
Friday, August 26, 2011 1:13 PM
-
Friday, August 26, 2011 1:13 PM
-
Friday, August 26, 2011 1:17 PM
Hello Amit, if your Sql Server verion is equal or greater to 2005 you can use the SERVERPTOPERTY function:
http://msdn.microsoft.com/en-us/library/ms174396(v=SQL.90).aspx
For example, this code can be used for that stuff:
using System; using System.Data; using System.Data.SqlClient; namespace SqlServerExamples { class Program { static void Main(string[] args) { using (var connection = new SqlConnection("Data Source = ...; User Id = ...; Password = ...;")) { connection.Open(); using (var command = new SqlCommand("SELECT SERVERPROPERTY('ServerName') AS ServerName, SERVERPROPERTY('InstanceName') AS InstanceName", connection)) { using (var reader = command.ExecuteReader(CommandBehavior.SingleRow)) { if (reader.Read()) { Console.WriteLine(reader["ServerName"]); Console.WriteLine(reader["InstanceName"]); } } } } Console.ReadLine(); } } }
Hope this helps,
Miguel.
-
Friday, August 26, 2011 3:40 PM
am getting error on connection.open();
without providing data source and other parameters how can i open?
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)- Edited by amit_kumar Friday, August 26, 2011 3:42 PM modification
-
Saturday, August 27, 2011 6:10 AM
Amit, Try the below function.
public string[] GetSqlInstances() { DataRowCollection rows = SqlDataSourceEnumerator.Instance.GetDataSources().Rows; string[] instances = new string[rows.Count]; for (int i = 0; i < rows.Count; i++) { instances[i] = Convert.ToString(rows[i]["InstanceName"]); } return instances; }
Hope this helps.
Please mark this post as answer if it solved your problem. Happy Programming!- Proposed As Answer by Hasibul Haque Saturday, August 27, 2011 8:01 AM
-
Friday, November 23, 2012 5:57 AM
Hi,
See the below URL to get the IP address and Servername of SQL server.
http://gallery.technet.microsoft.com/Get-the-IP-Address-of-SQL-f410071a
Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

