我想确定用户是否安装了SQL Server Express 2014。这个版本对我很重要。然后我想确保这个用户有实例" SQLEXPRESS"在他的2014年服务器上。
如果安装了SQLEXPRESS,我有一个返回布尔值的函数,但没有考虑版本(2008/2010/2012/2014)
Private Function SQLExpressInstalled() As Boolean
Try
Using key As RegistryKey = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Microsoft SQL Server\\", False)
If key Is Nothing Then Return False
Dim strNames() As String
strNames = key.GetSubKeyNames
'If we cannot find a SQL server registry key, we don't have SQL Server Express installed
If strNames.Length = 0 Then Return False
If strNames.Contains("SQLEXPRESS") Then
Return True
End If
End Using
Catch ex As Exception
'MsgBox(ex.Message)
End Try
Return False
End Function
有没有办法确定版本以及给定版本上安装的实例?
答案 0 :(得分:1)
我可以使用SqlDataSourceEnumerator展示如何使用它的一个小例子,但我不确定它是否适合你。我让你测试一下
using System.Data.Sql;
SqlDataSourceEnumerator sqe = SqlDataSourceEnumerator.Instance;
DataTable dt = sqe.GetDataSources();
// Here the DataTable has a column called Version,
// but in my tests it is always null, so let's go with
// the SELECT @@version approach
foreach (DataRow row in dt.Rows)
{
SqlConnectionStringBuilder scb = new SqlConnectionStringBuilder();
scb.DataSource = row.Field<string>("ServerName");
if(!row.IsNull("InstanceName"))
scb.DataSource += "\\" + row.Field<string>("InstanceName");
// Another major problem is the Authetication rules for the
// current instance, I just assume that IntegratedSecurity works also for you
// scb.UserID = "xxxx";
// scb.Password = "xxxx";
scb.IntegratedSecurity = true;
scb.InitialCatalog = "master";
using (SqlConnection cnn = new SqlConnection(scb.ConnectionString))
using (SqlCommand cmd = new SqlCommand("SELECT @@Version", cnn))
{
Console.WriteLine("Version for: " + row.Field<string>("ServerName"));
cnn.Open();
string result = cmd.ExecuteScalar().ToString();
// Now a bit of parsing will be required to isolate the information needed
Console.WriteLine(result));
}
}
答案 1 :(得分:0)
我从marc_s&#39;中找到了另一个解决方案。帖子here
代码是:
Private Sub SQLInformation()
' open the 64-bit view of the registry, if you're using a 64-bit OS
Dim baseKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
' find the installed SQL Server instance names
Dim key As RegistryKey = baseKey.OpenSubKey("SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL")
' loop over those instances
For Each sqlInstance As String In key.GetValueNames()
Console.WriteLine("SQL Server instance: {0}", sqlInstance)
' find the SQL Server internal name for the instance
Dim internalName As String = key.GetValue(sqlInstance).ToString()
Console.WriteLine(vbTab & "Internal instance name: {0}", internalName)
' using that internal name - find the "Setup" node in the registry
Dim instanceSetupNode As String = String.Format("SOFTWARE\Microsoft\Microsoft SQL Server\{0}\Setup", internalName)
Dim setupKey As RegistryKey = baseKey.OpenSubKey(instanceSetupNode, False)
If setupKey IsNot Nothing Then
' in the "Setup" node, you have several interesting items, like
' * edition and version of that instance
' * base path for the instance itself, and for the data for that instance
Dim edition As String = setupKey.GetValue("Edition").ToString()
Dim pathToInstance As String = setupKey.GetValue("SQLBinRoot").ToString()
Dim version As String = setupKey.GetValue("Version").ToString()
Console.WriteLine(vbTab & "Edition : {0}", edition)
Console.WriteLine(vbTab & "Version : {0}", version)
Console.WriteLine(vbTab & "Path to instance: {0}", pathToInstance)
End If
Next
End Sub