以编程方式更改SQL Server设置
我请你仔细阅读我的问题。
您可能知道在安装带有SQL Server Express版本的VS2005 / 2008时,SQL Server默认以Windows身份validation模式运行。 您可以使用SQL Server Management Studio将模式更改为混合模式(Windows和SQL Server身份validation模式)。
与允许通过TCP / IP进行SQL Server远程连接类似,您需要使用SQL Server配置管理器,然后选择SQLEXPRESS协议,然后更改Tcp / IP选项的设置。
我需要的是使用C#以编程方式自动执行此过程。 也就是说,我需要编写ac#program来改变模式或更改tcp / ip设置等。
任何人都可以为我提供帮助,我怎么能这样做?
感谢您分享宝贵的时间。
您应该使用SQL Server管理对象( SMO ) – 这是一个用于以编程方式管理SQL Server的API。
更新:
certificate有点棘手:Server.LoginMode(读/写),Server.TcpEnabled和Server.NamedPipesEnabled(遗憾的是只获取)。 为了修改协议,您需要检查Microsoft.SqlServer.Management.Smo.Wmi命名空间(因此来自“另一端”):
- ServerProtocol – 表示服务器协议
- ServerProtocolCollection – 给定服务器上定义的所有协议的集合
C#中的此function将启用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; } }
那么修改注册表呢?
客户端协议设置存储在此处:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ MSSQLServer \ Client \ SNI9.0签出ProtocolOrder。
身份validation模式存储在此处:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Microsoft SQL Server \ MSSQL.1 \ MSSQLServer \ LoginMode
请参阅: validation设置
通过从C#执行此存储过程,我能够以较小的占用空间执行此操作:
USE [master] EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2 GO
它看起来并不多,但可以完美无缺地立即工作,无需重启服务。
我认为您可以使用安装过程的配置文件来解决您的SQL Server Express版静默安装问题。
在此链接中,您可以找到安装的命令行参数。
在本文中,您可以找到如何制作配置文件。
- Marshal.PtrToStructure抛出System.ArgumentException错误
- 我们可以使用Response.Flush()而不是Response.End()
- 在SpecRun / SpecFlow测试执行报告中插入屏幕截图
- 将C#reflection代码移植到Metro-Ui
- 调试器Visualizer和“类型未标记为可序列化”
- C#:”和’bool’之间的隐式转换
- “Product:*”部分中未解决的符号’Property:NETFRAMEWORK45’的引用
- RegisteredTfsConnections.GetProjectCollection我得到nullexception
- 如何将数据读取器转换为动态查询结果