Help with Oledb

Christopherx

Well-known member
Joined
Jul 4, 2010
Messages
58
Programming Experience
Beginner
VB.NET:
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.OleDb

Module Module1

    Public Class AccessHelper
        Private Shared connectionstring As String

        Shared Sub New()


            'ACCESS CONNECTION STRING
            Dim builder As New OleDbConnectionStringBuilder

            builder("Provider") = "microsoft.jet.OLEDB.4.0"

            builder("Data source") = "C:\Users\Chris\Documents\db1.mdb"


            connectionstring = builder.ConnectionString

        End Sub


        Public Shared Function ExecuteNonQuery(ByVal sql As String, ByVal params() As OleDbParameter) As Integer


            Dim cnn As New OleDbConnection(connectionstring)


            Dim cmd As New OleDbCommand(sql, cnn)

            For i As Integer = 0 To params.Length - 1
                cmd.Parameters.Add(params(i))
            Next
            cnn.Open()
            Dim retval As Integer = cmd.ExecuteNonQuery()
            cnn.Close()
            Return retval
        End Function

        Public Shared Function ExecuteNonQuery(ByVal sql As String) As Integer


            Dim cnn As New OleDbConnection(connectionstring)


            Dim cmd As New OleDbCommand(sql, cnn)

            cnn.Open()
            Dim retval As Integer = cmd.ExecuteNonQuery()
            cnn.Close()
            Return retval
        End Function

        Public Shared Function ExecuteDataSet(ByVal sql As String, ByVal datamembername As String) As DataSet

            Dim ds As New DataSet

            Dim da As New OleDbDataAdapter(sql, connectionstring)

            da.Fill(ds, datamembername)
            Return ds
        End Function


        Public Shared Function ExecuteDataSet(ByVal sql As String, ByVal params() As OleDbParameter) As DataSet


            Dim ds As New DataSet

            Dim da As New OleDbDataAdapter(sql, connectionstring)

            For i As Integer = 0 To params.Length - 1
                da.SelectCommand.Parameters.Add(params(i))
            Next
            da.Fill(ds)
            Return ds
        End Function


    End Class
End Module

How could i write code to pass the correct variables to search the database? Just a mock up would be nice! :)
 
You could start by reading the documentation for the OleDbParameter class. Once you understand that class, then you'll know how to create them as you need them. If there's something specific that you read that you don't understand, ask about it here.
 
Why would you write a class like this when you can get Visual Studio to generate you a working one in a few seconds?
Add your access database to the Server Explorer
Add a dataset to your project
Drag the access database tables you wish to use, onto the the dataset

Youre done. For more info ,read the DW3 link in my signature, section "Creating a Simple Data App"
 
Back
Top