C# OdbcConnection

SQL: Structured Query Language

OdbcConnection is used with MySQL. It often leads to syntax errors. The connection string does not allow quotes around the values. With some care this SQL database object is effective.

Overview

We explore the OdbcConnection objects in the C# language and .NET Framework. After the basics, I will break down other details, such as how to add the code and actually use the OdbcConnection.

ODBC:The acronym ODBC stands for open database connectivity. The "open" refers to the platform status, not the connection state.

ASPNET web programming framework

These connections work with more than one SQL server, so you can use ODBC in many different scenarios. The scenario I outline uses ODBC connections in an ASP.NET application that accesses a MySQL database.

Performance:The applications you build with this combination have the potential to be fast and responsive.

Connection string

String type

Usually, if you are using a third-party web host, they provide you with a sample connection string. This is useful, but you need to be careful. There are some tricks. On my web apps, I have connection strings that look like these.

Connection string

DRIVER={MySQL ODBC 3.51 Driver}; SERVER=p50mysq5555.secureserver.net; PORT=3306;
DATABASE=OkieData; USER=SamAllen; PASSWORD=CutiePie; OPTION=0;

Driver:Specify the driver as MySQL ODBC 3.51 Driver in curly brackets. This version may be different for your setup.

Quotes:Don't put quotes around your driver. If you do put quotes, it might not work as you want it to.

Database server:Put your server URL in there. You will have to get this from your web host.

Attribute values:The database's name is OkieData. The user name is SamAllen. The password is CutiePie.

All quotes:If you read nothing else here read this: You can't put quotes around any of the values.

My experience. I spent a hectic half-hour or more trying to log in to the MySQL database with this configuration with quotes around my password. These attempts failed. You cannot have those quotes.

Web.config

Extensible markup language: XML

It is best practice, although not required, to put your connection string in Web.config in your ASP.NET project. Here are the lines I used. You will have to find the appropriate blocks in the XML config file yourself.

And:In this next XML element example, I use the connection string name of WhateverName.

XML
Web.config: XML

<!-- This XML should be put in Web.config -->

<connectionstrings>
    <add name="WhateverName" connectionString="Exact string shown above"/>
</connectionstrings>

Using

Your page will have a code-behind file. At the top are your using-statements. Add these two using-statements at the top. The first adds the database stuff, and the second allows you to access your connection string.

Using statements: C#

using System.Data.Odbc;
using System.Web.Configuration;

New

Using keyword

We can create an OdbcConnection by combining it with the using-statement. The next code block gets our special connection string from the Web.config file. The contents of that string are shown above.

The OdbcConnection with the identifier "con" is a new connection to the database. These are automatically pooled and shared. That code was written by Microsoft programmers skilled in these things. I could not easily improve it.

Fragment that uses ConnectionStrings: C#

// Try to connect to the database based on our stored connection string.
string conString = WebConfigurationManager.
    ConnectionStrings["WhateverName"].ConnectionString;
using (OdbcConnection con = new OdbcConnection(conString))
{
    con.Open();
    // We are now connected. Now we can use OdbcCommand objects
    // to actually accomplish things.
}

MySQL

In this next code block, I will declare a new OdbcCommand object, then add a parameter to the command object, and then read in data from the database. Note the question mark in the command text.

Fragment that uses OdbcDataReader, MySQL: C#

using (OdbcCommand com = new OdbcCommand(
    "SELECT ColumnWord FROM OkieTable WHERE MagicKey = ?", con))
{
    com.Parameters.AddWithValue("@var", paramWord);

    using (OdbcDataReader reader = com.ExecuteReader())
    {
	while (reader.Read())
	{
	    string word = reader.GetString(0);
	    // Word is from the database. Do something with it.
	}
    }
}
Question

The OdbcCommand takes the connection object as the second parameter. The paramWord string is added to the command. It is named @var but that doesn't matter. The parameter is added in the first question mark.

And:It looks through the table and find the rows where the MagicKey column is equal to paramWord.

Summary

Framework: NET

Here we fixed bugs with OdbcConnection. This can be applied to any ADO.NET provider, from SqlConnection objects to SqlCeConnections and SQLiteConnections. Use the material here as a launching point for learning more about ADO.NET.


C#: Data