当前文章的驱动版本为5.3.4,文件为:mysql-connector-odbc-5.3.4-win32.msi或者mysql-connector-odbc-5.3.4-winx64.msi
安装后,驱动为:ODBC driver Mysql ODBC 5.3 ANSI Driver
解决方案管理中添加引用 Microsoft.Data.Odbc.dll
代码中增加引用 using Microsoft.Data.Odbc
代码如下:
using System
using System.Collections.Generic
using System.ComponentModel
using System.Drawing
using System.Linq
using System.Text
using System.Windows.Forms
using Microsoft.Data.Odbc
namespace mysql
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent()
}
private void Form1_Load(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 5.3 ANSI Driver}" +
"SERVER=localhost" +
"DATABASE=inv" +
"UID=root" +
"PASSWORD=888888" +
"OPTION=3"
OdbcConnection MyConnection = new OdbcConnection(MyConString)
MyConnection.Open()
Console.WriteLine(" success, connected successfully ! ")
string query = "insert into test values( 'hello', 'lucas', 'liu')"
OdbcCommand cmd = new OdbcCommand(query, MyConnection)
//处理异常:插入重复记录有异常
try{
cmd.ExecuteNonQuery()
}
catch(Exception ex){
Console.WriteLine("record duplicate.")
}finally{
cmd.Dispose()
}
//***********************用read方法读数据到textbox**********************
string tmp1 = null
string tmp2 = null
string tmp3 = null
query = "select * from test "
OdbcCommand cmd2 = new OdbcCommand(query, MyConnection)
OdbcDataReader reader = cmd2.ExecuteReader()
while (reader.Read())
{
tmp1 = reader[0].ToString()
tmp2 = reader[1].ToString()
tmp3 = reader[2].ToString()
}
this.textBox1.Text = tmp1 + " " + tmp2 + " " + tmp3
*/
MyConnection.Close()
}
}
}
mysql官网上提供了各种语言连接数据库的类库,可到这里下载.net的http://www.mysql.com/products/connector/
有如下的各种支持类库:
ADO.NET Driver for MySQL (Connector/NET)
ODBC Driver for MySQL (Connector/ODBC)
JDBC Driver for MySQL (Connector/J)
MXJ Driver for MySQL (Connector/MXJ)
PHP Driver for MySQL (Connector/PHP - Windows Only)
C API for MySQL (mysqlclient)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)