Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code
Start/Stop SQL Server through Code Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
Go Back  Xtreme Visual Basic Talk > > > Start/Stop SQL Server through Code


Reply
 
Thread Tools Display Modes
  #1  
Old 05-05-2005, 07:01 AM
19d20Scout 19d20Scout is offline
Newcomer
 
Join Date: Mar 2005
Posts: 5
Question Start/Stop SQL Server through Code


Here is my problem. I'm fairly new to VB.net and I somewhat understand functions. The following code is something I found on the internet and for some reason the stop function works but the start function doesn't. I did change the Best Handler code to what I think will use the error Property procedure at the bottom of the list. The goal I'm trying to accomplish is to stop the server so I can restore my MSDE database from a .bak file and then restart the server once the restore is complete. I would also like to know if there is any need to check if the SQL server is running on program load.

Code:
Private Sub txtStartServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtStartServer.Click ServerName = txtServerName.Text UserName = txtUserName.Text Password = txtPassword.Text StartSQLServer(ServerName, UserName, Password) End Sub Public Function StartSQLServer(ByVal ServerName As String, ByVal Username As String, ByVal Password As String) On Error GoTo BestHandler SQLS.Start(False, ServerName, Username, Password) ServerStatusing() Exit Function BestHandler: ErrorNumber = Err.Number ErrorDescription = Err.Description End Function Private Sub btnStopServer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStopServer.Click ServerName = txtServerName.Text StopSQLServer(ServerName) End Sub Public Function StopSQLServer(ByVal ServerName As String) On Error GoTo BestHandler SQLS.Name = ServerName SQLS.Stop() ServerStatusing() Exit Function BestHandler: ErrorNumber = ErrorNum ErrorDescription = ErrorDes End Function Public Property ErrorNum() As Object Get ErrorNum = ErrorNumber End Get Set(ByVal ErrorNum As Object) End Set End Property
Reply With Quote
  #2  
Old 05-09-2005, 09:39 AM
DTFan's Avatar
DTFan DTFan is offline
Junior Contributor
 
Join Date: Jul 2003
Location: Atlanta, Georgia
Posts: 230
Default

Well, I tried doing what it appears you are doing. It looks to me (and this is my first attempt at stopping/starting a SQL Server through code) that you're using the MS SQLDMO Object. Did you actually Add the Reference to it in your Solution? If not, do that first.

Quote:
Right-Click References --> Add Reference --> Click "COM" Tab -- > Microsoft SQLDMO Object Library
Anyway, I created a blank form. Then I did "Imports SQLDMO". Then I put two buttons on it (btnStop and btnStart), and coded them like this:

Code:
' the buttton to stop the currently running server Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click On Error GoTo StopServerErr SQLS.Name = "MyMachineName\ServerName" ' ServerName SQLS.Stop() Exit Sub StopServerErr: MsgBox("Error Stopping Server" & Environment.NewLine & _ "Number: " & Err.Number & Environment.NewLine & _ "Description: " & Err.Description, MsgBoxStyle.Exclamation, "Error Stopping Server") End Sub ' the code to start the server Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click On Error GoTo StartServerErr SQLS.Start(False, "MyMachineName\ServerName", "MyUserName", "MyPassword") Exit Sub StartServerErr: MsgBox("Error Starting Server" & Environment.NewLine & _ "Number: " & Err.Number & Environment.NewLine & _ "Description: " & Err.Description, MsgBoxStyle.Exclamation, "Error Starting Server") End Sub

In terms of checking if the server is already running I woul dthink that would be a good thing to do. When my server wasn't running and I attempted to stop it I did get the error message. So I would either put a Try-Catch inside the buttons (in which case you'll have to remove the OnError lines), or first Check if the server is stopped or started and then run the code accordingly.

I see that you're also interested in BackUp and Restore. I have not tried this yet. I've got some stuff I've got to take care of but I'll see if I can figure that part out. If I can, I will also post here. But perhaps in the meantime somebody else will be able to assist you with that part of it.

Hope this helped a little (at least you know the code itself does work). And like I said, if I can figure out the Backup/Restore part, I'll post that code here also.
__________________

DTFan
Ever-hopeful programmer-in-training
:)
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
Start/Stop SQL Server through Code Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
 
Start/Stop SQL Server through Code
Start/Stop SQL Server through Code
 
-->