As a programmer you will run into the need to connect to multiple data sources such as SQL databases. Once you make the connection to a SQL server database, you can update or retrieve data from the database tables. In Visual Basic (VB) you can use the ActiveX Data Objects (ADO) to connect to a SQL server. Don't spend time exporting data from SQL to files for your VB application; instead use ADO and retrieve data directly from SQL.
Launch Microsoft Visual Basic Express and click "New Project..." on the left pane of your screen then select "Console Application." Click "OK" to create a new console project. Click the "Project" menu and select "<projectname> Properties".
Click "References" and click "Add." Click the "COM" tab and click ""Microsoft ActiveX Data Objects <version number> Library"." Click "OK" to add the reference.
Import the ADODB namespace by typing the following line of code in the first line of your module:
Type the following VB code below "Sub Main()" to create a new variable:
Dim ADOconnection As New ADODB.Connection
Copy and paste the following code to connect to a database in SQL server:
ADOconnection.ConnectionString = "Provider=SQLNCLI10;" _
& "Server=SERVER NAME;" _
& "Database=DATABASE NAME;" _
& "Integrated Security=SSPI;" _
& "DataTypeCompatibility=80;" _
& "MARS Connection=False;"
Edit "SERVER NAME" and type the name of your SQL server. Edit "DATABASE NAME" and type the name of the database you want to connect to.
Type the following to close your connection when you're done with the connection:
ADOconnection = Nothing
- Photo Credit Hemera Technologies/AbleStock.com/Getty Images
How to Connect to SQL Server Through VB.net
The VB.NET programming language and SQL Server work together to create dynamic content for Web or desktop applications. You use the "ADO"...
Visual Studio SQL Tutorial
SQL Server is a system that allows developers to create and manage databases. It runs as a service and has no user...
How to Connect to MS SQL Using VBScript
Visual Basic Scripting, or VBS, lets you connect to your internal database to retrieve data for your users. VBS is typically used...
How to Connect VB.Net With MySQL
MySQL contains your website's information, so you can use it to serve dynamic content to website readers. Websites created in VB.NET can...