0

我需要从 sql server 中检索正在运行的 SQL server 的默认路径。我知道应该是这样的工作:

declare @dir nvarchar(4000) 
exec master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\Setup',
N'SQLPath', 
@dir output
select @dir

现在我担心的是使用哪些参数名称从 .NET 调用此存储过程

Using sqlConn As New SqlConnection(connectionstring)
 Using cmd As New SqlCommand("xp_instance_regread", sqlConn)
            Parameter= New SqlParameter("@THE_FIRST_PARAMETER"...

我怎么能得到那个?是否有一些像 sp_help 这样的存储过程,但提供了有关 sqlparameters 的更多详细信息?谢谢你。

4

2 回答 2

3

我将添加有效的代码,以防万一有人需要它:)

Public Shared Function GetSqlPath(ByVal connectionstring As String) As String
    Dim rowCount = 0
    Using sqlConn As New SqlConnection(connectionstring)
        Using cmd As New SqlCommand("xp_instance_regread", sqlConn)
            cmd.CommandType = CommandType.StoredProcedure
            Dim parameterRootKey As New SqlParameter With {.SqlDbType = SqlDbType.NVarChar,
                                                           .ParameterName = "@rootkey",
                                                           .Size = 128,
                                                            .Direction = ParameterDirection.Input, .Value = "HKEY_LOCAL_MACHINE"
                                                          }

            Dim parameterKey As New SqlParameter With {.SqlDbType = SqlDbType.NVarChar,
                                                       .ParameterName = "@key",
                                                       .Size = 128,
                                                          .Direction = ParameterDirection.Input,
                                                        .Value = "SOFTWARE\Microsoft\MSSQLSERVER\Setup"
                                                        }

            Dim parameterValueName As New SqlParameter With {.SqlDbType = SqlDbType.NVarChar,
                                                             .ParameterName = "@value_name",
                                                             .Size = 128,
                                                          .Direction = ParameterDirection.Input, .Value = "SQLPath"
                                                        } '                                              
            Dim parameterValue As New SqlParameter With {.SqlDbType = SqlDbType.NVarChar,
                                                         .ParameterName = "@value",
                                                         .Size = 128,
                                                        .Direction = ParameterDirection.Output
                                                        }
            cmd.Parameters.Add(parameterRootKey)
            cmd.Parameters.Add(parameterKey)
            cmd.Parameters.Add(parameterValueName)
            cmd.Parameters.Add(parameterValue)
            sqlConn.Open()
            cmd.ExecuteNonQuery()

            Return CType(parameterValue.Value, String)
        End Using
    End Using
于 2013-02-20T00:58:42.480 回答
1

Even if there is some information online, xp_instance_regread is undocumented and that means there's a risk in using it: a new service pack may change it completely.

It's not entirely clear what you mean by the "default path" (path to what?), but the SMO Server and Information classes have properties for all the 'obvious' paths that you might need. In this case I think you're asking about the RootDirectory property.

于 2013-02-18T21:17:08.410 回答