Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 185,129 times

Contents

Downloads

Related Categories

Creating a database connection - Introduction

Tony Ho

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

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!

Comments

  • Re: [2459] Creating a database connection

    Posted by grace_qt on 21 Feb 2007

    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

    <...

  • Re: Connection to database with password

    Posted by grace_qt on 21 Feb 2007

    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...

  • Connection to database with password

    Posted by vnz on 23 Apr 2006

    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...

  • hi Try this

    Posted by onlyamir007 on 08 May 2005

    string connStr;
               string qryStr;
               OleDbConnection dbConn;
               
    connStr = "Provider=Microsoft.Jet.OL...

  • c# database connection with m.s acesss

    Posted by ratnakar on 05 Aug 2003

    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 ...