安装Windows 10后,无法从VB启动/停止SQL Server [英] Can't start/stop SQL Server from VB once Windows 10 installed

查看:117
本文介绍了安装Windows 10后,无法从VB启动/停止SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Windows 7旗舰版,Visual Basic 2013和SQL Server 2014 SP1。根据这个网站,

I've been using Windows 7 Ultimate, Visual Basic 2013, and SQL Server 2014 SP1. Per this website,

https://technet.microsoft.com/en-us/library/ms162139(v = sql.90).aspx

https://technet.microsoft.com/en-us/library/ms162139(v=sql.90).aspx

我一直在使用SMO ManagedComputer对象从我的VB .Net代码中重启(关闭,然后打开)SQL Server。我这样做主要是为了在SQL Server执行一些内存密集型操作之后释放内存(我在自己的PC上使用SQL Server只是为了
)。

I've been using the SMO ManagedComputer object to restart (turn off, then turn on) SQL Server from within my VB .Net code. I do this primarily to free up memory after SQL Server does it after some memory-intensive operations (I'm using SQL Server just for myself on my own PCs).

然后,我构建了一台新的PC并安装了Windows 10 Pro(而不是Windows 7)和VB 2015(而不是VB 2013),以及完全相同的VB .Net代码,它们之前不再停止并启动SQL Server。所以我在装有Windows 7的PC上安装了VB 2015,并且代码工作了
,因此VB 2015似乎不是问题所在。 (顺便说一下,我在Windows 10和7上都使用.Net Framework 4.5.2和4.6尝试了代码,它不会改变结果,所以框架不是问题。) 因此,从Windows 7升级到
Windows 10似乎导致代码不再起作用。这是一个错误,还是我需要在Windows 10中使用不同的代码?我的猜测是Windows 10中的安全性不同。

Then, I built a new PC and installed Windows 10 Pro (instead of Windows 7) and VB 2015 (instead of VB 2013), and the exact same VB .Net code that worked before no longer stops and starts SQL Server. So I installed VB 2015 on a PC with Windows 7, and the code worked, so VB 2015 doesn't appear to be the problem. (Incidentally, I tried the code using .Net Framework 4.5.2 and 4.6 on both Windows 10 and 7, and it doesn't change the results, so the framework is not the issue).  Therefore, upgrading to Windows 10 from Windows 7 appears to be causing the code to no longer work. Is this a bug, or do I need to use different code with Windows 10? My guess is that the security is different in Windows 10.

我也尝试使用下面的代码来停止/重启SQL Server。它适用于Windows 7. 对于Windows 10,执行scService.Stop()时出现以下错误 - 这使我认为这是Windows 10的安全问题:

I also tried using the code below to stop/restart SQL Server. It works with Windows 7. With Windows 10, I get the following error when scService.Stop() is executed--this additionally makes me think it is a security issue with Windows 10:

System.ServiceProcess.dll中出现未处理的"System.InvalidOperationException"类型异常

An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceProcess.dll

其他信息:无法在计算机"ASUS5"上打开MSSQLSERVER服务。

Additional information: Cannot open MSSQLSERVER service on computer 'ASUS5'.

            Dim i, numTries As Integer
            Dim scServices(), scService As ServiceProcess.ServiceController 'need Reference to System.ServiceProcess
            scServices = ServiceProcess.ServiceController.GetServices(My.Computer.Name)
            For i = 0 To scServices.Length - 1
                scService = scServices(i)
                If scService.DisplayName = "SQL Server (MSSQLSERVER)" Then
                    If scService.Status = ServiceProcess.ServiceControllerStatus.Running Then
                        Console.WriteLine("Stopping SQL Server")
                        scService.Stop() : scService.WaitForStatus(ServiceControllerStatus.Stopped) : Thread.Sleep(500)
                    End If
TryStart:           Try
                        numTries += 1
                        scService.Start()
                        Console.WriteLine("Waiting for SQL Server to Start.")
                        scService.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)
                    Catch ex As Exception
                        If numTries <= 2 Then
                            GoTo TryStart
                        Else
                            MsgBox("Error trying to start SQL Server.  Stopping Program" & vbCr & ex.ToString, MsgBoxStyle.Critical, "Restart SQL Server")
                            Stop
                        End If
                    End Try
                    Exit For
                End If
            Next


如何使用Windows 10在VB .Net代码中启动/停止SQL Server?


How do I start/stop SQL Server within VB .Net code using Windows 10?

推荐答案

您有哪个版本的SQL Server? 更重要的是,您是否已将SQL Server安装为默认实例或命名实例?

Which edition of SQL Server do you have?  And more to the point, have you installed SQL Server as a default instance or a named instance?


这篇关于安装Windows 10后,无法从VB启动/停止SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆