Documente Academic
Documente Profesional
Documente Cultură
Contents
What is .NET?
.NET and Data Access
What Does This Document Show?
A Simple Example
Getting Error Information
Updating Data
Multiple Results
What is .NET?
Microsoft describe .NET in the following way:
For more details about the entire framework, consult the FAQ Microsoft provide at:
http://msdn.microsoft.com/en-us/library/ms973850.aspx
http://msdn.microsoft.com/en-us/library/ms973217.aspx
One of the key changes that ADO.NET introduces is the replacement of the ADO
Recordset object with a combination of the DataTable, DataSet, DataAdapter, and
DataReader objects. A DataTable represents a collection of rows from a single
table, and in this respect is similar to the Recordset. A DataSet represents a
collection of DataTable objects, together with the relationships and constraints
that bind the various tables together.
One of the key characteristics of the DataSet is that it has no knowledge of the
underlying data source that might have been used to populate it. The actual
interface to the underlying data source is provided by using one of a number of
.NET Data Providers. A .NET Data Provider comprise four key objects (Connection,
Command, DataReader and DataAdapter).
.NET Data Providers fall into two categories: bridge providers and native
providers. Bridge providers, such as those supplied for OLE DB and ODBC, allow you
to use data libraries designed for earlier data access technologies. Native
providers are normally provided by the actual database supplier and provide
interfaces to specific databases. For example, SQL Server has a native provider
available from Microsoft.
Download Oracle®, Interbase, Salesforce, Derby, ISAM and RMS ODBC drivers for .NET.
Free trial.
A Simple Example
The following steps will create a application that show the basic operations used
to interface ODBC and .NET.
Create a new Visual Studio Project. For this example, we will use a C# Console
Application.
Import the System.data.Odbc namespace into the application, to allow us to use the
ODBC .NET Data Provider:
using System.Data.Odbc;
Now create an OdbcConnection object and connect it to our sample data source.
OdbcConnection DbConnection = new OdbcConnection("DSN=SAMPLE_ISAM");
This example uses a DSN connection, but a DSN free connection can be used by using
the DRIVER={} format. Consult your ODBC driver documentation for more details.
Console.Write(col + ":");
}
Console.WriteLine();
}
Once OdbcDataReader.Read() returns false, we have read every row in the result set,
so now we should cleanup the objects we have used
DbReader.Close();
DbCommand.Dispose();
DbConnection.Close();
We now have a complete C# ADO.NET application that connects to our ODBC data
source, issues a query then displays the results from that query.
The complete program source for this section can be found in CSharpODBCExample1.cs
First, we will allow the connection string to be passed in on the command line of
the application.
if (args.Length != 1 )
{
Console.WriteLine("usage: sample connection-string");
return;
}
String connStr = args[0];
"DSN=SAMPLE_ISAM"
The program can be run as before. However, if the user enters an invalid connection
string, the program will fail when the OdbcConnection.Open() method is called. The
program will throw an Exception at this point, and we can change our code to catch
this exception and display information from the ODBC system to the user.
The exception thrown will be an OdbcException, and we can use this to extract the
error string that the ODBC Driver Manager has returned.
try
{
DbConnection.Open();
}
catch (OdbcException ex)
{
Console.WriteLine("connection to the DSN '" + connStr + "' failed.");
Console.WriteLine("The OdbcConnection returned the following message");
Console.WriteLine(ex.Message);
return;
}
We will also allow the user to enter the query to executed and then set this string
as the query to be executed:
Console.Write("SQL> ");
String query = Console.ReadLine();
OdbcCommand DbCommand = DbConnection.CreateCommand();
DbCommand.CommandText = query;
But now the SQL may not be valid, so the OdbcCommand.ExecuteReader() may also throw
an exception, which we can catch and display.
try
{
OdbcDataReader DbReader = DbCommand.ExecuteReader();
// rest of the code to process the result set
}
catch (OdbcException ex)
{
Console.WriteLine("Executing the query '" + query + "' failed.");
Console.WriteLine("The OdbcCommand returned the following message");
Console.WriteLine(ex.Message);
return;
}
The complete program source for this section can be found in CSharpODBCExample2.cs
Download Oracle®, Interbase, Salesforce, Derby, ISAM and RMS ODBC drivers for .NET.
Free trial.
Updating Data
Now that we can enter a query at the command line, our application must handle
queries that do more than just create result sets. At the moment, if we try to
create a new table using the application, the OdbcDataReader will be created with
no columns, so we can use this do decide how to process the query.
else
{
int row_count = DbReader.RecordsAffected;
Console.WriteLine( "Query affected " + row_count + " row(s)");
}
Our sample can now query and update the database.
The complete program source for this section can be found in CSharpODBCExample3.cs.
Multiple Results
Most databases can now process commands like this:
Download Oracle®, Interbase, Salesforce, Derby, ISAM and RMS ODBC drivers for .NET.
Free trial.
Appendix A: Resources
Easysoft ODBC drivers:
Oracle® ODBC driver
Salesforce.com ODBC driver
MongoDB ODBC driver
InterBase ODBC driver
Firebird ODBC driver
Derby ODBC driver
C/D-ISAM ODBC driver
RMS ODBC driver
LINC Developer ODBC driver
System Z ODBC driver
ODBC-JDBC Gateway — providing ODBC access to JDBC data sources.
ODBC-ODBC Bridge — providing ODBC access to any remote ODBC data source. For
example, access remote ISAM files from C# on Windows.
64-bit ODBC — everything you need to know about ODBC on 64-bit Linux, UNIX and
Windows platforms.
Article Feedback
* Did this content help you?
Yes
No
Maybe
* Please select one option based on your first choice:
I'm very satisfied
I think it will help, but I haven't tried it yet
It is helpful, but I need more information
It is helpful, but hard to understand
Seemed relevant in search results, but didn't help me
The information is incorrect
The page contains one or more broken links
Suggest new content or let us know how we can improve this content:
(* Required Fields)
About Easysoft
Contact Us
About Us
Clients
Upgrade Offer
Blog
Careers
Products
ODBC Drivers
JDBC Drivers
Bridges and Gateways
In Development
Services
Consultancy
Training
Custom Development
Licensing
Product Licenses
Prices
Support
Support Home
Solution Wizard
Getting Started Guides
User Guides
Knowledge Base
Search
Easysoft.com
Documentation
Knowledge Base
Popular Resources
Developer Area
Client Applications
Legal
Privacy Statement
Your Account
Log In
Register
© 1993 - 2020 Easysoft Limited. All rights reserved. Oracle® is a registered
trademark of Oracle® Corporation and/or its affiliates. Other trademarks and
registered trademarks appearing on easysoft.com are the property of their
respective owners.Twitter LinkedIn Facebook Google+ YouTube