【www.hzclsc.cn--热门资讯】
Public Class dataload
"-----------------------------连接不明数据库种类代码部分---------------------------
"**************设置要连接的数据库种类和连接方式******************
Private stroledbprovider As String = System.Data.OleDb "大小写 -------更改此处可连接不同类型的数据库
"连接数据库的信息,更改连接不同数据库信息------- Provider=Microsoft.Jet.OLEDB.4.0;Data Source=datapath;Persist Security Info=false
Private stroledbconn As String = Provider=SQLOLEDB;Data Source=localhost,10000;Initial Catalog=haofefe;user id=sa ; password=123 "Integrated Security=SSPI
"*********************************************************************
"************生成Dbproviderfactory,idbconnection,idbcommand,and idatareader********
Dim cnfactory As IDbConnection
Dim drcustsreader As IDataReader
Dim cmfactory As IDbCommand
Dim dpfactory As DbProviderFactory
Public login As Boolean = False
Private Sub createconn()
dpfactory = System.Data.Common.DbProviderFactories.GetFactory(stroledbprovider)
cnfactory = dpfactory.CreateConnection
cnfactory.ConnectionString = stroledbconn
cmfactory = cnfactory.CreateCommand
cmfactory.CommandType = CommandType.Text
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
"*********************************************************
"利用生成的连接
"****************查询数据**************
Public Function getsources(ByVal strcomm As String) As DataTable
Dim i As Integer
Call createconn() "调用生成实例
cmfactory.CommandText = strcomm
getsources = New DataTable
cnfactory.Open()
drcustsreader = cmfactory.ExecuteReader(CommandBehavior.KeyInfo)
With drcustsreader
For i = 0 To .FieldCount - 1
getsources.Columns.Add(.GetName(i))
Next
While .Read
Dim objcells(.FieldCount - 1) As Object
.GetValues(objcells)
getsources.Rows.Add(objcells)
End While
End With
drcustsreader.Close()
"getsources.Load(drcustsreader)
Return getsources
cnfactory.Close()
Catch ex As Exception
cnfactory.Close()
Return New Data.DataTable
MsgBox(ex.ToString)
End Try
End Function
"**********************************
"-------------------------------------------------------------------------------------------------------------
"*******************查看已连接信息******************
Public Sub connectionstatistics(ByVal conn As SqlConnection)
Dim htstats As Hashtable
htstats = CType(conn.RetrieveStatistics, Hashtable)
Dim strstats As String
strstats = ServerVersion: + conn.ServerVersion.ToString + ControlChars.CrLf
Dim ostat As Object
Dim strstat As String
For Each ostat In htstats.Keys
strstat = ostat.ToString
If InStr(strstat, Time ) 0 Then
strstats = strstats + strstat + = + Microsoft.VisualBasic.Format(CLng(htstats(strstat)) / 1000, #,##0.000 ) + secs + vbCrLf
Else
strstats = strstats + strstat + = + htstats(strstat).ToString + ControlChars.Cr + ControlChars.Lf
End If
Next
MsgBox(strstats, MsgBoxStyle.Information, Connection Statistics )
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
"*(********************************************
本文来源:http://www.hzclsc.cn/news/35409.html