asp.net vb语言连接数据库代码

代码如下:

Imports System.Data
Imports System.Data.OleDb
Public Class Conn
    Public Sub New()
    End Sub
    Public ReadOnly Property Connection() As OleDbConnection
        Get
            Dim conn As New OleDbConnection(New Config().ConnStr)
            conn.Open()
            Return conn
        End Get
    End Property
    Public ReadOnly Property ReadSQL(ByVal SQL As String, ByVal Values As Array) As OleDbDataReader
        Get
            Dim cmd As New OleDbCommand(SQL, Connection())
            For i = 0 To Values.GetUpperBound(0)
                cmd.Parameters.Add(New OleDbParameter("@p" & i + 1, Values(i)))
            Next
            Return cmd.ExecuteReader()
        End Get
    End Property
    Public ReadOnly Property ExecSQL(ByVal SQL As String, ByVal Values As Array) As Integer
        Get
            Dim exec As New OleDbCommand(SQL, Connection())
            For i = 0 To Values.GetUpperBound(0)
                exec.Parameters.Add(New OleDbParameter("@p" & i + 1, Values(i)))
            Next
            Return exec.ExecuteNonQuery()
        End Get
    End Property
    Public ReadOnly Property CountSQL(ByVal Table As String, ByVal Where As String, ByVal Values As Array) As Integer
        Get
            Dim exec As New OleDbCommand("Select count(*) From [" & Table & "] Where " & Where, Connection())
            For i = 0 To Values.GetUpperBound(0)
                exec.Parameters.Add(New OleDbParameter("@p" & i + 1, Values(i)))
            Next
            Return exec.ExecuteScalar()
        End Get
    End Property

End Class
 

VS2010编译通过。

发表评论

注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

:wink: :twisted: :roll: :oops: :mrgreen: :lol: :idea: :evil: :cry: :arrow: :?: :-| :-x :-o :-P :-D :-? :) :( :!: 8-O 8)

本文链接:https://twd2.me/archives/184QrCode