{"id":737,"date":"2010-02-26T14:26:44","date_gmt":"2010-02-26T22:26:44","guid":{"rendered":"http:\/\/wp.colliertech.org\/cj\/?p=737"},"modified":"2010-02-26T15:31:03","modified_gmt":"2010-02-26T23:31:03","slug":"connecting-to-mysql-with-odbc-on-mono","status":"publish","type":"post","link":"https:\/\/wp.c9h.org\/cj\/?p=737","title":{"rendered":"Connecting to MySQL with ODBC on Mono"},"content":{"rendered":"<p>We had a visitor on #mono today who needed help with his homework.  It seems that <a href=\"http:\/\/www.reggieburnett.com\/\">Reggie<\/a> is happy to have forgotten everything about using <a href=\"http:\/\/en.wikipedia.org\/wiki\/ODBC\">ODBC<\/a> to connect to MySQL.  I was curious and feeling helpful, so I figured it out.<\/p>\n<h3>Install the MySQL ODBC driver<\/h3>\n<p><code><b>$<\/b> sudo apt-get install libmyodbc<\/code><\/p>\n<p>You can also grab the package <a href=\"http:\/\/www.mysql.com\/downloads\/connector\/odbc\/\">directly from MySQL<\/a><\/p>\n<h3>Configure the MySQL ODBC driver<\/h3>\n<p>ODBC looks in \/etc\/odbcinst.ini for driver configuration.  In order to let it know about the MySQL ODBC libraries, append the following to your \/etc\/odbcinst.ini file:<\/p>\n<pre>\r\n[MySQL]\r\nDescription\t= MySQL driver\r\nDriver\t= \/usr\/lib\/odbc\/libmyodbc.so\r\nSetup\t\t= \/usr\/lib\/odbc\/libodbcmyS.so\r\nCPTimeout\t=\r\nCPReuse\t=\t\r\n<\/pre>\n<h3>Configure your DSN<\/h3>\n<p>If you expect that you will be using this database connection often, you may want to create a short name for it, so you don&#8217;t have to enter all of the parameters every time you want to connect.  ODBC uses something called <a href=\"http:\/\/en.wikipedia.org\/wiki\/Database_Source_Name\">DSN<\/a>s (Data Source Names, if I recall correctly) to make it easier on the user.<\/p>\n<p>If you want to create a DSN, append something like the following to your \/etc\/odbc.ini file:<\/p>\n<pre>\r\n[myodbc3]\r\nDriver       = MySQL\r\nDescription  = MySQL ODBC 3.51 Driver DSN\r\nServer       = mysql\r\nPort         =\r\nUser         = testuser\r\nPassword     = password\r\nDatabase     = test\r\nOption       = 3   \r\nSocket       = \r\n<\/pre>\n<p>You will also need to update the ODBC Data Sources list near the top of the file to mention the new DSN:<\/p>\n<pre>\r\n[ODBC Data Sources]\r\nmyodbc3     = MySQL ODBC 3.51 Driver DSN \r\n<\/pre>\n<h3>C\u00e2\u2122\u00af ODBC connection example<\/h3>\n<p>Here is some example code to get you connected:<\/p>\n<pre class=\"brush:c-sharp\">\r\nusing System;\r\nusing System.Data;\r\nusing System.Data.Odbc;\r\n\r\nclass ODBCTest {\r\n  public static void Main(String[] args) {\r\n    \/\/ Connection string using explicit parameters                              \r\n    string ConStr = String.Format(\r\n      \"DRIVER={0};SERVER={1};DATABASE={2};UID={3};PWD={4}\",\r\n      \"{MySQL}\",\"mysql\",\"test\",\"testuser\",\"password\" );\r\n\r\n    \/\/ Connection string using DSN                                              \r\n\/\/  string ConStr = String.Format(\"DRIVER={0};DSN={1}\",                         \r\n\/\/                                \"{MySQL}\",\"myodbc3\");                       \r\n\r\n    \/\/Create the connection object                                              \r\n    var OdbcCon = new OdbcConnection( ConStr );\r\n\r\n    try {\r\n      Console.Write(\"Opening connection... \");\r\n\r\n      \/\/open the database connection                                            \r\n      if (OdbcCon.State == ConnectionState.Closed)\r\n        OdbcCon.Open();\r\n\r\n      Console.WriteLine(\"connection opened!\");\r\n    } catch (OdbcException Ex) {\r\n      Console.WriteLine(Ex.Message);\r\n    } finally {\r\n      \/\/close the database connection                                           \r\n      OdbcCon.Close();\r\n    }\r\n  }\r\n}\r\n<\/pre>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=cjamescollier\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>We had a visitor on #mono today who needed help with his homework. It seems that Reggie is happy to have forgotten everything about using ODBC to connect to MySQL. I was curious and feeling helpful, so I figured it out. Install the MySQL ODBC driver $ sudo apt-get install libmyodbc You can also grab [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[38,165,17,181,7,12,130,210],"tags":[],"class_list":["post-737","post","type-post","status-publish","format-standard","hentry","category-c","category-databases","category-debian","category-irc","category-mono","category-mysql","category-ubuntu","category-ubuntu-us-wa"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1YDIB-bT","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/posts\/737","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=737"}],"version-history":[{"count":19,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/posts\/737\/revisions"}],"predecessor-version":[{"id":756,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=\/wp\/v2\/posts\/737\/revisions\/756"}],"wp:attachment":[{"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=737"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=737"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.c9h.org\/cj\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=737"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}