我请你仔细阅读我的问题.
您可能知道在安装带有SQL Server Express版本的VS2005/2008时,SQL Server默认以Windows身份验证模式运行.您可以使用SQL Server Management Studio将模式更改为混合模式(Windows和SQL Server身份验证模式).
同样允许通过TCP/IP的SQL Server的远程连接,你需要使用SQL Server配置管理器,然后选择协议SQLEXPRESS,然后更改TCP/IP选项的设置.
我需要的是使用C#以编程方式自动执行此过程.也就是说,我需要编写ac#program来改变模式或改变tcp/ip设置等.
任何人都可以为我提供帮助,我怎么能这样做?
感谢您分享宝贵的时间.
C#中的此功能将启用TCP/IP协议并将登录模式设置为混合模式.
见补充信息在这里.
这是代码:
private static bool SetServerProperties()
{
#region standardize Connection String
string tempCatalog = "master";
string temp = @"Data Source=" + dataSource + ";Initial Catalog=" + tempCatalog + ";Integrated Security=True;MultipleActiveResultSets=True";
#endregion
SqlConnection sqlconnection = new SqlConnection(temp);
SqlCommand cmd = new SqlCommand("select @@ServerName", sqlconnection);
sqlconnection.Open();
string serverName = "";
try
{
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
serverName = dr[0].ToString();
}
catch
{
MessageBox.Show("Failed to Set SQL Server Properties for remote connections.");
}
Server srv = new Server(serverName);
srv.ConnectionContext.Connect();
srv.Settings.LoginMode = ServerLoginMode.Mixed;
ManagedComputer mc = new ManagedComputer();
try
{
Service Mysvc = mc.Services["MSSQL$" + serverName.Split('\\')[1]];
if (Mysvc.ServiceState == ServiceState.Running)
{
Mysvc.Stop();
Mysvc.Alter();
while (!(string.Format("{0}", Mysvc.ServiceState) == "Stopped"))
{
Mysvc.Refresh();
}
}
ServerProtocol srvprcl = mc.ServerInstances[0].ServerProtocols[2];
srvprcl.IsEnabled = true;
srvprcl.Alter();
Mysvc.Start();
Mysvc.Alter();
while (!(string.Format("{0}", Mysvc.ServiceState) == "Running"))
{
Mysvc.Refresh();
}
return true;
}
catch
{
MessageBox.Show("TCP/IP connectin could not be enabled.");
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7267 次 |
| 最近记录: |