代码开启sql2005远程连接功能

来源:互联网 发布:淘宝买家恶意大量退货 编辑:程序博客网 时间:2024/06/07 14:33

    背景:去年曾经开发过一款基于单机版的进销存系统,是基于sqlexpress 2005 数据库。考虑到时单机版,所以在打包的时候将sqlexpress 也直接打到压缩包中了,这样比较方便,完全自动安装,包括初始化数据库,初始化数据都是些在代码当中,客户在安装的时候只需要点下一步即可。

      新需求:要求曾经的单机版程序需要在局域网内访问。

      解决办法:系统分为两个版本,服务版安装数据库,客户版不安装数据库。将服务版sqlexpress数据库开启远程服务,然后客户版连接服务版上的数据库。

      新问题:用户计算机水平非常低,又不能全部上门服务,要求所有操作通过代码自动实现。

      1. 设置sqlexpress数据库允许通过账号混合模式验证登陆(默认是windows身份验证)

以下为实现代码:

C#代码  收藏代码
  1. RegistryKey hklm = Registry.LocalMachine;  
  2. RegistryKey MSSQLServer = hklm.OpenSubKey("SOFTWARE"true)  
  3.                            .OpenSubKey("Microsoft"true)  
  4.                            .OpenSubKey("Microsoft SQL Server"true)  
  5.                            .OpenSubKey("MSSQL.1"true)  
  6.                            .OpenSubKey("MSSQLServer"true);  
  7. MSSQLServer.SetValue("LoginMode", 2);  

 

      2. 设置sqlexpress数据库允许远程连接访问(开启TCP/IP访问协议)

以下为实现代码:

C#代码  收藏代码
  1. RegistryKey Tcp = MSSQLServer.OpenSubKey("SuperSocketNetLib"true)  
  2.                              .OpenSubKey("Tcp"true);  
  3. Tcp.SetValue("Enabled", 1);  

 

     3. 使系统sa账号可用,并设置sa的密码为sa2005(默认sa状态为不可用)

以下为实现代码:

C#代码  收藏代码
  1. string strConn = @"Data Source=(local)\SQLEXPRESS;Initial Catalog=master;";  
  2. strConn += "trusted_connection=sspi;Connect Timeout=30";  
  3. SqlConnection conn = null;  
  4. try  
  5. {  
  6.     conn = new SqlConnection(strConn);  
  7.     conn.Open();  
  8.     SqlCommand command = new SqlCommand("Alter LOGIN sa ENABLE ; Alter LOGIN sa WITH PASSWORD = 'sa2005' ; ", conn);  
  9.     command.ExecuteNonQuery();  
  10. }  
  11. catch (Exception ex)  
  12. {  
  13.   
  14. }  
  15. finally  
  16. {  
  17.     if (conn != null)  
  18.     {  
  19.         conn.Close();  
  20.     }  
  21. }  

 

      4. 重新启动服务,并将服务设置为Automatic。

以下为实现代码:

C#代码  收藏代码
  1.     ServiceControllerEx scEx = new ServiceControllerEx("SQL Server Browser");  
  2.     scEx.StartupType = "Automatic";  
  3.     if (scEx.Status != ServiceControllerStatus.Running)  
  4.     {  
  5.         scEx.Start();            
  6.     }  
  7.     ServiceControllerEx SQLServer = new ServiceControllerEx("SQL Server (SQLEXPRESS)");  
  8.     if (SQLServer.Status == ServiceControllerStatus.Running)  
  9.     {  
  10.         SQLServer.Stop();  
  11.   
  12.         while (SQLServer.Status != ServiceControllerStatus.Stopped)  
  13.         {  
  14.             Thread.Sleep(200);  
  15.             SQLServer.Refresh();  
  16.         }  
  17.         SQLServer.Start();  
  18.         while (SQLServer.Status != ServiceControllerStatus.Running)  
  19.         {  
  20.             Thread.Sleep(200);  
  21.             SQLServer.Refresh();  
  22.         }  
  23.     }  
  24.     else  
  25.     {  
  26.         SQLServer.Start();                  
  27.     }  
  28. }  

 

     5. ServiceControllerEx 类,对系统ServiceController的扩展。

C#代码  收藏代码
  1. class ServiceControllerEx : ServiceController  
  2. {  
  3.     public ServiceControllerEx()  
  4.         : base()  
  5.     { }  
  6.     public ServiceControllerEx(string name)  
  7.         : base(name)  
  8.     { }  
  9.     public ServiceControllerEx(string name, string machineName)  
  10.         : base(name, machineName)  
  11.     { }  
  12.     public string StartupType  
  13.     {  
  14.         get  
  15.         {  
  16.             if (this.ServiceName != null)  
  17.             {  
  18.                 //construct the management path  
  19.                 string path = "Win32_Service.Name='" + this.ServiceName + "'";  
  20.                 ManagementPath p = new ManagementPath(path);  
  21.                 //construct the management object  
  22.                 ManagementObject ManagementObj = new ManagementObject(p);  
  23.                 return ManagementObj["StartMode"].ToString();  
  24.             }  
  25.             else  
  26.             {  
  27.                 return null;  
  28.             }  
  29.         }  
  30.         set  
  31.         {  
  32.             if (value != "Automatic" && value != "Manual" && value != "Disabled")  
  33.                 throw new Exception("The valid values are Automatic, Manual or Disabled");  
  34.   
  35.             if (this.ServiceName != null)  
  36.             {  
  37.                 //construct the management path  
  38.                 string path = "Win32_Service.Name='" + this.ServiceName + "'";  
  39.                 ManagementPath p = new ManagementPath(path);  
  40.                 //construct the management object  
  41.                 ManagementObject ManagementObj = new ManagementObject(p);  
  42.                 //we will use the invokeMethod method of the ManagementObject class  
  43.                 object[] parameters = new object[1];  
  44.                 parameters[0] = value;  
  45.                 ManagementObj.InvokeMethod("ChangeStartMode", parameters);  
  46.             }  
  47.         }  
  48.   
  49.     }  
  50. }  

       6. 客户端方法(为了那些计算机名都不会看的客户),检测可连接的数据库。

需要引入COM组件 Microsoft SQLDMO Object Library,无需注册。

C#代码  收藏代码
  1. SQLDMO.ApplicationClass sqlApp = new SQLDMO.ApplicationClass();  
  2. SQLDMO.NameList nameList;  
  3. int i = 0;  
  4. nameList = sqlApp.ListAvailableSQLServers();  
  5. for (i = 1; i < nameList.Count; i++)  
  6. {     
  7.     MessageBox.Show(nameList.Item(i).ToString());  
  8. }  

 

通过这6步,达到完全代码开启sqlexpress远程服务。

如果sqlexpress数据库安装在win7 系统下,需要关闭防火墙,或者为1434端口和sqlserver.exe设置例外。


转载自:http://sodart.iteye.com/blog/909594

原创粉丝点击