473,468 Members | 1,364 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Methods of Connecting to SQL Server

ADezii
8,834 Recognized Expert Expert
At some point in time, you may need to make a Connection to an External Data Source residing in a SQL Server Database. There are basically 3 distinct Methods for creating this Connection. I will briefly outline each Method below, list several assumptions made concerning the Connections, then demonstrate the proper coding techniques needed to implement each Method. If there are any questions, please feel free to ask.
  1. Methods of Connecting to SQL Server
    1. Supply Connection information as an Argument to the Open Method of the Connection Object
    2. Use the ConnectionString Property of the Connection Object
    3. Set the Properties of the Connection Object
  2. Assumptions
    1. Data Source = "(local)"
    2. Database/Initial Catalogue = "pubs"
    3. User Id = "ADezii"
    4. Password = "r16G37P99J"
  3. Code Implementations
    Expand|Select|Wrap|Line Numbers
    1. 'Method 1 - Connection information as an Argument to the Open Method of the Connection Object
    2. Dim cnn As ADODB.Connection
    3. Set cnn = New ADODB.Connection
    4.  
    5. cnn.Open "Provider=SQLOLEDB;Data Source=(local);Database=pubs;User ID=ADezii;Password=r16G37P99J"
    6.  
    7. Debug.Print cnn.ConnectionString    'Test for a valid Connection
    8.  
    9. cnn.Close
    10. Set cnn = Nothing
    Expand|Select|Wrap|Line Numbers
    1. 'Method 2 - Use the ConnectionString Property of the Connection Object
    2. Dim cnn As ADODB.Connection
    3. Set cnn = New ADODB.Connection
    4.  
    5. cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=(local);Database=pubs;User ID=ADezii;Password=r16G37P99J"
    6. cnn.Open
    7.  
    8. Debug.Print cnn.ConnectionString    'Test for a valid Connection
    9.  
    10. cnn.Close
    11. Set cnn = Nothing
    Expand|Select|Wrap|Line Numbers
    1. 'Method 3 - Set Properties of the Connection Object
    2. Dim cnn As ADODB.Connection
    3. Set cnn = New ADODB.Connection
    4.  
    5. With cnn
    6.   .Provider = "SQLOLEDB"
    7.   .Properties("Data Source") = "(local)"
    8.   .Properties("Initial Catalog") = "pubs"
    9.   .Properties("User ID") = "ADezii"
    10.   .Properties("Password") = "r16G37P99J"
    11.     .Open
    12. End With
    13.  
    14. Debug.Print cnn.ConnectionString    'Test for a valid Connection
    15.  
    16. cnn.Close
    17. Set cnn = Nothing
Sep 2 '07 #1
0 6995

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Google Mike | last post by:
After a lot of thought and research, and playing with FreeTDS and InlineTDS, as well as various ODBC connections, I have determined that the fastest and cheapest way to get up and going with PHP on...
12
by: Ann Marinas | last post by:
Hi all, I would like to ask for some help regarding separating the asp.net webserver and the sql server. I have created an asp.net application for a certain company. Initially, we installed...
0
by: cj.snead | last post by:
Hello, I am having trouble connecting to a remote named instance of SQL Server via Pocket PC. I have had absolutely no luck connecting with VS 2005 (even to a default instance), so I wen't back...
6
by: Todd Brewer | last post by:
Windows Server 2000 ASP.NET 2.0 SQL Server 2000 (on a physically seperate server) I moved an ASP.NET 2.0 application from a development server to production, and am getting the following error:...
3
by: tcomer | last post by:
Hello! I'm working on an asynchronous network application that uses multiple threads to do it's work. I have a ChatClient class that handles the basic functionality of connecting to a server and...
10
by: mairhtin o'feannag | last post by:
Hello, I'm having problems connecting to my new v9 db box. The pertinent information is below: DB2_db2inst1 60000/tcp DB2_db2inst1_1 60001/tcp DB2_db2inst1_2 60002/tcp DB2_db2inst1_END...
2
by: samadams_2006 | last post by:
Hello, I have a problem that I'm hoping someone will be able to help me resolve. 1) I have a C# Web Site in which I connect to the database: "Install Microsoft SQL Server 2005 Express...
2
by: orandov | last post by:
Hi, I am having a problem connecting my .net applications from the application server to the database server. When I run the application from my windows xp (sp2) box it works fine. When I try to...
0
by: aboutjav.com | last post by:
Hi, I need some help. I am getting this error after I complete the asp.net register control and click on the continue button. It crashed when it tries to get it calls this Profile property ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.