C # connect to MySQL through ODBC

Source: Internet
Author: User

First, downloadMySQL connector/ODBC 3.51 downloads

Download the package and go to Control Panel> Management Tools> data source (ODBC). Double-click it and click Add,

Select MySql ODBC 3.51 driver

Then, the MySQL parameter configuration window is displayed, as shown below:

After entering the relevant parameters, click test. If the connection is successful, confirm the configuration.

Below is C #Code

 

Code
//Add namespace
UsingSystem. Data. ODBC;

 

Code
Private   Void Page_load ( Object Sender, system. eventargs E)
{
// Place user code here to initialize the page
String Strconn =   " Driver = {MySQL ODBC 3.51 driver}; server = 192.168.1.105; database = mydb; user = Joe; Password = AST; option = 3; " ;

String SQL =   " Select title, tid from Info " ;
Odbcconnection myconnection =   New Odbcconnection (strconn );
Odbccommand mycommand =   New Odbccommand (SQL, myconnection );
Myconnection. open ();
Dropdownlist1.datasource = Mycommand. executereader (commandbehavior. closeconnection );
Dropdownlist1.datatextfield =   " Title " ;
Dropdownlist1.datavaluefield =   " TID " ;
Dropdownlist1.databind ();
Myconnection. Close ();
}

 

MySQL connector/ODBC 3.51 (myodbc 3.51)

Remote database connection to MySQL

 

Code
Driver={MySQL ODBC3.51Driver}; server=Data.domain.com; Database=Mydatabase; User=Myusername; Password=Mypassword; Option=3;

 

PS: in Visual Studio 2003, MySQL connector/ODBC 5.1 has the following exception:

"Error [hy000] [MySQL] [ODBC 5.1 Driver] Access denied for user 'odbc '@ '192. 168.1.104' (using password: Yes)"

 

Test environment of this post:

OS: Windows xp2

Development Environment: Visual Studio 2003 Asp.net 1.1

MySQL database server: Fedora Core 9 Linux

MySQL version: MySQL ver 14.12 distrib 5.0.51a, for Redhat-Linux-GNU (i386) using Readline 5.0

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.