G
Guest
I was told to post this in one of the dotnet groups. I hope this is the
correct one.
Several months ago, I took the official Microsoft 2733B course to upgrade my
skills from SQL 2000 to SQL 2005 and one of the neat things the course did
was show us how to create an SMO object which backs up a database at the
click of a button. Now that we're finally moving to SQL Server 2005 at work,
I went to whip up this little program following the instructions at work, but
I'm having a problem.
When I create this project from scratch, it gives me the error: Name
"DatabaseList" is not declared. This is really strange as when I pull up the
sample solution from the CD we got, everything is almost exact the same,
including the IMPORTS. Can someone help me figure out what I'm missing that
VS thinks "DatabaseList" is a variable instead of a real object? Form is
simple, 1 textbox and 1 button. Code is listed below:
'##### Add imports statements here #####
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Public Class DBBackupForm
'##### Add variable declarations hereâ€
Dim myServer As New Server()
Dim conn As ServerConnection
Private Sub BackupDatabaseButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles BackupDatabaseButton.Click
' If no database is selected, exit this event handler
If DatabaseList.SelectedIndex = -1 Then Exit Sub
'##### Add backup code here #####
Dim dbName As String = DatabaseList.SelectedItem.ToString
Dim MyBackup As New Backup
Dim ProcessDate As Date
ProcessDate = Now()
Dim BackupTime As String
BackupTime = ProcessDate.ToString("YYYY") &
ProcessDate.ToString("MM") _
& ProcessDate.ToString("DD") & ProcessDate.ToString("HH") _
& ProcessDate.ToString("MM")
MyBackup.Action = BackupActionType.Database
MyBackup.BackupSetName = dbName & "Backup"
MyBackup.Database = dbName
Dim MyDevice As BackupDeviceItem = New BackupDeviceItem( _
"\\MyServerName\SQL_BAK\" & dbName & BackupTime & ".BAK",
DeviceType.File)
MyBackup.Devices.Add(MyDevice)
MyBackup.SqlBackup(myServer)
MessageBox.Show(dbName & " backed up in the SQL_BAK folder with the
following FileName: " _
& CStr("\\MyServerName\SQL_BAK\" & dbName & BackupTime & ".BAK"))
End Sub
Private Sub DatabaseListTextBox_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
DatabaseListTextBox.TextChanged
'##### Add connection code here #####
conn = myServer.ConnectionContext
conn.ServerInstance = "localhost"
conn.Connect()
'##### Add list database code here #####
Dim db As Database
For Each db In myServer.Databases
DatabaseList.Items.Add(db.Name)
Next
End Sub
End Class
correct one.
Several months ago, I took the official Microsoft 2733B course to upgrade my
skills from SQL 2000 to SQL 2005 and one of the neat things the course did
was show us how to create an SMO object which backs up a database at the
click of a button. Now that we're finally moving to SQL Server 2005 at work,
I went to whip up this little program following the instructions at work, but
I'm having a problem.
When I create this project from scratch, it gives me the error: Name
"DatabaseList" is not declared. This is really strange as when I pull up the
sample solution from the CD we got, everything is almost exact the same,
including the IMPORTS. Can someone help me figure out what I'm missing that
VS thinks "DatabaseList" is a variable instead of a real object? Form is
simple, 1 textbox and 1 button. Code is listed below:
'##### Add imports statements here #####
Imports Microsoft.SqlServer.Management.Smo
Imports Microsoft.SqlServer.Management.Common
Public Class DBBackupForm
'##### Add variable declarations hereâ€
Dim myServer As New Server()
Dim conn As ServerConnection
Private Sub BackupDatabaseButton_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles BackupDatabaseButton.Click
' If no database is selected, exit this event handler
If DatabaseList.SelectedIndex = -1 Then Exit Sub
'##### Add backup code here #####
Dim dbName As String = DatabaseList.SelectedItem.ToString
Dim MyBackup As New Backup
Dim ProcessDate As Date
ProcessDate = Now()
Dim BackupTime As String
BackupTime = ProcessDate.ToString("YYYY") &
ProcessDate.ToString("MM") _
& ProcessDate.ToString("DD") & ProcessDate.ToString("HH") _
& ProcessDate.ToString("MM")
MyBackup.Action = BackupActionType.Database
MyBackup.BackupSetName = dbName & "Backup"
MyBackup.Database = dbName
Dim MyDevice As BackupDeviceItem = New BackupDeviceItem( _
"\\MyServerName\SQL_BAK\" & dbName & BackupTime & ".BAK",
DeviceType.File)
MyBackup.Devices.Add(MyDevice)
MyBackup.SqlBackup(myServer)
MessageBox.Show(dbName & " backed up in the SQL_BAK folder with the
following FileName: " _
& CStr("\\MyServerName\SQL_BAK\" & dbName & BackupTime & ".BAK"))
End Sub
Private Sub DatabaseListTextBox_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
DatabaseListTextBox.TextChanged
'##### Add connection code here #####
conn = myServer.ConnectionContext
conn.ServerInstance = "localhost"
conn.Connect()
'##### Add list database code here #####
Dim db As Database
For Each db In myServer.Databases
DatabaseList.Items.Add(db.Name)
Next
End Sub
End Class