Visual Basic .NET Forums    

Go Back   Visual Basic .NET Forums > Database > MS Access

VB.NET Forums Newsletter Signup:
Email address:


MS Access Related discussions using Access and JET as the backend

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-22-2008, 11:38 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Nov 2008
Age: 31
Posts: 2
Reputation: 0
onestarpro is on a distinguished programming path ahead
Lightbulb Help with Database class

ok I am trying to create a database class to access an access database (2000 Sorry).

Now i get an error in my class stating Keywork provider is not a valid key word can someone help me here is my code.

Code:
Imports System.Data.SqlClient

Public Class clsDB
    Private database_ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\mydb.mdb;"
    Private database_Connection As New SqlConnection()
    Private database_Sql_Statement As String = ""
    Public Reader As SqlDataReader

    Public Sub OpenDatabaseConnection()
        Try
            database_Connection.ConnectionString = database_ConnectionString
            database_Connection.Open()
        Catch ex As Exception
            Addlvitems("DataBase Connection Error: " + ex.Message.ToString(), "System")
        End Try
    End Sub
    Public Sub CloseDatabaseConnection()
        Try
            Reader.Close()
            If database_Connection.State <> Data.ConnectionState.Closed Then
                Reader.Close()
                database_Connection.Close()
            End If
        Catch ex As Exception
            Addlvitems("DataBase Close Error: " + ex.Message.ToString(), "System")
        End Try
    End Sub

    Public Function RunSqlStatement(ByVal SQL_Statement As String, ByVal where As String) As String
        RunSqlStatement = ""
        Try
            Dim cmd As New System.Data.SqlClient.SqlCommand(SQL_Statement, database_Connection)
            Reader = cmd.ExecuteReader
            If Reader.HasRows Then
                Do While Reader.Read()
                    RunSqlStatement = Reader.Item(where)
                Loop
            Else
                RunSqlStatement = " "
            End If

            Return RunSqlStatement
        Catch ex As Exception
            Addlvitems("Sql Error: " + ex.Message.ToString(), "System")
        End Try
    End Function

End Class
ps if someone has a easy db class i can use I would love to try it.
Reply With Quote
  #2 (permalink)  
Old 11-23-2008, 1:05 PM
Troy's Avatar
VB.NET Forum Genius
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Feb 2005
Age: 39
Posts: 153
Reputation: 53
Troy is on a distinguished programming path ahead
Default

Hi, ok for starters The first thing you need to do is change a few lines.

To open an access DB with code you you will need to specify it as OLEDB

This line:

Quote:
Imports System.Data.SqlClient
Should be:

Code:
Imports System.Data.OleDB
this line:

Quote:
Private database_Connection As New SqlConnection()
should be this:

Code:
Private database_Connection As New OleDbConnection()
Next you will need to change this line:

Quote:
Dim cmd As New System.Data.SqlClient.SqlCommand(SQL_Statement, database_Connection)
to this:

Code:
Dim cmd As New OleDbCommand(SQL_Statement, database_Connection)
That should get you working .
__________________
Visual Studio Professional 2008
Microsoft Vista Utimate 64 bit
Reply With Quote
  #3 (permalink)  
Old 11-23-2008, 3:42 PM
VB.NET Forum Newbie
.NET Framework: .NET 3.5 (VS 2008)
 
Join Date: Nov 2008
Age: 31
Posts: 2
Reputation: 0
onestarpro is on a distinguished programming path ahead
Default thanks

thanks for the help and the code for everyone else

Dim vbnoob() as string{"youranoob", "Imanoob", "wearevbnoobs"}

Code:
Imports System.Data


Public Class clsDB
    Private database_ConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\smartftp.mdb;"
    Private database_Connection As New System.Data.OleDb.OleDbConnection()
    Private database_Reader As System.Data.OleDb.OleDbDataReader


    Public Sub OpenDatabaseConnection()
        Try
            database_Connection.ConnectionString = database_ConnectionString
            database_Connection.Open()
        Catch ex As Exception
            Addlvitems("DataBase Connection Error: " + ex.Message.ToString(), "System")
        End Try
    End Sub
    Public Sub CloseDatabaseConnection()
        Try
            database_Reader.Close()
            If database_Connection.State <> Data.ConnectionState.Closed Then
                database_Reader.Close()
                database_Connection.Close()
            End If
        Catch ex As Exception
            Addlvitems("DataBase Close Error: " + ex.Message.ToString(), "System")
        End Try
    End Sub

    Public Function RunSqlStatement(ByVal SQL_Statement As String, ByVal where As String) As String
        RunSqlStatement = ""

        Try
            Dim cmd As New System.Data.OleDb.OleDbCommand(SQL_Statement, database_Connection)
            database_Reader = cmd.ExecuteReader
            If database_Reader.HasRows Then
                Do While database_Reader.Read()
                    RunSqlStatement = database_Reader.Item(where)
                Loop
            Else
                RunSqlStatement = " "
            End If

            Return RunSqlStatement
        Catch ex As Exception
            Addlvitems("Sql Error: " + ex.Message.ToString(), "System")
        End Try
    End Function

End Class
Reply With Quote
  #4 (permalink)  
Old 11-24-2008, 8:16 AM
cjard's Avatar
VB.NET Forum All-Mighty
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Apr 2006
Age: 64
Posts: 5,375
Reputation: 539
cjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalistcjard VB.NET gold medalist
Default

If you want to make a proper, modern class (set of classes) for accessing a database, read the DW2 link in my signature. The datasets you will generate are the database access layer
__________________
"it's a fax from your dog, Mr Dansworth. It looks like your cat" - Gary Larson...DW1: Data Walkthroughs 1.1...DW2: Data Walkthroughs 2.0...DDS: The DataSet Designer Surface...ANO: ADO.NET2 Orientation...DAN: Deeper ADO.NET...DNU...PQ
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT -4. The time now is 11:42 PM.




Click to advertise here

Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
For advertising opportunities click here.