# Connecting to any CData ODBC Driver in C# applications

**URL:** <https://community.cdata.com/t/connecting-to-any-cdata-odbc-driver-in-c-applications/427>\
**Category:** Editions\
**Tags:** article, odbc, c, net\
**Created:** [October 2, 2023, 5:54pm UTC](https://community.cdata.com/t/connecting-to-any-cdata-odbc-driver-in-c-applications/427 "2023-10-02T17:54:02Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brajan\_Hasmeta](https://avatars.discourse-cdn.com/v4/letter/b/7993a0/32.png) [@Brajan\_Hasmeta](https://community.cdata.com/u/Brajan_Hasmeta)\
**Post date:** [October 2, 2023, 5:54pm UTC](https://community.cdata.com/t/connecting-to-any-cdata-odbc-driver-in-c-applications/427/1 "2023-10-02T17:54:02Z")

</div>

Our ODBC Drivers, as shown in our [documentation](https://cdn.cdata.com/help/RFJ/odbc/pg_odbcapi.htm), are usually used in other programming languages such as _C/C++_, _Node.JS_, _PHP_, _Python_, _R_, _Go_. In this article, I will provide a detailed, step-by-step explanation of how to connect your _C# [ADO.NET](http://ADO.NET)_ application to an ODBC connector, specifically using the CData ODBC Driver for Salesforce.

Unlike [ADO.NET](http://ADO.NET) connectors, where you can add a reference to the _System.Data.CData.Salesforce.dll_, in ODBC you can’t add a reference to the _CData.ODBC.Salesforce.dll_ found in default path “_C:\Program Files\CData\CData ODBC Driver for Salesforce\lib64_”. Fortunately,&nbsp;there is a means of connecting C# applications to our ODBC connectors, and that is by utilizing Microsoft’s _System.Data.Odbc_ package.

The following steps will create an application that show the basic operations used to interface ODBC and .NET:

1. Create a new Visual Studio Project. For this example, we will use a C# Console Application:

 ![1f2f3972-3df0-4fb2-8c76-91fe781b336a.png](https://us1.discourse-cdn.com/cdata/original/2X/6/666024de29d34ba60cb7e96f497e31326869d449.png)

2. Install the _System.Data.Odbc_ package. You can do so by using the “NuGet Package Manager” console.&nbsp;Please note that you can also download the _System.Data.Odbc.dll_ file from an online source and then add it as a project reference. However, I would suggest installing the package from the “Package Manager Console” instead, as it offers a simpler and faster approach.

To install the package, click _Tools_ \>\> _NuGet Package Manager_ \>\> _Package Manager Console_:

 ![0682eb10-e447-4f74-84e3-fccb34871894.png](https://us1.discourse-cdn.com/cdata/original/1X/20e5d3dd41eb2006fa5b6625babf03f8e4e7d463.png)

3. Type _Install-Package System.Data.Odbc_ in Package Manager Console and press _Enter:_

 ![b74ce3af-3446-4ce9-adb6-f0c6fdae807b.png](https://us1.discourse-cdn.com/cdata/original/2X/c/cbe89cdb793a4b5afe9034d965e0d06e8dbd0fd3.png)

4. After&nbsp;the installation is successful, the Project class should look like this:

 ![dee0d237-135d-4790-9160-4dd1c1e7c231.png](https://us1.discourse-cdn.com/cdata/original/2X/a/ad33d7f2787cc1462b1885173195cecd547f36e8.png)

Now that we have added the _System.Data.Odbc_ package, we are able to connect to our ODBC driver.

5. Import the _System.Data.Odbc_ namespace into the application, to allow us to use the ODBC .NET Data Provider:

_using_ _System.Data.Odbc;_

6. Create an _OdbcConnection_ object and connect it to our sample data source:

_string_ _connectionString =_ _“Driver={CData ODBC Driver for Salesforce};”_ _+_

_“User=xxxxx;”_ _+_

_“Password=xxxxx;”_ _+_

_“SecurityToken=xxxxxxxxxxxx;”__;_

_using_ _(OdbcConnection connection =_ _new_ _OdbcConnection(connectionString)) {}_

This example makes use of a DSN free connection by using the DRIVER={} format, but a DSN connection can be used instead:

_string_ _connectionString =_ _“DSN=CData Salesforce Source;”__;_

or

_string_ _connectionString =_ _“DSN=CData Salesforce Sys;”__;_

By following this approach, there is no need to manually include the connection properties you have configured in your ODBC Admin within the connection string. Instead, you can simply set the DSN, and the properties will be retrieved from there. For more information on configuring the connection, refer to the documentation provided by our ODBC driver.

7. Next, we will proceed to open the _OdbcConnection_ object and create an _OdbcCommand_ using it. With this command, we can execute a query and generate an _OdbcDataReader_. Consequently, we can iterate through each row in the result set and display the values of the specified columns. The complete C# [ADO.NET](http://ADO.NET) application that connects to our Salesforce ODBC driver would be:

_using_ _System.Data.Odbc;_

_string_ _connectionString =_ _“Driver={CData ODBC Driver for Salesforce};”_ _+_

_“User=xxxxx;”_ _+_

_“Password=xxxxx;”_ _+_

_“SecurityToken=xxxxxxxxxxxx;”__;_

_using_ _(OdbcConnection connection =_ _new_ _OdbcConnection(connectionString))_

_{_

_connection.Open();_

_string_ _sql =_ _“SELECT Id, Name FROM Account”__;_

_using_ _(OdbcCommand command =_ _new_ _OdbcCommand(sql, connection))_

_{_

_using_ _(OdbcDataReader reader = command.ExecuteReader())_

_{_

_while_ _(reader.Read())_

_{_

_Console.WriteLine(__$"Id:_ _{reader[__“Id”__]}__, Name:_ _{reader[__“Name”__]} __"__ );_

_}_

_}_

_}_

_}_

Please reach out to&nbsp;[support@cdata.com](mailto:support@cdata.com)&nbsp;if you run into any issues.
