分类分类
关注+2009-10-21作者:
初学者不防试试以下代码。。 Option Explicit On Option Strict On Imports Microsoft.VisualBasic Imports System.Data.SqlClient Imports System.Data.Common Imports System.Data.OleDb Imports System.Data.Odbc Imports System.data 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() Try 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 Try 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 Try 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 '*(********************************************
相关文章
更多+相同厂商
热门推荐
点击查看更多
点击查看更多
点击查看更多
说两句网友评论