博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#访问MySQL数据库的方法
阅读量:5060 次
发布时间:2019-06-12

本文共 3941 字,大约阅读时间需要 13 分钟。

C#访问MySQL数据库的方法

(1)首先需要下载C#访问MySQL数据库的ADO.NET驱动程序

下载地址为:

http://dev.mysql.com/downloads/connector/net/6.0.html

我下载的版本为: mysql-connector-net-6.3.8.msi

下载地址如下url:

(2)安装mysql-connector-net

然后直接在Windows操作系统安装 mysql-connector-net-6.3.8.msi

默认是安装在C盘:

C:\Program Files\MySQL\MySQL Connector Net 6.3.8\Assemblies

v2.0

v4.0

安装完后我选择的是v2.0版本的

然后在应用工程中引用组件MySQL.Data.dll

(3)封装数据库访问组件DbConnectionMySQL

/// <summary> /// MySQL数据库 /// 版本 mysql-connector-net-6.3.8.msi /// vp:hsg /// create date:2012-02-28 /// </summary> [Serializable] public class DbConnectionMySQL : DbConnectionWrapper { public DbConnectionMySQL(string pConnectionString) : base(pConnectionString) { this.m_dbconn = new MySqlConnection(pConnectionString); this.m_DbConnState = DbConnState.Free; } //-- public override DbDataAdapter GetDbDataAdapter() { return new MySqlDataAdapter(); } public override DbDataAdapter GetDbDataAdapter(DbCommand dbCommand) { return new MySqlDataAdapter(dbCommand as MySqlCommand); } public override DbCommand GetDbCommand() { return new MySqlCommand(); } public override DbConnection GetDbConnection() { return new MySqlConnection(); } public override DbCommandBuilder GetDbCommandBuilder() { return new MySqlCommandBuilder(); } public override DataProviderType GetCurrentDataProviderType() { return DataProviderType.Sql; } public override bool IsExistsTable(string TableName, string UserName) { #region information bool rbc = false; //TABLES表中去查询 table_name string dSql = "select * from TABLES where table_name='" + TableName + "'"; DataSet ds = this.ExecuteDataSet(dSql); if (ds != null) { if (ds.Tables[0].Rows.Count > 0) { rbc = true; } else { rbc = false; } } else { rbc = false; } return rbc; #endregion } public override bool IsExistsField(string FieldName, string TableName) { #region information bool rbc = false; string dSql = ""; dSql = "select * from " + TableName + " where 1<>1"; DataSet ds = this.ExecuteDataSet(dSql); if (ds != null) { DataTable dt = ds.Tables[0]; for (int j = 0; j < dt.Columns.Count; j++) { if (dt.Columns[j].ColumnName.ToString().ToUpper() == FieldName.ToString().ToUpper()) { rbc = true; goto Return_End; } } dt.Dispose(); dt = null; } ds.Dispose(); ds = null; Return_End: return rbc; #endregion } public override char ParameterChar { get { return ':'; //SQLite的参数符号为: } } public override DbParameter CreateParameter(string name, object value) { return new MySqlParameter(name, value); } public override DbParameter CreateParameter(string name) { DbParameter dbp = new MySqlParameter(); dbp.ParameterName = name; return dbp; } public override DbParameter CreateParameter(string name, DbType dbtype, object value) { DbParameter dbp = new MySqlParameter(); dbp.ParameterName = name; dbp.Value = value; dbp.DbType = dbtype; return dbp; } public override DbParameter CreateParameter(string name, DbType dbtype, int size, object value) { DbParameter dbp = new MySqlParameter(); dbp.ParameterName = name; dbp.Value = value; dbp.DbType = dbtype; dbp.Size = size; return dbp; } }

(4)客户端访问测试开发实例

public void TestCShape_MySQL() { string constr = "server=localhost;User Id=root;password=root;Database=xp_users"; DbConnectionWrapper dbw = new DbConnectionMySQL(constr); bool rbc=dbw.TestConnection(); this.Context.Response.Write(rbc); string x = ""; //删除语句 x = "delete from xp_users"; if (dbw.ExecuteQuery(x) > 0) { this.Context.Response.Write("删除语句成功!下面是SQL语句<br>" + x); } //插入语句 x = "insert into xp_users(gid,uid,uname,sex,email,pwd) values('"; x += "1','hsg77','何XXX',1,'hsg77@163.com','1')"; if (dbw.ExecuteQuery(x) > 0) { this.Context.Response.Write("插入语句成功!下面是SQL语句<br>"+x); } //查询语句 DataTable dt = dbw.ExecuteDataTable("select * from xp_users"); if (dt != null && dt.Rows.Count > 0) { this.Context.Response.Write("<br>用户数:"+dt.Rows.Count); } if (dt != null) { dt.Dispose(); dt = null; } dbw.Dispose(); dbw = null; }

----the---end---

create date:2012-02-28

转载于:https://www.cnblogs.com/sqlite3/archive/2012/02/28/2566742.html

你可能感兴趣的文章
WPF程序加入3D模型
查看>>
WPF中实现多选ComboBox控件
查看>>
读构建之法第四章第十七章有感
查看>>
C#中的IEnumerable<T>知识点
查看>>
android访问链接时候报java.net.MalformedURLException: Protocol not found
查看>>
dwz ie10一直提示数据加载中
查看>>
Windows Phone开发(4):框架和页 转:http://blog.csdn.net/tcjiaan/article/details/7263146
查看>>
Windows Phone Marketplace 发布软件全攻略
查看>>
Unity3D研究院之打开Activity与调用JAVA代码传递参数(十八)【转】
查看>>
语义web基础知识学习
查看>>
hexo个人博客添加宠物/鼠标点击效果/博客管理
查看>>
python asyncio 异步实现mongodb数据转xls文件
查看>>
关于WPF的2000件事 02--WPF界面是如何渲染的?
查看>>
单元测试、、、
查看>>
SVN使用教程总结
查看>>
JS 浏览器对象
查看>>
TestNG入门
查看>>
【ul开发攻略】HTML5/CSS3菜单代码 阴影+发光+圆角
查看>>
虚拟中没有eth0
查看>>
Unity 3D游戏开发学习路线(方法篇)
查看>>