isman
asked on
sample code to connect SQL database using VB6
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
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>.
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)
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Jagertee,
Do I have to specify DNS name in the connection command?
Please give me more examples...
Thanks
Isman
Do I have to specify DNS name in the connection command?
Please give me more examples...
Thanks
Isman
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;U ID=youruse rname;PWD= yourpasswo rd;DATABAS E=yourdata base"
(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
"DRIVER=SQL SERVER;SERVER=yourserver;U
(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
ex using DSN.
DIM CN as New ADODB.Connection
cn.ConnectionString = "DSN=" & _
";DATABASE=pubs;UID=Admin;
cn.open
DSNLESS
cn.connectionstring="DRIVE
";SERVER=nameofserver;UID=
cn.open
ex of OLEDB
cn.Connectionstring="PROVI
cn.open