Solved

sample code to connect SQL database using VB6

Posted on 2000-04-03
8
903 Views
Last Modified: 2010-08-05

How many ways to make connection from VB6 to SQL database?

Is there any sample code ?

I'm using Win95 and SQL server 7.

thanks
0
Comment
Question by:isman
8 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2682439
WELL It depends. YOu can use iether OLEDB PROVIDER OR ODBC. Connection can be iether though DSN or DSNLESS. However you prefer. Here are some samples.

ex using DSN.
DIM CN as New ADODB.Connection
cn.ConnectionString = "DSN=" & _
";DATABASE=pubs;UID=Admin;PWD=sdf"
cn.open
DSNLESS
cn.connectionstring="DRIVER=SQL SERVER" & _
";SERVER=nameofserver;UID=Admin;PWD=blank;DATABASE=pubs"
cn.open
ex of OLEDB

cn.Connectionstring="PROVIDER=SQLOLEDB";SERVER=servername;UID=ADMIN;PWS=blank;DATABASE=pubs"
cn.open
0
 
LVL 14

Expert Comment

by:wsh2
ID: 2682519
As Marine so correctly points out.. ADO is the preferred way to go.. <smile>. Although the documentation is not the best (at least on my CD version of MSDN 4/99), you may also want to try using the Data Environment Designer (Vb Menu -> Project -> Add Data Environment). It is a graphical interface for building ADO connections, commands, recordsets, SQL statements and features a Data Viewer. Its only downside, as I stated earlier, is the documentation that accompanies it.. <sigh>. Oh well.. such is life in the VB fast lane.. <smile>.

0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2682570
According to Robert Vieira, author "SQL Server 7.0 Programming", he seems to lean towards ODBC if you are strictly working with tabular like data (i.e. rows and columns)
0
 
LVL 4

Expert Comment

by:Jeremy_D
ID: 2683004
Although SQLOLEDB should be slightly faster, I've found it behaves a bit unpredictable at some points. For instance, when using ADODB.Command objects, the second execution of such a command-object will sometimes clear your parameters (but not all times), even when you've just set them explicitly. I would recommend using one of Marine's first two code-examples.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:myvinod
ID: 2683091
 The answers given by Marine and others are absolutely correct. But even if you face any problems to make connection to any database Try this simple method out to get the connection string. Place a ADO DataControl on the form and establish the connection using the details given on the property page, when you give the neccessary information like your database name and provider name displayed on the screen. And test the connection you end up with connection string. Copy the string and use it in the code.
0
 

Accepted Solution

by:
Jagertee earned 50 total points
ID: 2683505
Easiest is to register your server with Control Panel -> ODBC, give it a unique Data Source Name (DSN), let's say "MyDatabase".

I don't like Data Controls because they're not flexible enough. I'm using the ADO, so you need to reference (Project->References in VB) "Microsoft OLE DB ActiveX Data Object 1.0 Library".

At the start of the application I open the database connection to my server and leave it open till the user closes the app. For this, I use a Public variable in a Module to hold the database connection:

Public DBConn as New ADODB.Connection

Here's the code to open a database connection to your server:

DBConn.Open "MyDatabase", "UserName", "Password"

Somewhere deeper in the code you would like to fetch some records from the database... do it this way to be completely sure that it's working:

On Error Resume Next

Dim RS As ADODB.Recordset
Dim EOF As Boolean

Set RS = DBConn.Execute("SELECT * FROM addresses WHERE city='London'")

EOF = RS.EOF
If Err = 0 Then
    If EOF Then
        MsgBox "Nothing found!"
    Else
        While (Not EOF) And (Err = 0)
            'display current record (just the person's name)
            Debug.Print RS.Fields("name")

            'move to next record
            Err = 0
            RS.MoveNext
            EOF = RS.EOF
        Wend
    End If
End If
If Err Then
    MsgBox "There's been an error in the query: " & Err & " (" & Error & ")"
End If


Need more?
0
 

Author Comment

by:isman
ID: 2686164
Jagertee,

Do I have to specify DNS name in the connection command?

Please give me more examples...

Thanks
Isman
0
 

Expert Comment

by:Jagertee
ID: 2686445
You may also use Marine's connection string if you prefer to not register a System DSN in the ODBC control panel applet:

"DRIVER=SQL SERVER;SERVER=yourserver;UID=yourusername;PWD=yourpassword;DATABASE=yourdatabase"

(this is usefull if you have to deploy lots of clients)

for yourserver you may use
- ip address
- netbios name
- dns name (if you have dns deployed in your company or if your server is on the internet)
- dns name (a *must* in a native windows 2000 network)

yourusername and yourpassword should be a valid login for the server and yourdatabase
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now