C# tutorial-Database support: Connection object

C# Database Access with ADO.NET


ADO.NET is the evolution of ADO (Active Data Objects). It is used to interact with a database or a data source. ADO.NET contains such useful classes to work with the databases as:

Connection
Command
DataReader
DataSet
DataAdapter

Connection Object

When you want to connect to a database, you will use the Connection object. In this part you will learn to connect to databases of Ms. Access and SQL Server. To connect to Ms. Access databases, we use System.Data.Oledb class. And to connect to SQL Server databases, we use System.Data.SqlClient class.

 

Example: Connect to Ms. Access database

OleDbConnection cn;

//Create Connection object

 

cn = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" + Application.StartupPath + "\\dbimages.mdb");

'Open Connection to the database

cn.Open();

OR

//Create Connection object

OleDbConnection cn=new OleDbConnection();

            //Set connection string

   cn.ConnectionString="Provider=Microsoft.Jet.Oledb.4.0; Data Source="+ Application.StartupPath+ "\\dbimages.mdb";

cn.Open();

 

This code will connect to Ms. Access database “dbimages.mdb” located in start up path of the current project. The database contains a table named tblimages that store image ID and its path.

 

Example: Connect to SQL Server database

 

SqlConnection cn;

//Create Connection object

cn = new SqlConnection("Server=(local); Database=dbimages; User=sa; Password=123;");

//Open connection

cn.Open();

 

OR

//Create Connection object

SqlConnection cn=new SqlConnection();

//Set connection string

cn.ConnectionString="Server=(local); Database=dbimages; User=sa; Password=123;"

//Open connection

cn.Open();

 

This code will connect to SQL Server database “dbimages” in the local machine.





Comments

KrishnaMohan comment

 KrishnaMohan

http://skillgun.com/sql/interview-questions-and-answers
Its simply superb.Feeling good to share the link to practice Csharpinterview questions

08-20-2014
Name:
Comment:
Enter the text:
CAPTCHA image

....................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Home Programming Web DB & App Questions Exercises Tips Programs FAQs Download About
Computer-Wbest
Popular Pages
You might like