VB.NET tutorial: Database access with ADO.NET

Database access with ADO.NET



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
Dim cn As OleDb.OleDbConnection
'Create Connection object
 
cn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\dbimages.mdb")
'Create Connection object
cn.Open()
OR
'Create Connection object
Dim cn As New OleDb.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.

If the database has password protection (e.g. 123), the connection string should be modified to:
cn.ConnectionString="Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\dbimages.mdb; Jet OLEDB:Database Password=123;"

For newer Ms. Access database (2007+), you need to use another provider called Microsoft.ACE.OLEDB.12.0 rather than Microsoft.Jet.Oledb.4.0.

 

ms access database vb 


Example: Connect to MS SQL Server database
Dim cn As SqlClient.SqlConnection
'Create Connection object
cn = New SqlClient.SqlConnection("Server=(local); Database=dbimages; User=sa; Password=123;")
'Open connection
cn.Open()
 
OR
'Create Connection object
Dim cn As New SqlClient.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 that uses SQL Server authentication. If you use the Windows authentication
, You will need to use the integrated security=SSPI; instead of the user name and password. So the connecting string will look like the following:

"Server=(local); Database=dbimages; integrated security=SSPI;"




Comments

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