代码
using System; using System.Collections.Generic; using System.Linq;
MySQLDriverCS是MySQL数据库的一个免费开源的.NET驱动程序,在 http://sourceforge.net/projects/mysqldrivercs/可以下载到,使用它不需要额外的去设置ODBC数据源,基本上只要能连接到MySQL就能通过MySQLDriverCS来访问。
在安装文件夹下面找到MySQLDriver.dll,然后将MySQLDriver.dll添加引用到项目中。
下面实现增删改操作:
using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using MySQLDriverCS; public partial class _Default : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { MySQLConnection conn = null ; try {
//关键是字符串的配置 string connstr = " Data Source=test;Password=carl;User ID=root;Location=localhost " ; conn = new MySQLConnection(connstr); conn.Open(); string query = " insert into people (name) values ('aaaa') " ; string tmp = null ; MySQLCommand cmd = new MySQLCommand(query, conn); for ( int i = 0 ; i < 100 ; i ++ ) { cmd.ExecuteNonQuery(); } cmd.Dispose(); conn.Close(); query = " select * from people " ; MySQLCommand cmd2 = new MySQLCommand(query, conn); conn.Open(); MySQLDataReader reader = cmd2.ExecuteReaderEx(); while (reader.Read()) { tmp = reader[ 0 ].ToString(); } conn.Close(); query = " delete from people " ; MySQLCommand cmd3 = new MySQLCommand(query, conn); conn.Open(); cmd3.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { conn.Close(); } }}