Welcome Guest Search | Active Topics | Members | Log In | Register

How to connect SQLwith VB? Options · View
Dhiliban.R
Posted: Wednesday, January 02, 2008 5:09:37 AM
Rank: Guest
Groups: Guest

Joined: 12/30/2007
Posts: -9
How to connect SQL as a backend to the VB?d'oh!
naveen
Posted: Wednesday, January 02, 2008 9:45:56 AM

Rank: Newbie
Groups: moderators

Joined: 12/30/2007
Posts: 27
Location: Coimbatore
Seems to be a nice start dilip! lemme make u clear how to conncet sql as a backend to vb!

->To conncet VB with sql, we need ADO
For this ,I am using a simple form. I have a button dragged and dropped on the form. Here I plan to simply use a Connection object and a Recordset object to retrieve records. These two objects shall do to serve our purpose for the moment. I am not using any control to display data; I intend to display data on the form itself. This post will show you how to fetch records, find the total number of records fetched and how to access the values of each field.

->We have to create instances of a Connection and a Recordset object in our button’s click object.
nw consider this code!
Dim MyConnObj As New ADODB.Connection
Dim myRecSet As New ADODB.Recordset


->To create a Connection to a database we use the Connection object’s Open method
MyConnObj.Open _
"Provider = sqloledb;" & _
"Data Source={ServerName};" & _
"Initial Catalog={DatabaseName};" & _
"User ID={UserName};" & _
"Password={Password};"


this is normally we used to call as connection string!Drool

Next we use the Recordset object’s Open mentod to fetch records from a table (myTable):
myRecSet.Open “select * from myTable”, MyConnObj, adOpenKeyset

->Let us suppose that my table has three fields. You can refer to a data in the first field using the RecordSet object as:
Msgbox myRecSet(0)
The index 0 maps to the first field, 1 maps to data in the second field, and so on…However this analogy will not allow you to get the next record! So how do we get the information pertaining to the next record? itz simpleDrool use the below code!
myRecordSet.MoveNext

->So now the question comes: you know how to open a connection, you know how to fetch records, and you know how to point to the next record. But how will you know when the records fetched exhausts? How will you know if the last record has reached?
*There are two ways. To know the numbers of records before hand we can use the RecordCount property of the Recordset object.
Msgbox “Total no of records = ”& myRecSet.RecordCount

->Another way is to iterate; we can move the pointer to the next record and check if the EOF property is True or not. This is what I have used to print data on the form.

This is my example module:
Private Sub Command1_Click()

Dim MyConnObj As New ADODB.Connection 'ADODB Connection Object
Dim myRecSet As New ADODB.Recordset 'Recordset Object
Dim sqlStr As String ' String variable to store sql command

MyConnObj.Open _
"Provider = sqloledb;" & _
"Data Source=172.16.1.60;" & _
"Initial Catalog=TESTATV;" & _
"User ID=sa;" & _
"Password=p@ssW0rd;"

sqlStr = "select * from employee"

myRecSet.Open sqlStr, MyConnObj, adOpenKeyset

MsgBox "Total Number of records = " & myRecSet.RecordCount

Dim i As Integer 'variable to keep count
i = 1

Print "#"; Tab; "ID"; Tab; "Name"; Tab; "Salary"
Print ""

While Not myRecSet.EOF ' Loop until endd fo file is reached

Print i; Tab; myRecSet(0); Tab; myRecSet(1); Tab; myRecSet(2)
'0- 1st filed, 1- 2nd Field and so on...

myRecSet.MoveNext 'Moves the RecordSet pointer to the next position

i = i + 1
Wend

MyConnObj.Close

End Sub


->If u follow these steps and constraints carefully, then my friend, there will be no problem in establishing connection!

Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.2 (NET v2.0) - 9/27/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.055 seconds.