Ton*_*Nam 39 configuration cmd database-connection remote-connection sql-server-express
我正在使用我的应用程序部署sql express.我希望该数据库引擎接受远程连接.我知道如何通过启动sql server配置管理器,启用tcp/ip连接,指定端口等来配置该手册.我想知道是否可以从命令行执行相同的操作.
或许我必须在visual studio中创建一个"SQL Server 2008 Server Project".
我在这里发布了相同的问题,但我想在已经安装的sql express实例上做同样的事情.看看这里的问题
我发现这些链接声称做了类似的事情,但我仍然无法使其发挥作用.
1)http://support.microsoft.com/kb/839980
4)http://datazulu.com/blog/post/Enable_sql_server_tcp_via_script.aspx
正如Krzysztof在他的回答中所说,我需要(以及我所知道的其他需要的东西)
1 - 启用TCP/IP
我已经设法在安装传递参数的SQLExpress的新实例时执行此操作 /TCPENABLED=1
.当我在这个例子中安装sql express时.sql express的实例将启用TCP/IP
2 - 在防火墙中打开正确的端口
(我已经完成了这个manualy,但我相信我将能够弄清楚如何用c#来做到这一点)到目前为止,我必须使用这个控制台命令来解决问题:
netsh firewall set portopening protocol = TCP port = 1433 name = SQLPort mode = ENABLE scope = SUBNET profile = CURRENT
Run Code Online (Sandbox Code Playgroud)
3 - 修改TCP/IP属性启用IP地址
我一直无法弄清楚如何启用IP,更改端口等.我认为这将是更复杂的解决步骤
4 - 在sql server中启用混合模式身份验证
我已经设法在安装SQL Express时执行此操作,传递参数/SECURITYMODE=SQL
参考步骤1的链接.
SQL Server Express要求此身份验证类型接受远程连接.
5 - 更改用户(sa)默认passowrd
默认情况下,sa帐户具有NULL passowrd.为了接受连接,该用户必须拥有密码.我用脚本更改了sa的默认passowrd:
ALTER LOGIN [sa] WITH PASSWORD='*****newPassword****'
Run Code Online (Sandbox Code Playgroud)
6 - 终于
如果满足所有最后步骤,将能够连接:
SQLCMD -U sa -P newPassword -S 192.168.0.120\SQLEXPRESS,1433
Run Code Online (Sandbox Code Playgroud)
通过在命令行中键入它:C#中的连接字符串将非常相似.我将不得不为用户替换-U,为密码替换-P,为数据源替换-S.我不记得确切的名字.
Krz*_*zyk 35
我使用SQL Server 2008 R2 Express测试了以下代码,我相信我们应该为您概述的所有6个步骤提供解决方案.让我们一个接一个地看待它们:
我们可以使用WMI启用TCP/IP协议:
set wmiComputer = GetObject( _
"winmgmts:" _
& "\\.\root\Microsoft\SqlServer\ComputerManagement10")
set tcpProtocols = wmiComputer.ExecQuery( _
"select * from ServerNetworkProtocol " _
& "where InstanceName = 'SQLEXPRESS' and ProtocolName = 'Tcp'")
if tcpProtocols.Count = 1 then
' set tcpProtocol = tcpProtocols(0)
' I wish this worked, but unfortunately
' there's no int-indexed Item property in this type
' Doing this instead
for each tcpProtocol in tcpProtocols
dim setEnableResult
setEnableResult = tcpProtocol.SetEnable()
if setEnableResult <> 0 then
Wscript.Echo "Failed!"
end if
next
end if
Run Code Online (Sandbox Code Playgroud)
我相信您的解决方案可行,只需确保指定正确的端口即可.我建议我们选择一个不同于1433的端口,并使其成为SQL Server Express将监听的静态端口.我将在这篇文章中使用3456,但请在实际实现中选择不同的数字(我觉得我们很快会看到很多使用3456的应用程序:-)
我们可以再次使用WMI.由于我们使用静态端口3456,我们只需更新IPAll部分中的两个属性:禁用动态端口并将侦听端口设置为3456
:
set wmiComputer = GetObject( _
"winmgmts:" _
& "\\.\root\Microsoft\SqlServer\ComputerManagement10")
set tcpProperties = wmiComputer.ExecQuery( _
"select * from ServerNetworkProtocolProperty " _
& "where InstanceName='SQLEXPRESS' and " _
& "ProtocolName='Tcp' and IPAddressName='IPAll'")
for each tcpProperty in tcpProperties
dim setValueResult, requestedValue
if tcpProperty.PropertyName = "TcpPort" then
requestedValue = "3456"
elseif tcpProperty.PropertyName ="TcpDynamicPorts" then
requestedValue = ""
end if
setValueResult = tcpProperty.SetStringValue(requestedValue)
if setValueResult = 0 then
Wscript.Echo "" & tcpProperty.PropertyName & " set."
else
Wscript.Echo "" & tcpProperty.PropertyName & " failed!"
end if
next
Run Code Online (Sandbox Code Playgroud)
请注意,我没有必要启用任何单个地址才能使其正常工作,但如果在您的情况下需要,则应该能够轻松扩展此脚本.
只是提醒一下,在使用WMI时,WBEMTest.exe是您最好的朋友!
我希望我们可以再次使用WMI,但遗憾的是,此设置不会通过WMI公开.还有两个选择:
使用LoginMode
的财产Microsoft.SqlServer.Management.Smo.Server
所描述的阶级,在这里.
在SQL Server注册表使用LoginMode值,如描述的这个帖子.请注意,默认情况下,SQL Server Express实例已命名SQLEXPRESS
,因此对于我的SQL Server 2008 R2 Express实例,正确的注册表项是
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQLServer
.
你得到了这个.
由于我们使用分配给SQL Server Express实例的静态端口,因此不再需要在服务器地址中使用实例名称.
SQLCMD -U sa -P newPassword -S 192.168.0.120,3456
Run Code Online (Sandbox Code Playgroud)
请告诉我这是否适合您(手指交叉!).
归档时间: |
|
查看次数: |
106515 次 |
最近记录: |