以编程方式更改SQL Server设置

Irf*_*aza 7 c# sql-server

我请你仔细阅读我的问题.

您可能知道在安装带有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设置等.

任何人都可以为我提供帮助,我怎么能这样做?

感谢您分享宝贵的时间.

Ale*_*exS 9

您应该使用SQL Server管理对象(SMO) - 这是一个用于以编程方式管理SQL Server的API.

更新:

证明有点棘手:Server.LoginMode(读/写),Server.TcpEnabled和Server.NamedPipesEnabled(遗憾的是只获取).为了修改协议,您需要检查Microsoft.SqlServer.Management.Smo.Wmi命名空间(因此来自"另一端"):

  • ServerProtocol - 表示服务器协议
  • ServerProtocolCollection - 给定服务器上定义的所有协议的集合


Rez*_*eri 5

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)

  • 这是一个很好的解决方案,需要做一些事情:1)添加对Microsoft.SqlServer.ConnectionInfo,Microsoft.SqlServer.Management.Sdk.Sfc,Microsoft.SqlServer.Smo,Microsoft.SqlServer.SqlEnum,Microsoft.SqlServer.SqlWmiManagement的引用,以及在C:\ Program Files(x86)\ Microsoft SQL Server\100\SDK\Assemblies中找到的Microsoft.SqlServer.WmiEnum 2)使用Microsoft.SqlServer.Management.Smo并使用Microsoft.SqlServer.Management.Smo.Wmi添加. (3认同)