Share Your Source Code or Article

Do you have source code, articles, tutorials, web links, and books to share? You can write your own content here. You can even have your own blog.

Submit now...

Database Programming Made Easy

This tutorial will teach you step by step on how to connect and manipulate database. If you'd like to suggest a tutorial please write a comment at the bottom of this article.

Read more...

Hire Us to Do Your Work

Do you want a customized system? Do you want to setup your own website to do business? Then we are here to help you in your programming needs.

Read more...

Search

Making Database Connection
admin's picture


0
Your rating: None
Language: 

This tutorial is part of Database Programming Made Easy Series.

Making Database Connection

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

  1. 'Set up connection string
  2. Dim cnString As String
  3.  
  4. txtStatus.Text = "Connecting to database using MS Access" & vbCrLf & vbCrLf
  5.  
  6. cnString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=..\data\Northwind.mdb"
  7.  
  8. 'Create connection
  9. Dim conn As OleDbConnection = New OleDbConnection(cnString)
  10.  
  11. Try
  12. ' Open connection
  13. conn.Open()
  14. txtStatus.Text = txtStatus.Text & "Connection opened" & vbCrLf
  15. Catch ex As SqlException
  16. txtStatus.Text = "Error: " & ex.ToString & vbCrLf
  17. Finally
  18. ' Close connection
  19. conn.Close()
  20. txtStatus.Text = txtStatus.Text & "Connection closed."
  21. End Try

2. Using SqlConnection – Microsoft SQL Server

  1. 'Set up connection string
  2. Dim cnString As String
  3.  
  4. txtStatus.Text = "Connecting to database using SQL Server" & vbCrLf & vbCrLf
  5.  
  6. cnString = "Data Source=localhost;Integrated Security=True"
  7.  
  8. 'Create connection
  9. Dim conn As SqlConnection = New SqlConnection(cnString)
  10.  
  11. Try
  12. ' Open connection
  13. conn.Open()
  14. txtStatus.Text = txtStatus.Text & "Connection opened" & vbCrLf
  15. Catch ex As SqlException
  16. txtStatus.Text = txtStatus.Text & "Error: " & ex.ToString & vbCrLf
  17. Finally
  18. ' Close connection
  19. conn.Close()
  20. txtStatus.Text = txtStatus.Text & "Connection closed."
  21. 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:

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

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



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

admin's picture

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

admin's picture

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

admin's picture

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.

admin's picture

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

admin's picture

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.

admin's picture

re: error sql connection

What's your connection string?

help

Imports System.Data.SqlClient
Imports System.Data.OleDb

para saan tong code.........

admin's picture

re: help

This will import OleDB and SQL Server data provider namespaces.

Post new comment

  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <java>, <java5>, <javascript>, <mysql>, <php>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • Links to specified hosts will have a rel="nofollow" added to them.

  • You may insert videos with [video:URL]

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.

Step by Step Java Tutorial

In this tutorial you will learn how to program with Java. It has a rich of information to be educated well with Java.

Read more...

Do You Have Question?

Do you have any question related to computer programming? Visit our forum and post new topic on the category you like. Be gentle when asking a question.

Ask now...

Point of Sale

Point of Sale is very useful especially for supermarkets or restaurants. I have included a barcode scanner in this program. Please check it out.

Read more...