初学者不防试试以下代码。。
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
'*(********************************************
相关视频
相关阅读 数据库流行度排行2019年9月 数据库排行榜2019年最新版fifa online4数据库大全 fifa online4球员数据库在哪myeclipse怎么连接到数据库 myeclipse连接到数据库方法PLSQL Developer怎么导出数据库PLSQL Developer配置Oralce11g连接plsql developer怎么使用 plsql developer使用教程plsql developer怎么连接数据库 plsql developer连接数据库教程mysql数据库root密码忘记的修改方法
热门文章 没有查询到任何记录。
最新文章
ADO在vb.net中的使用和
vb script基本的窗体操作多文档程序Visualasic概念Visualasic概述
人气排行 ADO在vb.net中的使用和方法VB基础教程(字符串运算符)第三章(2)VB VC混合编程疑难问题解VB基础教程(常用控件)第二章(3)教你如何用VB.net连接数据库VB基础教程( 程序调试 )第四章(4)VB基础教程(创建窗体)第二章(2)VB基础教程(Visual Basic概述)第一章(1)
查看所有0条评论>>