2 Replies - 10664 Views - Last Post: 15 October 2007 - 04:57 PM Rate Topic: -----

#1 silambarasan_84  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 18-July 07

How can I connect to SQL Server in VB6

Post icon  Posted 18 July 2007 - 02:44 AM

hi i have great confusion to connecting vb with sqlserver. anybody can help me?.
Is This A Good Question/Topic? 0
  • +

Replies To: How can I connect to SQL Server in VB6

#2 muddasir  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 8
  • View blog
  • Posts: 114
  • Joined: 14-January 07

Re: How can I connect to SQL Server in VB6

Posted 15 October 2007 - 12:43 AM

you can make connection with the followin code

Public con As New Connection
Dim UserName As String
Dim Password As String
Dim ServerName As String
Dim DBName As String
UserName = ""
Password = ""
ServerName = ""
DBName = "databasename"
	con.ConnectionTimeout = 5										' Set the time out.
	con.Provider = "sqloledb"										' Specify the OLE DB provider.
	con.Properties("Data Source").Value = ServerName				 ' Set SQLOLEDB connection properties.
	con.Properties("Initial Catalog").Value = DBName				 ' Set SQLOLEDB connection properties.
	con.Properties("Integrated Security").Value = "SSPI"			 ' Set SQLOLEDB connection properties.
	con.Open



*Always use code blocks when posting code :)

This post has been edited by muddasir: 15 October 2007 - 03:37 AM

Was This Post Helpful? 0
  • +
  • -

#3 AuGuS666  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-October 07

Re: How can I connect to SQL Server in VB6

Posted 15 October 2007 - 04:57 PM

There are 2 kinds of Connection Strings:
- To Local Server (local means to your machine).
--> Available authentification:
-----> Windows Authentification, Sql Server Authentification.
- To External Server (another machine in a network(local, internet, etc))
--> Available Authentification:
-----> Windows Authentification, Sql Server Authentification.

Available Authentification = The permition to conect to the sql server.

Lets see a Conection to a local server with SqlServer Authentification:
  'NEW CONEXTION OBJECT ------------------
  Dim cnnConextion As New ADODB.Connection
	  
	  'OPEN THE CONEXTION TO LOCAL SERVER
	   cnnConextion.Open "driver={SQL Server};" & _
						 "server=(local);uid=sa;pwd=''; database=master"



Lets explain the code, remember end a stament with ';':
-Driver ={SQL Server}; <-- driver to conect example SQLServer 7, 2000, 2005, etc.
-server=(local); conect to a local server.
-uid=sa; name of the user who wants a connection (example 'sa'). This a Sql Server Authentification becouse we are using an SQL Server to create a connection.
-pwd=; By default the user 'sa' has no password, if 'sa' got a password use it there.
-database=master; the database to conect.

Lets see a Conection to a local server with Windows Authentification:
PD: Remember you cant connect to a SqlServer in another machine with a Windows Authentification (well you can but its really complicated :crazy:.)
  'NEW CONEXTION OBJECT ------------------
  Dim cnnConextion As New ADODB.Connection
	  
	  'OPEN THE CONEXTION TO LOCAL SERVER
	   cnnConextion.Open "PROVIDER=sqloledb; " & _
						 "Integrated Security=SSPI; " & _
						"Initial Catalog=master; Data Source=(local);"



Lets explain the code, remember end a stament with ';':
-PROVIDER=sqloledb; <-- driver to conect example SQLServer 7, 2000, 2005, etc.
-Integrated Security=SSPI; user pc (local pc) who wants a connection .
-Initial Catalog=master; the database to conect.
-Data Source=(local); SqlServer Data source of the databases.

There are many properties, but this are the most import, remember always close your conecctions :rolleyes: ...

Easy right??? :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1