“ Database [DatabaseName]”中出现语法错误提示
Here is the screenshot of the error
我不知道查询中有什么错误。
这是此问题的代码:
Private Sub btnBackUp_Click(sender As Object, e As EventArgs) Handles btnBackUp.Click
Try
Me.Cursor = Cursors.WaitCursor
Dim Filename As String
get_servername = cmbservername.Text.Trim
get_userid = txtuser.Text.Trim
get_password = txtpassword.Text.Trim
database = cmbdatabase.Text
If cmbauth.SelectedIndex = 0 Then
constr = "Data Source=" & get_servername & ";Initial Catalog=" & cmbdatabase.Text & "; Integrated Security= True "
Else
constr = "Data Source=" & get_servername & ";Initial Catalog=" & cmbdatabase.Text & "; user id=" & get_userid & ";password=" & get_password & ";Integrated Security=false"
End If
con = New SqlConnection(constr)
con.Open()
Dim strquery As String
Dim save_dialog As New SaveFileDialog
save_dialog.FileName = database
save_dialog.ShowDialog()
Filename = save_dialog.FileName
strquery = "BACKUP DATABASE" & database & " TO DISK ='" & Filename & "'"
Me.Cursor = Cursors.Default
Try
com = New SqlCommand(strquery, con)
data_affector = com.ExecuteNonQuery
MessageBox.Show("Backup has been successfully created", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.Cursor = Cursors.Default
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End Try
Catch ex As Exception
Me.Cursor = Cursors.Default
MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Try
End Sub
答案 0 :(得分:0)
您在查询中缺少空格
strquery = "BACKUP DATABASE" & database & " TO DISK ='" & Filename & "'"
如果数据库的值为Northwinds
,则查询将从BACKUP DATABASENorthwinds
开始。尝试更新以添加空间:
strquery = "BACKUP DATABASE " & database & " TO DISK ='" & Filename & "'"