SQLDMO在VB中的应用

前端之家收集整理的这篇文章主要介绍了SQLDMO在VB中的应用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sqlDMO在VB中的应用

http://blog.csdn.net/cncco/archive/2010/03/09/5362111.aspx

sqlDMO(sql Distributed Management Objects,sql分布式管理对象)封装 Microsoft sql Server 2000 数据库中的对象。sql-DMO 允许用支持自动化或 COM 的语言编写应用程序,以管理 sql Server 安装的所有部分。 sql-DMO 是 sql Server 2000 中的 sql Server 企业管理器所使用的应用程序接口 (API);因此使用 sql-DMO 的应用程序可以执行 sql Server 企业管理器执行的所有功能

  sql-DMO 用于必须包含 sql Server 管理的任何自动化或 COM 应用程序,例如:

  1. 封装 sql Server 作为自己的数据存储并想尽量减少用户sql Server 管理任务的应用程序。

  2. 在程序本身并入了专门的管理逻辑的应用程序。

  3. 想在自己的用户界面中集成 sql Server 管理任务的应用程序。

  sqlDMO对象来自sqlDMO.dll,sqlDMO.dll是随sql Server2000一起发布的。sqlDMO.dll自身是一个COM对象,因此,在你的.NET项目里必须先引用它。

  得到网络中的sql服务器的列表:

'得到sql服务器的列表
'必须安装sql SERVER 2000 SP2 及以上版本

Dim I As Short
Dim sqlApp As New sqlDMO.Application()
Dim ServerName As sqlDMO.NameList
ServerName = sqlApp.ListAvailablesqlServers
For i = 1 To ServerName.Count
cbServer.Items.Add(ServerName.Item(i))
Next

  得到指定sql服务器所有数据库的列表:

'得到指定sql服务器所有数据库的列表

Dim sqlApp As New sqlDMO.Application()
Dim oServer As New sqlDMO.sqlServer()
oServer.Connect("(local)","sa","sa")
cboDatabase.Items.Clear()
Dim db As sqlDMO.Database
For Each db In oServer.Databases
Me.cboDatabase.Items.Add(db.Name)
Next

  得到所有的表、视图、存储过程:

Dim I As Short
Dim oServer As New sqlDMO.sqlServer()
oServer.Connect("(local)","sa")
Dim db As New sqlDMO.Database()

For I = 1 To oServer.Databases.Count
If oServer.Databases.Item(I,"dbo").Name = "Northwind" Then Exit For
Next
If I > oServer.Databases.Count Then Exit Sub

db = oServer.Databases.Item(I,"dbo")
ListBox1.Items.Clear()
'得到所有的存储过程
For I = 1 To db.StoredProcedures.Count
ListBox1.Items.Add(db.StoredProcedures.Item(I,"dbo").Name)
Next
'得到所有的表
For I = 1 To db.Tables.Count
ListBox1.Items.Add(db.Tables.Item(I,"dbo").Name)
Next
' 得到所有的视图
For I = 1 To db.Views.Count
ListBox1.Items.Add(db.Views.Item(I,"dbo").Name)
Next


利用sqlDMO实现带进度条的数据库备份:

'添加进度条ProgressBar1控件
'引用Microsoft sqlDMO Object Library
'声明

Public WithEvents bkps As sqlDMO.Backup

'数据库备份操作
Private Sub btnBackUp_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles btnBackUp.Click
Dim osqlServer As New sqlDMO.sqlServer()
osqlServer.LoginSecure = False
osqlServer.Connect("(local)","sa") '连接服务器
Me.Cursor = Windows.Forms.Cursors.WaitCursor
bkps = CreateObject("sqlDMO.Backup")
bkps.Database = "Northwind" '指定需备份的数据库
bkps.Action = 0
bkps.Files = "f:/Northwind.bak" '指定备份文件
bkps.Initialize = True
ProgressBar1.Value = 0
ProgressBar1.Maximum = 100
Me.Cursor = Windows.Forms.Cursors.Default()
Application.DoEvents()
Dim mouseCur As Cursor
Me.Cursor = Windows.Forms.Cursors.WaitCursor
bkps.sqlBackup(osqlServer)
ProgressBar1.Value = 100
Application.DoEvents()
bkps = Nothing
Me.Cursor = Windows.Forms.Cursors.Default()
MsgBox("数据库备份完成",MsgBoxStyle.Information,"系统消息")
End Sub

'显示进度

Private Sub bkps_PercentComplete(ByVal Message As String,ByVal Percent As Integer) Handles bkps.PercentComplete
ProgressBar1.Value = ProgressBar1.Maximum * (Percent / 100)
End Sub

自动发布MS sql SERVER数据库

当您的MIS系统开发好以后,您如何尽快的分发您的数据库呢?

下面我将提供VB代码的具体实现:

Option Explicit


'define the sql connect
Dim osqlServer As sqlDMO.sqlServer
Dim oCurrentDB As sqlDMO.Database
Dim oCurrentTable As sqlDMO.Table
Dim oTestIdx As sqlDMO.Index

'sqlDmo is Connected Yes or No
Public sName As String
Private IsConnected As Boolean

'connect to local database server
Public Function ConnectDmo() As Boolean
On Error GoTo HandleError
'If we're connected,then disconnect and clear lists.
If IsConnected = True Then
osqlServer.DisConnect
IsConnected = False
End If
'Begin connect to sqlserver or msde
'Attempt a connection,then fill the properties stuff.
osqlServer.ApplicationName = "sql-DMO Index Test"
osqlServer.LoginSecure = True

'connect
osqlServer.Connect "(local)",""

IsConnected = True
ConnectDmo = True

HandleError:
'connect failth
If IsConnected = False Then
IsConnected = False
ConnectDmo = False
End If

End Function

'add a exists database to server
Public Function AddDataBase(ByVal dbName As String,ByVal DBPath As String,ByVal rstr As String) As Boolean
Dim rstring
'rstring = osqlServer.AttachDBWithSingleFile(dbName,DBPath)
rstring = osqlServer.AttachDB(dbName,DBPath)
AddDataBase = True


rstr = rstring
End Function

'delete the exists database
Public Function DelDataBase(ByVal dbName As String) As Boolean
Dim rstring
rstring = osqlServer.DetachDB(dbName)
DelDataBase = True
End Function

Private Sub UserControl_Initialize()
On Error GoTo merror
Set osqlServer = New sqlDMO.sqlServer
osqlServer.LoginTimeout = 15
osqlServer.ODBCPrefix = False
Name = "msdeconn1"
merror:
End Sub

Public Function isDBexists(ByVal dbName As String) As Boolean
Dim oDB As sqlDMO.Database
Dim rc As Boolean
'reconnect to database
osqlServer.DisConnect
osqlServer.ReConnect

rc = False
For Each oDB In osqlServer.Databases
If oDB.SystemObject = False Then
If Trim(UCase(oDB.Name)) = Trim(UCase(dbName)) Then
rc = True
End If
End If
Next oDB

'set the return value
isDBexists = rc

End Function

Private Sub UserControl_Terminate()
'end connect the database
osqlServer.Close
End Sub

Public Function startServer()
osqlServer.Start True
End Function

Public Sub stopserver()
osqlServer.Stop
End Sub


Public Property Get Name() As Variant
Name = sName
End Property

Public Property Let Name(ByVal vNewValue As Variant)
sName = vNewValue

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/cncco/archive/2010/03/09/5362111.aspx

原文链接:https://www.f2er.com/vb/262044.html

猜你在找的VB相关文章