Connecting to a Data Source in ADO.NET

This article has been excerpted from the book "A Programmer's Guide to ADO.NET in C#".
 
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 from 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 Northwind 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
  1. // Create the Data Source Name connection string to  
  2. // acess our Data source  
  3. string ConnectionString = @ " DSN =NWDSN";  
  4. OdbcConnection conn = new OdbcConnection(ConnectionString);  
  5.   
  6. // use the SQL Query to get the customers data  
  7. OdbcCommand cmd = new OdbcCommand("Select * From customers", conn);  
  8. 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.


Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.