aspx(c#)怎么向MySQL数据库插入数据,不要odbc,急切需要

aspx(c#)怎么向MySQL数据库插入数据,不要odbc,急切需要,第1张

mysql的odbc驱动下载官方地址为:http://dev.mysql.com/downloads/connector/odbc/[喝小酒的网摘]http://blog.hehehehehe.cn/a/17717.htm

当前文章的驱动版本为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)


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7282234.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-03
下一篇 2023-04-03

发表评论

登录后才能评论

评论列表(0条)

保存