Microsoft.Win64.RegistryKey?!!!
-
Hey all, The RegistryKey refers to Microsoft.Win32.RegistryKey and I am on Windows Server 2003 with 64 bit. Do you all know if there is a reference for Microsoft.Win64.RegistryKey? The code looks like this now...debugging it shows that the value of serverName is NULL(no kidding Wink )
public string RetrieveDataSource { get { try { System.Data.Sql.SqlDataSourceEnumerator en = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable dt = en.GetDataSources(); RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server"); //using(RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft SQL Server")) String[] instances = (String[])rk.GetValue("InstalledInstances");**(This is NULL)** if (instances.Length > 0) { foreach (string element in instances) { //add if doesn't exist if (element != "MSSQLSERVER") { serverName = String.Format(System.Environment.MachineName, element); } else { serverName = System.Environment.MachineName; } } } } catch (Exception ex) { ex2 = ex.Message; } return serverName;**(This is NULL)** } }
Is there any other way of getting the instance name without this roundabout? Seemed pretty straight forward to me before i coded...now not so much. I can get the machine name directly by System.Environment.MachineName; if only there is a way to retrieve Instance name without RegistryKey hassle..i would avoid this method totally. Thanks to Sandeep, Rolcr, Disgyza and Colin Angus Mackay for helpingSwetha
-
Hey all, The RegistryKey refers to Microsoft.Win32.RegistryKey and I am on Windows Server 2003 with 64 bit. Do you all know if there is a reference for Microsoft.Win64.RegistryKey? The code looks like this now...debugging it shows that the value of serverName is NULL(no kidding Wink )
public string RetrieveDataSource { get { try { System.Data.Sql.SqlDataSourceEnumerator en = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable dt = en.GetDataSources(); RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server"); //using(RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft SQL Server")) String[] instances = (String[])rk.GetValue("InstalledInstances");**(This is NULL)** if (instances.Length > 0) { foreach (string element in instances) { //add if doesn't exist if (element != "MSSQLSERVER") { serverName = String.Format(System.Environment.MachineName, element); } else { serverName = System.Environment.MachineName; } } } } catch (Exception ex) { ex2 = ex.Message; } return serverName;**(This is NULL)** } }
Is there any other way of getting the instance name without this roundabout? Seemed pretty straight forward to me before i coded...now not so much. I can get the machine name directly by System.Environment.MachineName; if only there is a way to retrieve Instance name without RegistryKey hassle..i would avoid this method totally. Thanks to Sandeep, Rolcr, Disgyza and Colin Angus Mackay for helpingSwetha
I posted this on a different thread of yours but I figured I'd post it here as perhaps a different approach to your problem. I'm not entirely sure if this will work for you, but you can certainly give it a shot. You could try using this:
using(Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer m = new Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer())
{
foreach (ServerInstance si in mc.ServerInstances)
{
Response.Write("ServerInstance:" + si.Name + "
");
}
}You'll probably have to add a Reference to Microsoft.SqlServer.Smo Best of luck
Cheers Disgyza Programmer Analyst
-
Hey all, The RegistryKey refers to Microsoft.Win32.RegistryKey and I am on Windows Server 2003 with 64 bit. Do you all know if there is a reference for Microsoft.Win64.RegistryKey? The code looks like this now...debugging it shows that the value of serverName is NULL(no kidding Wink )
public string RetrieveDataSource { get { try { System.Data.Sql.SqlDataSourceEnumerator en = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable dt = en.GetDataSources(); RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server"); //using(RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft SQL Server")) String[] instances = (String[])rk.GetValue("InstalledInstances");**(This is NULL)** if (instances.Length > 0) { foreach (string element in instances) { //add if doesn't exist if (element != "MSSQLSERVER") { serverName = String.Format(System.Environment.MachineName, element); } else { serverName = System.Environment.MachineName; } } } } catch (Exception ex) { ex2 = ex.Message; } return serverName;**(This is NULL)** } }
Is there any other way of getting the instance name without this roundabout? Seemed pretty straight forward to me before i coded...now not so much. I can get the machine name directly by System.Environment.MachineName; if only there is a way to retrieve Instance name without RegistryKey hassle..i would avoid this method totally. Thanks to Sandeep, Rolcr, Disgyza and Colin Angus Mackay for helpingSwetha
public string RetrieveDataSource { get { try { System.Data.Sql.SqlDataSourceEnumerator en = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable dt = en.GetDataSources(); RegisteredServer[] rsvrs = Microsoft.SqlServer.Management.Smo.SqlServerRegistrations.EnumRegisteredServers(); foreach (RegisteredServer rs in rsvrs) { serverName = rs.ServerInstance; } } catch (Exception ex) { ex2 = ex.Message; } return serverName; } }
The returned string is "machine name"\sqlexpress. Atleast this returns something instead of object reference error. Now the next question is, the Sql server instance the solution is accessing is MSSQLSERVER2008, now why would it show Sqlexpress in the output? If the answer is because its default, how to find the instance which is being currently used. Thanks.Swetha Sankaran
-
I posted this on a different thread of yours but I figured I'd post it here as perhaps a different approach to your problem. I'm not entirely sure if this will work for you, but you can certainly give it a shot. You could try using this:
using(Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer m = new Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer())
{
foreach (ServerInstance si in mc.ServerInstances)
{
Response.Write("ServerInstance:" + si.Name + "
");
}
}You'll probably have to add a Reference to Microsoft.SqlServer.Smo Best of luck
Cheers Disgyza Programmer Analyst
Though it seemed like it would work, it showed error in ServerInstance keyword. Added a bunch of assembly reference using Microsoft.SqlServer.Management.Smo.Wmi; using Microsoft.SqlServer.Management.Smo; using Microsoft.SqlServer.Management.Smo.RegisteredServers; Looks like i'm pretty close, if only i could figure out how to find the sql server instance running, i'll be all set to post an article about the exercise :laugh:
Swetha
-
public string RetrieveDataSource { get { try { System.Data.Sql.SqlDataSourceEnumerator en = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable dt = en.GetDataSources(); RegisteredServer[] rsvrs = Microsoft.SqlServer.Management.Smo.SqlServerRegistrations.EnumRegisteredServers(); foreach (RegisteredServer rs in rsvrs) { serverName = rs.ServerInstance; } } catch (Exception ex) { ex2 = ex.Message; } return serverName; } }
The returned string is "machine name"\sqlexpress. Atleast this returns something instead of object reference error. Now the next question is, the Sql server instance the solution is accessing is MSSQLSERVER2008, now why would it show Sqlexpress in the output? If the answer is because its default, how to find the instance which is being currently used. Thanks.Swetha Sankaran
You should probably verify that there is only one ServerInstance being returned. Because you are using a foreach loop and overwriting the serverName value it will always show you the last ServerInstance. If you have more than one ServerInstance this may become a problem. Perhaps you can store the value of the ServerInstance in the Web.config file in the AppSettings? I'm heading home for the weekend and won't be able to check up on this post anymore until Monday. I wish you the best of luck Swetha. I'm sure you will find a solution.
Cheers Disgyza Programmer Analyst
-
public string RetrieveDataSource { get { try { System.Data.Sql.SqlDataSourceEnumerator en = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable dt = en.GetDataSources(); RegisteredServer[] rsvrs = Microsoft.SqlServer.Management.Smo.SqlServerRegistrations.EnumRegisteredServers(); foreach (RegisteredServer rs in rsvrs) { serverName = rs.ServerInstance; } } catch (Exception ex) { ex2 = ex.Message; } return serverName; } }
The returned string is "machine name"\sqlexpress. Atleast this returns something instead of object reference error. Now the next question is, the Sql server instance the solution is accessing is MSSQLSERVER2008, now why would it show Sqlexpress in the output? If the answer is because its default, how to find the instance which is being currently used. Thanks.Swetha Sankaran
Hi Swetha, I got the results you were looking for. In my case this returns more than one instance. But it does work. Try using this
Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer mc = new Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer();
foreach (Microsoft.SqlServer.Management.Smo.Wmi.ServerInstance si in mc.ServerInstances)
{
Response.Write("ServerInstance: " + si.Name);
}In my case it returns two instances MSSMLBIZ SQLEXPRESS
Cheers Disgyza Programmer Analyst
-
Hi Swetha, I got the results you were looking for. In my case this returns more than one instance. But it does work. Try using this
Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer mc = new Microsoft.SqlServer.Management.Smo.Wmi.ManagedComputer();
foreach (Microsoft.SqlServer.Management.Smo.Wmi.ServerInstance si in mc.ServerInstances)
{
Response.Write("ServerInstance: " + si.Name);
}In my case it returns two instances MSSMLBIZ SQLEXPRESS
Cheers Disgyza Programmer Analyst
-
Hey all, The RegistryKey refers to Microsoft.Win32.RegistryKey and I am on Windows Server 2003 with 64 bit. Do you all know if there is a reference for Microsoft.Win64.RegistryKey? The code looks like this now...debugging it shows that the value of serverName is NULL(no kidding Wink )
public string RetrieveDataSource { get { try { System.Data.Sql.SqlDataSourceEnumerator en = System.Data.Sql.SqlDataSourceEnumerator.Instance; DataTable dt = en.GetDataSources(); RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Microsoft SQL Server"); //using(RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft SQL Server")) String[] instances = (String[])rk.GetValue("InstalledInstances");**(This is NULL)** if (instances.Length > 0) { foreach (string element in instances) { //add if doesn't exist if (element != "MSSQLSERVER") { serverName = String.Format(System.Environment.MachineName, element); } else { serverName = System.Environment.MachineName; } } } } catch (Exception ex) { ex2 = ex.Message; } return serverName;**(This is NULL)** } }
Is there any other way of getting the instance name without this roundabout? Seemed pretty straight forward to me before i coded...now not so much. I can get the machine name directly by System.Environment.MachineName; if only there is a way to retrieve Instance name without RegistryKey hassle..i would avoid this method totally. Thanks to Sandeep, Rolcr, Disgyza and Colin Angus Mackay for helpingSwetha
-
I WILL continue to update the code and WILL delete that which is obsolete. The code that was deleted were updated and posted back based on further research and modifications.
Swetha
swetha sankaran wrote:
The code that was deleted were updated and posted back based on further research and modifications.
It looks like you have no idea of what this forum is for First do "research", then if you dont find the answer, then post, and let the post grow as more people enriches the post If you continue doing that, i WILL block your account for posting
Alexei Rodriguez