在VB.NET中如何运用SQL查询语句?

hxd209 2009-06-08 12:17:42
比如要运用一条SQL语名备份数据库,还用进行别的统计平均成绩......谢谢
...全文
162 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
outou 2009-06-08
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 wzuomin 的回复:]
VB.NET codePublic 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
sqlConnect…
[/Quote]
支持
wzuomin 2009-06-08
  • 打赏
  • 举报
回复
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" '恢复数据库
wzuomin 2009-06-08
  • 打赏
  • 举报
回复
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



相应的sql语句如何写,lz去查SQLServer2000的帮助文档即可找到答案。呵呵呵

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧