OdbcConnection Example
This page was last reviewed on Jun 5, 2024.
Dot Net Perls
OdbcConnection. This C# connection type is used with MySQL. It often leads to syntax errors. The connection string does not allow quotes around the values.
Some helpful hints. With some care this SQL database object in C# programs is effective. We explore the OdbcConnection objects in the C# language and .NET.
Connection string. If you are using a third-party web host, you will often have a sample connection string. This is useful, but you need to be careful.
Start Specify the driver as MySQL ODBC 3.51 Driver in curly brackets. This version may be different for your setup.
Tip Don't put quotes around your driver. If you do put quotes, it might not work as you want it to.
Next Put your server URL in there. You will have to get this from your web host.
Info The database's name is OkieData, the user name is SamAllen, and the password is CutiePie. You can't put quotes around any of the values.
DRIVER={MySQL ODBC 3.51 Driver}; SERVER=p50mysq5555.secureserver.net; PORT=3306; DATABASE=OkieData; USER=SamAllen; PASSWORD=CutiePie; OPTION=0;
Web.config. It is ideal to put your connection string in Web.config in your ASP.NET project. You will have to find the appropriate blocks in the XML config file.
And In this next XML element example, I use the connection string name of WhateverName.
<!-- This XML should be put in Web.config --> <connectionstrings> <add name="WhateverName" connectionString="Exact string shown above"/> </connectionstrings>
Using. 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 System.Data.Odbc; using System.Web.Configuration;
New. 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.
Info The OdbcConnection with the identifier "c" is a new connection to the database. These are automatically pooled and shared.
// Try to connect to the database based on our stored connection string. string conString = WebConfigurationManager.ConnectionStrings[ "WhateverName"].ConnectionString; using (OdbcConnection c = new OdbcConnection(conString)) { c.Open(); // We are now connected. // Now we can use OdbcCommand objects to accomplish things. }
MySQL. Here we declare a new OdbcCommand object, add a parameter to the command object, and then read in data from the database. Note the question mark in the command text.
Info The OdbcCommand takes the connection object as the second parameter. The paramWord string is added to the command.
And The parameter is named "var" but that is not important. The parameter is added in the first question mark.
Result It looks through the table and find the rows where the MagicKey column is equal to paramWord.
using (OdbcCommand c = new OdbcCommand("SELECT ColumnWord FROM OkieTable WHERE MagicKey = ?", con)) { c.Parameters.AddWithValue("@var", paramWord); using (OdbcDataReader reader = c.ExecuteReader()) { while (reader.Read()) { string word = reader.GetString(0); // Word is from the database. Do something with it. } } }
A summary. Here we fixed bugs with OdbcConnection. This can be applied to any ADO.NET provider, from SqlConnection objects to SqlCeConnections and SQLiteConnections.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jun 5, 2024 (edit).
© 2007-2024 Sam Allen.