1:36 PM How to connect to different databases like MS access database, MS SQL server or Oracle using VB .NET? |
In this article we shall learn how to connect to several databases using Microsoft visual studio or VB .NET. We shall practice the simplest code to connect to each database one by one.
1. MS Access Database:
Here now we shall understand the code. First of all we are declaring a connection con which represents the connection to a data source. Secondly we have declared a connection string which will be used to connect to the database. Here connection string is the most important property of a connection. Connection string consists of following attributes:
You can choose or create any connection string from here. Next is we have set the connection string property of the connection. We put them in try-catch block to handle any exception. con.ConnectionString = constr In the above code we have commented out one connection string. That is valid when we are connecting to an encrypted MS access database. In that case we need to give password. The database can be encrypted using a password when you open it in exclusive mode in MS access. And then we open the connection using con.open(). If the connection string is not correct, the connection to the database will fail. So please make sure that all the attributes of the connection string is correct. 2. Oracle 10g
Here again the only difference is in connection string. Provider is "MSDAORA.1" and the userid and password are the credential to connect to the database. 3. MS Sql Server
While connecting to MS Sql server we have used three different connection strings for 3 different cases. Case 1: Connecting to Sql server SHANKAR and the database Teachers. Here please note that we are using sqlClient.SqlConnection. So here, Provider attribute is not required. Case 2: Connecting to Sql server using database file name StudentDB.mdf
Now as we have connected to database, lets fetch some data and put it into our Form controls Below is the sample code that can fetch data and assign them to the textboxes or other controls on the GUI. We shall explain the code step by step.
Now lets try to understand the above code. 1. We have declared an OleDB Command cmd which represents an sql statement or stored procedure that will be executed again the database. It takes two arguments: the command text as string and the open connection to the database. Here we have taken the empId from the input textbox txtEmpId.Text. 2. Then we have declared reader rdr as OleDBdatareader to read data from database (forward-only). 3. Next is we have executed the command again the database to fill the oledbdatareader rdr. rdr = cmd.ExecuteReader() 4. Here rdr.HasRows checks if the rdr has retrieved some data or not. so when it is non-empty we have assigned the data from reader rdr to our GUI textboxes. While (rdr.Read()) End While Following is a RAR file containing code for all types of connections. The Project uses Microsot visual studio 2008 as front end and all 3 database as back-end. This project consists of very simple implementation to connect to all these databases. |
|
Related blogs
You may also like to see:
[2024-03-27] | [Technical Solution] |
How to discuss on Performance Appraisal with your manager? 10 Points to help you get a better hike |
[2014-03-01] | [Technical Solution] |
RSS feed: what is it and how to use it? |
[2014-02-06] | [Technical Solution] |
Simple steps to boost your computer. |
[2014-01-29] | [Technical Solution] |
How to create my own website for free? |
[2015-12-30] | [Technical Solution] |
SOLVED : Big problem of windows 10, internet speed reduces greatly. Turn off Windows update. |
Total comments: 0 | |