VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • vb.net 操作Access数据库帮助类

VB.net 操作Access数据库的一些常用方法,代码如下


  1.  
    Imports System
  2.  
    Imports System.Data.SqlClient
  3.  
    Imports System.Configuration
  4.  
    Imports System.Collections.Generic
  5.  
    Imports System.Text
  6.  
    Imports System.Data
  7.  
    Imports System.Configuration.ConfigurationSettings
  8.  
    Imports System.Data.OleDb
  9.  
     
  10.  
     
  11.  
     
  12.  
    Public Class DB
  13.  
     
  14.  
     
  15.  
     
  16.  
    Private Shared com As OleDb.OleDbCommand
  17.  
    Private Shared reader As OleDb.OleDbDataReader
  18.  
    Private Shared adapter As OleDb.OleDbDataAdapter
  19.  
    Private Shared conn As OleDb.OleDbConnection
  20.  
     
  21.  
    ''' <summary>
  22.  
    ''' 获取数据库连接
  23.  
    ''' </summary>
  24.  
    ''' <value></value>
  25.  
    ''' <returns></returns>
  26.  
    ''' <remarks></remarks>
  27.  
    Public Shared ReadOnly Property NewConn() As OleDbConnection
  28.  
    Get
  29.  
    Dim connectionString As String
  30.  
    'connectionString = System.Configuration.ConfigurationSettings.GetConfig("Supermarket")
  31.  
    '连接2010数据库
  32.  
    'connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\GCU.accdb"
  33.  
    '连接03-07数据库
  34.  
    connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=F:\JL\GCU.MDB"
  35.  
     
  36.  
    '应该在这里先判断conn是否为Nothing
  37.  
    If conn Is Nothing Then
  38.  
    conn = New OleDb.OleDbConnection(connectionString)
  39.  
    End If
  40.  
    If conn.State <> ConnectionState.Open Then
  41.  
    conn.Open()
  42.  
    End If
  43.  
    Return conn
  44.  
    End Get
  45.  
    End Property
  46.  
     
  47.  
    ''' <summary>
  48.  
    ''' 执行增删改(无参)
  49.  
    ''' </summary>
  50.  
    ''' <param name="sql">执行的Sql语句</param>
  51.  
    ''' <returns></returns>
  52.  
    ''' <remarks></remarks>
  53.  
    Public Shared Function ExecuteNonQuery(ByVal sql As String) As Integer
  54.  
    com = New OleDb.OleDbCommand(sql, NewConn())
  55.  
    Return com.ExecuteNonQuery()
  56.  
    End Function
  57.  
     
  58.  
    ''' <summary>
  59.  
    ''' 执行增删改(有参)
  60.  
    ''' </summary>
  61.  
    ''' <param name="sql"></param>
  62.  
    ''' <param name="para"></param>
  63.  
    ''' <returns></returns>
  64.  
    ''' <remarks></remarks>
  65.  
    Public Shared Function ExecuteNonQuery(ByVal sql As String, ByVal para As OleDbParameter()) As Integer
  66.  
    com = New OleDb.OleDbCommand(sql, NewConn)
  67.  
    com.Parameters.AddRange(para)
  68.  
    Return com.ExecuteNonQuery()
  69.  
    End Function
  70.  
     
  71.  
    ''' <summary>
  72.  
    ''' 执行增删改的存储过程
  73.  
    ''' </summary>
  74.  
    ''' <param name="para"></param>
  75.  
    ''' <param name="ProcedureName"></param>
  76.  
    ''' <returns></returns>
  77.  
    ''' <remarks></remarks>
  78.  
    Public Shared Function ExecuteNonQuery(ByVal para As SqlParameter(), ByVal ProcedureName As String) As Integer
  79.  
    Dim cmd As OleDb.OleDbCommand
  80.  
    cmd = New OleDb.OleDbCommand()
  81.  
    cmd.Connection = NewConn()
  82.  
    cmd.CommandText = ProcedureName
  83.  
    cmd.CommandType = CommandType.StoredProcedure
  84.  
    cmd.Parameters.AddRange(para)
  85.  
    Return com.ExecuteNonQuery
  86.  
    End Function
  87.  
     
  88.  
    ''' <summary>
  89.  
    ''' 执行查询(返回一个结果集,无参)
  90.  
    ''' </summary>
  91.  
    ''' <param name="sql"></param>
  92.  
    ''' <returns></returns>
  93.  
    ''' <remarks></remarks>
  94.  
    Public Shared Function GetScalar(ByVal sql As String) As String
  95.  
    Dim dataset As DataSet
  96.  
    dataset = New DataSet()
  97.  
    com = New OleDb.OleDbCommand(sql, NewConn)
  98.  
    adapter = New OleDbDataAdapter(com)
  99.  
    adapter.Fill(dataset)
  100.  
    If dataset.Tables.Count > 0 And dataset.Tables(0).Rows.Count > 0 Then
  101.  
    Return dataset.Tables(0).Rows(0)(0).ToString()
  102.  
    End If
  103.  
    Return "Null"
  104.  
    End Function
  105.  
     
  106.  
    ''' <summary>
  107.  
    ''' 执行查询(返回一个结果集,有参)
  108.  
    ''' </summary>
  109.  
    ''' <param name="sql"></param>
  110.  
    ''' <param name="para"></param>
  111.  
    ''' <returns></returns>
  112.  
    ''' <remarks></remarks>
  113.  
    Public Shared Function GetScalar(ByVal sql As String, ByVal para As SqlParameter()) As Integer
  114.  
    com = New OleDb.OleDbCommand(sql, NewConn)
  115.  
    com.Parameters.AddRange(para)
  116.  
    Return Convert.ToInt32(com.ExecuteScalar())
  117.  
    End Function
  118.  
     
  119.  
    ''' <summary>
  120.  
    ''' 执行查询(返回一行数据,无参)
  121.  
    ''' </summary>
  122.  
    ''' <param name="sql"></param>
  123.  
    ''' <returns></returns>
  124.  
    ''' <remarks></remarks>
  125.  
    Public Shared Function GetReader(ByVal sql As String) As OleDbDataReader
  126.  
    com = New OleDb.OleDbCommand(sql, NewConn)
  127.  
    reader = com.ExecuteReader()
  128.  
    Return reader
  129.  
    End Function
  130.  
     
  131.  
    ''' <summary>
  132.  
    ''' 执行查询(返回一行数据,有参)
  133.  
    ''' </summary>
  134.  
    ''' <param name="sql"></param>
  135.  
    ''' <param name="para"></param>
  136.  
    ''' <returns></returns>
  137.  
    ''' <remarks></remarks>
  138.  
    Public Shared Function GetReader(ByVal sql As String, ByVal para As SqlParameter()) As OleDbDataReader
  139.  
    com = New OleDb.OleDbCommand(sql, NewConn)
  140.  
    com.Parameters.AddRange(para)
  141.  
    reader = com.ExecuteReader()
  142.  
    Return reader
  143.  
    End Function
  144.  
     
  145.  
    ''' <summary>
  146.  
    ''' 执行查询(返回一个数据集,无参)
  147.  
    ''' </summary>
  148.  
    ''' <param name="sql"></param>
  149.  
    ''' <returns></returns>
  150.  
    ''' <remarks></remarks>
  151.  
    Public Shared Function GetDataSet(ByVal sql As String) As DataTable
  152.  
    Dim dataset As DataSet
  153.  
    dataset = New DataSet()
  154.  
    com = New OleDb.OleDbCommand(sql, NewConn)
  155.  
    adapter = New OleDbDataAdapter(com)
  156.  
    adapter.Fill(dataset)
  157.  
    Return dataset.Tables(0)
  158.  
    End Function
  159.  
     
  160.  
     
  161.  
    ''' <summary>
  162.  
    ''' 执行查询(返回一个数据集,有参)
  163.  
    ''' </summary>
  164.  
    ''' <param name="sql"></param>
  165.  
    ''' <param name="para"></param>
  166.  
    ''' <returns></returns>
  167.  
    ''' <remarks></remarks>
  168.  
    Public Shared Function GetDataSet(ByVal sql As String, ByVal para As SqlParameter()) As DataTable
  169.  
    Dim dataset As DataSet
  170.  
    dataset = New DataSet()
  171.  
    com = New OleDbCommand(sql, NewConn)
  172.  
    com.Parameters.AddRange(para)
  173.  
    adapter = New OleDbDataAdapter(com)
  174.  
    adapter.Fill(dataset)
  175.  
    Return dataset.Tables(0)
  176.  
    End Function
  177.  
     
  178.  
     
  179.  
     
  180.  
    End Class
  181.  
     
 

相关教程