16,553
社区成员
发帖
与我相关
我的任务
分享
Dim sql2 As String = "RESTORE DATABASE Northwind From DISK= c:\test'' With REPLACE,RESTART" '恢复数据库
单引号写错位置了。
Dim sql2 As String = "RESTORE DATABASE Northwind From DISK= 'c:\test' With REPLACE,RESTART" '恢复数据库
Public Class Form1
Private ConnString As String = "Data Source=(local);Initial Catalog=tempdb;Integrated Security=True"
'执行SQL语句
Public Function ExecuteSQL(ByVal strSQL As String) As DataSet
Dim SplitSQL() As String = Split(strSQL)
Dim sqlConnection As New System.Data.SqlClient.SqlConnection(Me.ConnString)
Try
sqlConnection.Open()
If InStr("INSERT,DELETE,UPDATE", UCase(SplitSQL(0))) Then
Dim sqlCommand As New System.Data.SqlClient.SqlCommand(strSQL, sqlConnection)
sqlCommand.ExecuteNonQuery()
sqlCommand.Dispose()
Else
Dim rst As System.Data.DataSet = New System.Data.DataSet
Dim adpt As New System.Data.SqlClient.SqlDataAdapter(strSQL, sqlConnection)
adpt.Fill(rst) : adpt.Dispose()
Return rst
End If
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
sqlConnection.Close()
End Try
Return Nothing
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sql As String = "backup database Northwind to disk='c:\test'" '备份数据库
Dim sql2 As String = "RESTORE DATABASE Northwind From DISK= c:\test'' With REPLACE,RESTART" '恢复数据库
Call ExecuteSQL(sql)
End Sub
End Class