Library tutorials & articles

Creating a database connection

Introduction

Microsoft Visual Studio .NET is a new revolution in computer programming. I am grateful that Microsoft developed such a great IDE for us. .NET had a lot of changes to the way we program and what we program in (especially in Visual Basic). ADO.NET had also a substantial difference from its previous versions. It's almost a completely new language! :)

I'll show how to create a DB Connection through either OLE or SQL in runtime using C# as well as VB. Drag and dropping in the IDE is really easy, but there are times when you need to do it in runtime. SQL and OLE both have their own namespaces in the .NET Framework.  They contain every single object and method that relate to that type of connection. Of course there is the namespace for all the General Data handling, which includes objects like the dataset.

OLE = System.Data.OleDB
SQL = System.Data.SqlClient
General Data = System.Data

Step #1: Declare the Connection Object

for OLE:
    VB:  Friend WithEvents OleConn as new System.Data.OleDB.OleDBConnection()
    C#:  internal System.Data.OleDB.OleDBConnection OleConn;

for SQL:
    VB:  Friend WithEvents SqlConn As New System.Data.SqlClient.SqlConnection()
    C#:  internal System.Data.SqlClient.SqlConnection SqlConn;

Some may wonder why I applied the WithEvents and Friend or internal modifiers to the objects.

Reason for WithEvents:
    -We can create procedures to handle the connection's events... like the StateChanged event

Reason for internal or Friend:
    -Make it available throughout the whole project

Comments

  1. 02 Apr 2009 at 10:41
    Richard, You cannot use the JET OLEDB libraries in Vista or 2008 x64 running your .NET Applications in 64bit mode. So in your 'Configuration Management' change it to use x86 and it should work. If your using the 'Any CPU' profile, the .NET CLR will use the 64bit CLR when your .NET application runs which is where your problem lies. Hope this helps :-)
  2. 26 Mar 2009 at 20:58
    I'm having a tough time connecting to an Access database using Visual Basic 2008. My connection string is "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Shep\Documents\Databasess\Contacts.mdb" but no mater what/how I try I get an exception. The message is "The Microsoft.Jet.OLEDB.4.0 provider is not registered on the local machine." I have no idea how to cope with this exception and have not been able to get any help anywhere else. Can someone 'splain this exception and help me overcome it? I'm running 64 bit Vista if that is of any consequence. Richard
  3. 19 Sep 2008 at 06:41

    Thanks for taking the time to post this great little sample, complete with a working app and source code. It was very helpful to me. May you get laid constantly in college, graduate with a nice CS degree, and land a job at a hot game developer outfit where you get paid to have fun building stuff while making a ton of money. Cool 

  4. 21 Feb 2007 at 03:08
    hi i am trying to connect my vb. net prog tosql can yoi help me on how to do that..
    here is my codes..please tell my the error and what else do i need to put..
    Try





            Dim LintCount As Integer
            Dim resultValue As Boolean
            Dim myreader As SqlDataReader
            Dim conn As New SqlConnection("Datasource = TASDI;" + "userId = test;" + "password = test;")
            Dim myConnection As SqlConnection
            Dim myCommand As SqlCommand
            Dim strsql As String
           '     Call LSubfrmCnlenabled(True)


                myConnection = New SqlConnection("userId=Test;" + "password =Test;" + "datasource=TASDI;")
                establishing connection. you need to provide password for sql server
                myConnection.Open()
                opening the connection
                myCommand = New SqlCommand("Select * from M_EMPLOYEE", myConnection)
            executing the command and assigning it to connection

                myreader = myCommand.ExecuteReader()
                While myreader.Read()


                End While
                myreader.Close()
                myConnection.Close()
            Catch ex As Exception
            End Try



    please reply




























  5. 21 Feb 2007 at 01:32
    Open connection to password protected Access database:
    "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\App1\Your_Database_Name.mdb; Jet OLEDB:Database Password=Your_Password"
  6. 23 Apr 2006 at 05:52

    Hi..
    I'm trying to connect to an access database that is protected by password. but, it's always says "the database is being opened exclusively by another program" even I don't open other application. Please help me to solve this problem

  7. 08 May 2005 at 06:36
                                                 string connStr;
               string qryStr;
               OleDbConnection dbConn;
               
                                                    connStr = "Provider=Microsoft.Jet.OLEDB.4.0; " +
               "Data Source=G:\\Backup\\Kashif\\xyzDB.mdb";
               qryStr = "select content,websiteID  from content";
               dbConn = new OleDbConnection(connStr);
                           

    Amir
  8. 05 Aug 2003 at 04:55

    Plz reply me when i ma writing in c# using System.Data.Ado for the database connection with m.s acess then it is giving errror  the type name space ado does not  exist in in the class system.Data.ADO
    what is reason
    plz answer me

  9. 16 May 2003 at 16:15

    I keep getting an error when I try and create a new data connection. This is the error I'm getting.


    Unable to connect to database.
    It is only possible to connect to SQL Server Desktop Engine databases and Microsoft Access with this version of Visual Studio.



    I can connect to Access without any problems. Is there anyway around this?
    I'm using VB .net Standard with SQL Server 2000.


    Thanks for any help.

  10. 16 Apr 2003 at 07:21

    Sir,
    I am presently engaged in doing networking project in VB.NET and MSAcces.
    I would like to know the connection string
    (Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.......) for connecting to a database in another machine(given its machine name) in  a network while sitting in my own machine(also in the same network). I would be grateful if you help. Expecting your early reply,
    Thank you
    Renjini S
    Please mail me at
    srenjini@sify.com

  11. 29 Nov 2002 at 10:53

    It´s ok. works good

  12. 01 Jan 1999 at 00:00

    This thread is for discussions of Creating a database connection.

Leave a comment

Sign in or Join us (it's free).

Tony Ho Currently attending Sophmore Year at Henry Clay High School, I have recently started on .NET and created a couple of sample projects. Working on my MCAD!

Related discussion

Related podcasts

  • More jQuery in ASP.NET

    In this episode Chris Brandsma, Rick Strahl, Dave Ward, Bertrand Le Roy, and Scott Koon conclude their discussion of Microsoft's jQuery in ASP.NET announcement1.This episode of the Alt.NET Podcast is brought to you by LLBLGen Pro, the most mature O/R mapper and code generator out there.Are ...

Events coming up

  • Dec 9

    GL.net Group Meeting - December 2009

    Gloucester, United Kingdom

    The beginning of this year holiday season will belong to mocks. Ronnie and Stephen will take us for a tour around exciting world of unit testing.

Want to stay in touch with what's going on? Follow us on twitter!