Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear Experts,

I've SQL Server express edition installed in my local system. I want to know the corrrect instance (server name) programetically(C#) forlogin purpouse.

i wrote a small application to read from the registry.


C#
RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server");            String[] instances = (String[])rk.GetValue("InstalledInstances");            if (instances.Length > 0)            {                foreach (String element in instances)                {                    MessageBox.Show(element);                }            }


But my problem is that I've to entry in above key SQLEXPRESS and SQLEXPRESSMYLOG. So I'm not able to know what is the correct login address.

Please tell me, how can i do it?

Thank you all in advance.
Posted

1 solution

Hi,

Use following code to get installed sql server instances to login. You need to use your servername as MachineName\\SQLInstanceName to login.
C#
string[] InstanceNames = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL").GetValueNames();

string ServerName = string.Empty;
foreach (string str in InstanceNames)
{
    ServerName = string.Empty;
    ServerName = string.Concat(System.Environment.MachineName, "\\", str);
    MessageBox.Show(LoginName);
}
 
Share this answer
 
Comments
vidiking 26-Apr-12 1:45am    
Thank You Deepak.

But problem is that if there is two entry in registry @"SOFTWARE\\Microsoft\\Microsoft SQL Server\\Instance Names\\SQL"
like SQLEXPRESS and SQLEXPRESS1 (As displaying in my registry), how i'll come to know which one I should use.

And is it possible to give local ip address for connection in connect string like

SqlConnection myConn1 = new SqlConnection("Server=127.0.0.1;User ID=sa; Password=pass@word; database=MyDatabase");
Deepak_Sharma_ 26-Apr-12 1:52am    
Hi,

If you have more than one SQL Server instances installed on your machine, you have to decide on which instance you want to log in. Like if you have two instances one for SQL Express 2005 and one for SQL Express 2008 you have to decide which one you want to use.

It is possible to use IP address but still you have to give the instance name you want to log in like "127.0.0.1\\SQLEXPRESS1"
vidiking 26-Apr-12 2:35am    
Ok. Thanks Deepak.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900