ARTICLE

Connecting to a Data Source in ADO.NET

Posted by Raj Kumar Articles | ADO.NET in VB.NET March 08, 2010
In this article I will explain Connecting to a Data Source in ADO.NET.
 
Reader Level:

Connecting through the ODBC Data Source Name (DSN) is another option to access a database using ODBC data providers. Using DSN is useful when you need to access different kinds of ODBC data sources and you don't know the user ID and passwords. You ask the user to create a DSN, or you create a DSN programmatically and ask the user to enter their UserId and password.

You can either create a DNS programmatically or create one manually using the ODBC Data Source Administrator. If you're using windows 2000, you go to Control Panel > Administrative Tools > Data Sources (ODBC). This brings up the ODBC Data Sources Administrator. Using this Administrator, you can create data sources, reconfigure them, and remove them. The ODBC Data Source Administrator looks like figure 5-26.


Figure-5.26.jpg

Figure 5-26. ODBC Data Source Administrator screens with add, remove, and configure options


As you can see from figure 5-26, you can add or remove new data source names or configure existing data source names using the Add, Remove, and Configure buttons. The Add button launches the Create New Data Source Wizard. On the first screen of this wizard, you can select the desired driver. As you can see form figure 5-27. I picked the Microsoft Access driver because I'm going to create a DSN for the Access database.

Figure-5.27.gif

Figure 5-27. Selecting an ODBC driver for a new DSN

When you click Finish, the next step is to select a database name. On this screen, you can also create a new database or repair and compact an existing one. For this example, select the c:\Northwind Access 2000 database and give it the DSN name NWDSN (see figure 5-28).


Figure-5.28.jpg

Figure 5-28. Creating a DSN using the Norht wind Access database


Now you've created a DSN. Connecting to this DSN using an ODBC data provider is pretty simple. Instead of creating a connection string, you use the DSN as a connection string. Rest assured, everything is the same you've been doing so far.

As you can see from Listing 5-26, you can connect to an ODBC DSN using ODBC data providers.


Listing 5-26. Connecting to an ODBC DSN


        ' Create the Data Source Name connection string to
        ' acess our Data source
        Dim ConnectionString As String = " DSN =NWDSN"
        Dim conn As New OdbcConnection(ConnectionString)

        ' use the SQL Query to get the customers data
        Dim cmd As New OdbcCommand("Select * From customers", conn)
        conn.Open()

You can also access your other favorite databases, such as MySQL. You must have the MyODBC driver installed, but assuming you have everything set up that you need, you can use.NET to read this popular database. Listing 5-27 shows the code that will read the MySQL database through the Test DSN ODBC data source.


Conclusion

Hope this article would have helped you in understanding
Connecting to a Data Source in ADO.NET. See my other articles on the website on ADO.NET.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Diagram
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Diagram
Become a Sponsor