Search
This tutorial is part of Database Programming Made Easy Series.
I am writing this tutorial in the hope that you will understand the different kinds of database connection using Visual Basic .NET. This tutorial is for beginner only. If you are an advance programmer I advice you suggest correction for the betterment of this tutorial.
Before you begin accessing a data, first you need to establish a session with your database. This can be done using an object called connection.
Whether you are accessing an MS Access database or MS SQL Server Database you still need to open a connection before you can read and write back to the database.
1. Using OleDbConnection – Microsoft Access Database
'Set up connection string Dim cnString As String txtStatus.Text = "Connecting to database using MS Access" & vbCrLf & vbCrLf cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\data\Northwind.mdb" 'Create connection Dim conn As OleDbConnection = New OleDbConnection(cnString) Try ' Open connection conn.Open() txtStatus.Text = txtStatus.Text & "Connection opened" & vbCrLf Catch ex As SqlException txtStatus.Text = "Error: " & ex.ToString & vbCrLf Finally ' Close connection conn.Close() txtStatus.Text = txtStatus.Text & "Connection closed." End Try
2. Using SqlConnection – Microsoft SQL Server
'Set up connection string Dim cnString As String txtStatus.Text = "Connecting to database using SQL Server" & vbCrLf & vbCrLf cnString = "Data Source=localhost;Integrated Security=True" 'Create connection Dim conn As SqlConnection = New SqlConnection(cnString) Try ' Open connection conn.Open() txtStatus.Text = txtStatus.Text & "Connection opened" & vbCrLf Catch ex As SqlException txtStatus.Text = txtStatus.Text & "Error: " & ex.ToString & vbCrLf Finally ' Close connection conn.Close() txtStatus.Text = txtStatus.Text & "Connection closed." End Try
As you have notice. There is just one line of code to change if you are planning to use either Microsoft Access or Microsoft SQL Server.
For Microsoft Access the connection string is:
cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\data\Northwind.mdb"
And in Microsoft SQL Server the connection string is:
cnString = "Data Source=localhost;Integrated Security=True"
In this tutorial I prepare a project file for you to practice the difference between a Microsoft Access and Microsoft SQL Server. Download the zip file below.
Next: How to Retrieve Data
Download Code
- 14166 reads
Relevant Content
- TextBox Control Using Datasets and Data Adapters
- Database Programming Made Easy
- How to Add/Update/Delete Record using MS Access Database
- Restaurant Billing System
- Simple Web Browser
- File Downloader
- CPU Meter
- rOSALIE SIMPLE iPHONE style phonebook - UPDATED WITH BUTTONXP.OCX
- Library Management System(Updated) for second time
- Student Registration




making program using ms access(making library system)
hai sir may i have your attention please ? im sherwin asking for your help. may i know how to make libraey system using ms access? thank you !!
connection
Is this source code can be used for all connection establishment between VB and MS Access?
Thanks
Thanks
Connecting to the database in s server
What do I need to modify in the connection string to connect to the sql database located in a server,the client computer in whch the application will be installed is in the same network (LAN) with the server
AddUpdateDeleteRecord
Any chance you have this code in VB 2005?
help
how can i connect c# to sql database
sql server in vb.net
sir is there any way to convert a database created in ms access to sql server2005?
sql server in vb.net
yes if your SQL server has import or export funtion.
help
help.. how to connect login form using ms access 2007.
the username and password are inside the database access 2007.
MS Access
I have MSAccess 2000... I can't open the database to manipulate the fields (need to add some more to the search). Is there any way you can help?
Help
Hi admin! I have tried that codes to create connection to MS Access but it gives a compile time error that unrecognized escape sequence at the connection string cnString
cnString="Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\data\Northwind.mdb";
am using visual c# 2008. Please can you help me out what is wrong with that error..
edoboy_07@yahoo.com
re: Help
Please check the location of the northwind.mdb file.
Datagrid Problems
Bro....actually i want to find a record den diaply it on the datagrid and then display it on the form......wat wud the code in VB.NET
re: Datagrid Problems
I have made a program on this already. Though I'm using DataGridView.
Please browse the Database Programming Made Easy.
VB.NET
Thank you so much
i have a question
cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\data\Northwind.mdb"
hav a question about this the datasource=..\data - do i need to place the complete path where the datase is located such a s C:\my document\.. i receive errors
re: i have a question
If the database is relative to your application folder then you do not need to put the full path.
But if it is outside your application folder then you need to specify the full path.
how can i connect database through javascript
Hi i m swapneel.
my problem is....
i m using autocomplete extender in my site but its not working efficently so i want to use a javascript can u help me on this
Hi Admin
I have downloaded POS system coding.can i use it for my personal perpose.
Tks.
re: Hi Admin
As you wish. Sell it if you like. If you can make money out of it then good.
Tulong nman po!
GoodDay!!!
...admin...
can we have your contact?..
pls....
are familiar on Wireless Monitoring???..
pls........
response k nmn po agad!!!
salamat po....
help pls...........
Hi! admin...
are u in Philippines now?
can we have your contact pls???..
we need your help on our thesis..?
pls.....
Are you familiar on Wireless Monitoring!!!???
need your reply???..
re: help pls...........
Use the contact menu above.
error sql connection
Connecting to database using SQL Server
Error: System.Data.SqlClient.SqlException: An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.SqlClient.SqlConnection.Open()
at Making_Database_Connection.frmMain.btnSQLServer_Click(Object sender, EventArgs e) in C:\Documents and Settings\BTB\Desktop\temp\Making Database Connection\frmMain.vb:line 48
Connection closed.
re: error sql connection
What's your connection string?
help
Imports System.Data.SqlClient
Imports System.Data.OleDb
para saan tong code.........
re: help
This will import OleDB and SQL Server data provider namespaces.
Post new comment