我想知道如何在SQL Server 2005的VB.net中以編程方式進行數據庫的備份。如果您有關於它的想法,那麼請給我這個代碼。如何在VB.net中備份sql server 2005的數據庫使用代碼
-1
A
回答
1
您可以通過使用Sql管理對象來實現此目的。 SQL Server管理對象(SMO)是爲Microsoft SQL Server的程序化管理而設計的對象。
參考How to: Back Up Databases and Transaction Logs in Visual Basic .NET
2
1
Imports System.IO
Imports System.Data.SqlClient
Public Class Form2
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim dread As SqlDataReader
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
server(".")
server(".\sqlexpress")
End Sub
Sub server(ByVal str As String)
con = New SqlConnection("Data Source=" & str & ";Database=Master;integrated security=SSPI;")
con.Open()
cmd = New SqlCommand("select * from sysservers where srvproduct='SQL Server'", con)
dread = cmd.ExecuteReader
While dread.Read
ComboBox1.Items.Add(dread(2))
End While
dread.Close()
con.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ComboBox1.Text = "" Then
MsgBox("select the server name")
ComboBox1.Focus()
Exit Sub
End If
Try
ComboBox2.Text = ""
con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;")
con.Open()
If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
ComboBox2.Text = System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog1.FileName)
cmd = New SqlCommand("drop database " & ComboBox2.Text & " ", con)
cmd.ExecuteNonQuery()
cmd = New SqlCommand("RESTORE DATABASE " & ComboBox2.Text & " FROM disk='" & OpenFileDialog1.FileName & "'", con)
cmd.ExecuteNonQuery()
MsgBox("Restore Successfully Completed")
ComboBox1.Text = ""
ComboBox2.Text = ""
Else
MsgBox("Restore Cancelled")
End If
con.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComboBox1.Text = "" Then
MsgBox("select the server name")
ComboBox1.Focus()
Exit Sub
ElseIf ComboBox2.Text = "" Then
MsgBox("select the database name")
ComboBox2.Focus()
Exit Sub
End If
Try
con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;")
con.Open()
SaveFileDialog1.FileName = ComboBox2.Text + ".bak"
If SaveFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim s As String
s = SaveFileDialog1.FileName
cmd = New SqlCommand("backup database " & ComboBox2.Text & " to disk='" & s & "'", con)
cmd.ExecuteNonQuery()
MsgBox("Back Up Successfully Created")
ComboBox1.Text = ""
ComboBox2.Text = ""
con.Close()
Else
MsgBox("Back Up Cancelled")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
con = New SqlConnection("Data Source=" & Trim(ComboBox1.Text) & ";Database=Master;integrated security=SSPI;")
con.Open()
ComboBox2.Items.Clear()
cmd = New SqlCommand("select * from sysdatabases", con)
dread = cmd.ExecuteReader
While dread.Read
ComboBox2.Items.Add(dread(0))
End While
dread.Close()
con.Close()
End Sub
End Class
相關問題
- 1. 如何備份數據庫備份每天在SQL Server 2005中
- 2. SQL Server 2005數據庫備份
- 3. 如何在SQL Server 2005中導入MS Access數據庫表在vb.net代碼
- 4. Microsoft SQL Server 2005備份* .sql中的數據庫
- 5. 還原Sql Server 2005數據庫備份到Sql Server 2000
- 6. 如何備份live sql server 2005 express數據庫
- 7. 我可以使用SQL Server 2008 R2 Express備份SQL Server 2005數據庫嗎
- 8. 備份SQL Server數據庫
- 9. 用於小型數據庫的SQL Server 2005備份
- 10. 如何從SQL Server 2008數據庫獲得SQL Server 2005兼容備份?
- 11. Vb.net 2008與SQL Server 2005數據庫
- 12. 在ASP.NET中備份SQL Server數據庫
- 13. SQL Server 2008中的數據庫備份
- 14. SQL Server 2005的完整數據庫備份到磁盤
- 15. DBA - SQL Server 2005 - 備份
- 16. 如何在vb.net備份我的sql數據庫
- 17. 如何備份使用SMO的SQL Server 2012數據庫
- 18. 使用SQL Server 2005還原數據庫
- 19. SQL Server 2005中的壓縮備份
- 20. 如何使用數據庫備份創建SQL Server複製?
- 21. 如何在SQL Server 2005中恢復SQL SERVER 2008數據庫
- 22. 如何在sql server 2005中編寫sql server數據庫圖表?
- 23. 備份數據時在SQL Server 2005中執行哪個文件
- 24. 製作SQL Server數據庫的備份
- 25. 在C#中使用SMO備份SQL Server數據庫
- 26. 使用VB.Net附加SQL Server Express數據庫代碼
- 27. 如何在XP中備份本地SQL Server數據庫?
- 28. 備份如何在SQL Server中工作?
- 29. SQL Server Management Studio中2005 - 備份位置
- 30. 使用visual studio在備份數據庫服務器上使用的備份本地sql server數據庫表
我已經提到我的答案的文章必須幫助你,但那麼你必須拿出代碼,按您的要求。 – 2010-04-30 11:08:34