在vb.net中,我正在尝试备份我的SQL Server 2012数据库。当我运行代码时,它可以正确地从本地数据库备份,但是当我将连接更改为服务器(本地服务器)时,它不起作用。我认为问题出在服务器的安全性上... 欢迎提供任何解决这个问题的方法。以下是我的代码:
Sub server(ByVal str As String)
con = New SqlConnection("Data Source=" & str & ";Initial Catalog=DATABASE;Persist Security Info=True;User ID=username;Password=password")
con.Open()
cmd = New SqlCommand("select * from sysservers where srvproduct='SQL Server'", con)
dread = cmd.ExecuteReader
While dread.Read
cmbserver.Items.Add(dread(2))
End While
dread.Close()
End Sub
Sub connection()
con = New SqlConnection("Data Source=192.168.0.200;Initial Catalog=database;Persist Security Info=True;User ID=username;Password=password")
con.Open()
cmbdatabase.Items.Clear()
cmd = New SqlCommand("select * from sysdatabases", con)
dread = cmd.ExecuteReader
While dread.Read
cmbdatabase.Items.Add(dread(0))
End While
dread.Close()
End Sub
Sub query(ByVal que As String)
On Error Resume Next
cmd = New SqlCommand(que, con)
cmd.ExecuteNonQuery()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Try
If PRGBackup.Value = 100 Then
Timer1.Enabled = False
PRGBackup.Visible = False
MsgBox("successfully the backup has been done in the specified folder.")
Else
PRGBackup.Value = PRGBackup.Value + 5
End If
Catch ex As Exception
MsgBox(ex.Message, , projectTitle)
End Try
End Sub
Sub blank(ByVal str As String)
Try
If cmbserver.Text = "" Or cmbdatabase.Text = "" Then
MsgBox("Server Name or Database can not be blank.")
Exit Sub
Else
If str = "backup" Then
SFDBackup.FileName = cmbdatabase.Text
SFDBackup.ShowDialog()
Timer1.Enabled = True
PRGBackup.Visible = True
Dim s As String
s = SFDBackup.FileName
Dim sql As String = "BACKUP DATABASE " & cmbdatabase.Text & " to disk='" & s & "'"
query(sql)
ElseIf str = "restore" Then
OFDBackup.ShowDialog()
Timer1.Enabled = True
PRGBackup.Visible = True
query("RESTORE DATABASE " & cmbdatabase.Text & " FROM disk='" & OFDBackup.FileName & "'")
End If
End If
Catch ex As Exception
MsgBox(ex.Message, , projectTitle)
End Try
End Sub
Private Sub cmbbackup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbbackup.Click
blank("backup")
End Sub
Private Sub cmdrestore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdrestore.Click
blank("restore")
End Sub
Private Sub frmBackup_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Me.Cursor = Cursors.WaitCursor
server("192.168.0.200")
'server(".\sqlexpress")
Me.Cursor = Cursors.Default
Catch ex As Exception
MsgBox(ex.Message, , projectTitle)
End Try
End Sub