文章导航PC6首页软件下载单机游戏安卓资源苹果资源

pc软件新闻网络操作系统办公工具编程服务器软件评测

安卓新闻资讯应用教程刷机教程安卓游戏攻略tv资讯深度阅读综合安卓评测

苹果ios资讯苹果手机越狱备份教程美化教程ios软件教程mac教程

单机游戏角色扮演即时战略动作射击棋牌游戏体育竞技模拟经营其它游戏游戏工具

网游cf活动dnf活动lol周免英雄lol礼包

手游最新动态手游评测手游活动新游预告手游问答

您的位置:首页技术开发VB Script → 教你如何用VB.net连接数据库

教你如何用VB.net连接数据库

时间:2009/10/21 9:30:00来源:本站整理作者:我要评论(0)


   初学者不防试试以下代码。。
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)