一、准备工具

visual stuido(本示例使用visual studio 2010)

MySql.Data.dll

mysql_installer_community_V5.6.21.1_setup.1415604646.msi(网上搜索去下载或者向我索要,联系方式在http://xunwn.com/1010100

二、新建DBUtility通用访问MySQL类库

1、在解决方案中新建一个DBUtility类库,作为访问MySQL的通用sql语句存放地点

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAJkAAAA/CAYAAADzGWpJAAANZUlEQVR4Ae1df2wb1R3/XJJCStFQ25WiFpoNxUULKSA2tmFTBKVIjSu0drTpKipSNuFMo2r8x1okFLFNqqqJaJozMSCGgbJ26iilDYXEUNpSmOKW/toKSdbUTkZSQtKQpPnR+sfd2Z6+zz77zo4dx/E5cXwnXd7du/e+773v++T7ftzne+YCgUAA2qFpQEUN5KkoWxOtaYBpQAOZBgTVNaCBTHUVawUUZEIFlR8/A5/gg48XIbpFCB4KBQihU3QJEFwCeLcQfO4W4POKuHC4PRPV08pQWQMZARm1oeDWNgREHzjej4DXB8Erwu0W4fEIwZBd++DlRfC8D4svLFG56Zr4TGkgYyAT+0vg50UE3CI4j4g5bgFwCyhwC1hacAseuPMnuLtoBTq6O/Dhp43owv8ypYPkynHWwqDbj3JHM6qKbajkGrAuUIcyRXxyonItVUZA9qi4Db8w/lChW9o38fsCoA2UvDwOXB4wt64Q/33yIp4y/gaPb6xUpI/cOFFr0MFsj8QAJjRRh8uibJUcjFZZhN4CR3MVilkUgWQXShhgZGkIMBVAfTid7Jl2mbIGVJ34v/+vdtB549xCFn56/it09Y1gaNSNoREXA1Z+Poeb37gJXYNfwSt6cffBEgwPX52wQaYmAmjobAKMHIdKmzKb3uIIp2kqNUMXnUCZPOqOwCyTWVyF5gBZsahkivioPFFJc/U2oSU7ceIEPv/8czz//PMp6+fxe74M5y24sQS9LmDFYQO+cX2D7o3dIIvmETxY/s5yYA7wyepP4eHFcJ6kLsrqEHCUwKCrhC3Kokn5dSV6oE2608JMaiCuJTt+/Di2bduGN998M+X6uN1uwM/j4y9WsHPIvQz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1Kvr8aQpPt2A4SDilHYU1paNSnYSFT0mOOua0aW4LjrufPL0ONQ9SNf6w4/NFo9hbqW19dK2EhU7JnjrhkJc9w1JLQjG4mKBHHcv321C9+/4QoNG9CcJB5P4uGnXsRDT7yAh9ZtxOi65xBetwHhJzZg7R/WI51OZdLnndA4O2+t04Euu5LStqwHGB7E4DCyO47EAgiIDXRt8I3REGsck9E8yfT4hGe8D4O6zXYjXv2QziSPWZDh/YTGce9X9tqyEced9oKocrxvKMNMiQrC8u47hsBAqAIBlSfl1a0imDHHvX4c95BbQqZp0zaqQ0UqwhClzPOkTH4H/H4PMGxIVNMLJl3VFM7KhTHHvXLM8nLQDpIDXYiq+xfnxVcZwD1JlQBWlT0WwEDIg35l/56qRHHm+iHARlI/bItKZo57UXgsFcnDLUtVBytjRQR4dcuKtcI6WQoBNhJLVQcrY0UE2EisWCusk6UQ+P/AtQCM3r8qVwAAAABJRU5ErkJggg==" alt="" />

3、配置web.config MySQL数据库连接字符串

在解决方案根目录下打开web.config配置文件,在connectionStrings节点中加入MySQL连接字符串

  <connectionStrings>
<add name="DBConnectionString" connectionString="Data Source=127.0.0.1;Database=你的数据库名;User ID=用户名;Password=密码" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

4、新建SQLHelper通用MySQL访问类,该类的定义如下:  

/// <summary>
///SQLHelper 的摘要说明
/// </summary>
public abstract class SQLHelper
{
//数据库连接字符串(注意:这里的“DBConnectionString”一定要与web.config文件中connectionStrings节点值一致)
public static readonly string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["DBConnectionString"].ToString(); // 用于缓存参数的HASH表
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); /// <summary>
/// 给定连接的数据库用假设参数执行一个sql命令(不返回数据集)
/// </summary>
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
/// <param name="cmdText">存储过程名称或者sql命令语句</param>
/// <param name="commandParameters">执行命令所用参数的集合</param>
/// <returns>执行命令所影响的行数</returns>
public static int ExecuteNonQuery(string cmdText, CommandType cmdType = CommandType.Text, params MySqlParameter[] commandParameters)
{ MySqlCommand cmd = new MySqlCommand(); using (MySqlConnection conn = new MySqlConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
} /// <summary>
/// 用执行的数据库连接执行一个返回数据集的sql命令
/// </summary>
/// <remarks>
/// 举例:
/// MySqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
/// </remarks>
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
/// <param name="cmdText">存储过程名称或者sql命令语句</param>
/// <param name="commandParameters">执行命令所用参数的集合</param>
/// <returns>包含结果的读取器</returns>
public static MySqlDataReader ExecuteReader(string cmdText, CommandType cmdType = CommandType.Text, params MySqlParameter[] commandParameters)
{
//创建一个MySqlCommand对象
MySqlCommand cmd = new MySqlCommand();
//创建一个MySqlConnection对象
MySqlConnection conn = new MySqlConnection(connectionString); //在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在,
//因此commandBehaviour.CloseConnection 就不会执行
try
{
//调用 PrepareCommand 方法,对 MySqlCommand 对象设置参数
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
//调用 MySqlCommand 的 ExecuteReader 方法
MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
//清除参数
cmd.Parameters.Clear();
return reader;
}
catch
{
//关闭连接,抛出异常
conn.Close();
throw;
}
} /// <summary>
/// 返回DataSet
/// </summary>
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
/// <param name="cmdText">存储过程名称或者sql命令语句</param>
/// <param name="commandParameters">执行命令所用参数的集合</param>
/// <returns></returns>
public static DataSet GetDataSet(string cmdText, CommandType cmdType = CommandType.Text, params MySqlParameter[] commandParameters)
{
//创建一个MySqlCommand对象
MySqlCommand cmd = new MySqlCommand();
//创建一个MySqlConnection对象
MySqlConnection conn = new MySqlConnection(connectionString); //在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在, try
{
//调用 PrepareCommand 方法,对 MySqlCommand 对象设置参数
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
//调用 MySqlCommand 的 ExecuteReader 方法
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = cmd;
DataSet ds = new DataSet(); adapter.Fill(ds);
//清除参数
cmd.Parameters.Clear();
conn.Close();
return ds;
}
catch (Exception e)
{
throw e;
}
} /// <summary>
/// 用指定的数据库连接字符串执行一个命令并返回一个数据集的第一列
/// </summary>
/// <remarks>
///例如:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
/// </remarks>
/// <param name="cmdType">命令类型(存储过程, 文本, 等等)</param>
/// <param name="cmdText">存储过程名称或者sql命令语句</param>
/// <param name="commandParameters">执行命令所用参数的集合</param>
/// <returns>用 Convert.To{Type}把类型转换为想要的 </returns>
public static object ExecuteScalar(string cmdText, CommandType cmdType = CommandType.Text, params MySqlParameter[] commandParameters)
{
MySqlCommand cmd = new MySqlCommand(); using (MySqlConnection connection = new MySqlConnection(connectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
} /// <summary>
/// 将参数集合添加到缓存
/// </summary>
/// <param name="cacheKey">添加到缓存的变量</param>
/// <param name="commandParameters">一个将要添加到缓存的sql参数集合</param>
public static void CacheParameters(string cacheKey, params MySqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
} /// <summary>
/// 找回缓存参数集合
/// </summary>
/// <param name="cacheKey">用于找回参数的关键字</param>
/// <returns>缓存的参数集合</returns>
public static MySqlParameter[] GetCachedParameters(string cacheKey)
{
MySqlParameter[] cachedParms = (MySqlParameter[])parmCache[cacheKey]; if (cachedParms == null)
return null; MySqlParameter[] clonedParms = new MySqlParameter[cachedParms.Length]; for (int i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (MySqlParameter)((ICloneable)cachedParms[i]).Clone(); return clonedParms;
} /// <summary>
/// 准备执行一个命令
/// </summary>
/// <param name="cmd">sql命令</param>
/// <param name="conn">OleDb连接</param>
/// <param name="trans">OleDb事务</param>
/// <param name="cmdType">命令类型例如 存储过程或者文本</param>
/// <param name="cmdText">命令文本,例如:Select * from Products</param>
/// <param name="cmdParms">执行命令的参数</param>
private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open(); cmd.Connection = conn;
cmd.CommandText = cmdText; if (trans != null)
cmd.Transaction = trans; cmd.CommandType = cmdType; if (cmdParms != null)
{
foreach (MySqlParameter parameter in cmdParms)
{
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
cmd.Parameters.Add(parameter);
}
}
} }

三、访问MySQL数据库(增删查改)

提示:sql语句无论是oracle、mysql、sqlserver,增删查改语句大体相似,皆为insert、delete、update、select操作,故熟悉一种数据库操作语句后,亦可相对容易学习其他数据库语句

using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using DBUtility;

  

1、插入操作(insert)

示例代码:

        public int Add( int userId, string name)
{
StringBuilder sb = new StringBuilder();
sb.Append("INSERT INTO T_Photo(Name,UserID) ");
sb.Append("VALUES(?Name,?UserID) ");
MySqlParameter[] parameters = {
new MySqlParameter("?Name", MySqlDbType.String),
new MySqlParameter("?UserID", MySqlDbType.Int32)
};
parameters[0].Value = name;
parameters[1].Value = userId;
return SQLHelper.ExecuteNonQuery(sb.ToString(), CommandType.Text, parameters);
}

  

2、删除操作(delete)

示例代码:

        public int Delete(long id, int userId)
{
StringBuilder sb = new StringBuilder();
sb.Append("DELETE FROM T_Photo WHERE ID = ?ID AND UserID = ?UserID");
MySqlParameter[] parameters = {
new MySqlParameter("?ID", MySqlDbType.Int64),
new MySqlParameter("?UserID", MySqlDbType.Int32)
};
parameters[0].Value = id;
parameters[1].Value = userId;
return SQLHelper.ExecuteNonQuery(sb.ToString(), CommandType.Text, parameters);
}

  

3、修改操作(update)

示例代码:

        public int EditName(long id, int userId, string name)
{
StringBuilder sb = new StringBuilder();
sb.Append("UPDATE T_Photo SET Name = ?Name WHERE ID = ?ID AND UserID = ?UserID");
MySqlParameter[] parameters = {
new MySqlParameter("?ID", MySqlDbType.Int64),
new MySqlParameter("?UserID", MySqlDbType.Int32),
new MySqlParameter("?Name", MySqlDbType.String)
};
parameters[0].Value = id;
parameters[1].Value = userId;
parameters[2].Value = name;
return SQLHelper.ExecuteNonQuery(sb.ToString(), CommandType.Text, parameters);
}

  

4、查询操作(select)

示例代码:

        public MySqlDataReader GetListByOrderCode(string orderCode)
{
StringBuilder sb = new StringBuilder();
sb.Append("SELECT ID,OrderCount,Subtotal,ProductID, ");
sb.Append("FROM t_orderdetail "); //筛选条件
sb.Append("WHERE OrderCode = ?OrderCode "); //排序
sb.Append("ORDER BY ID DESC "); MySqlParameter[] parameters = {
new MySqlParameter("?OrderCode", MySqlDbType.String)
}; parameters[0].Value = orderCode;
return SQLHelper.ExecuteReader(sb.ToString(), CommandType.Text, parameters); }

  

5、调用存储过程

示例代码:

        public int BackOrder(long id)
{
StringBuilder sb = new StringBuilder(); sb.Append("BackOrder ");//存储过程名称 MySqlParameter[] parameters = {
new MySqlParameter("?OrderId", MySqlDbType.Int64)//OrderId必须与存储过程参数名、数据类型一致
};
parameters[0].Value = id;
return MyCustomSQLHelper.ExecuteNonQuery(sb.ToString(), CommandType.StoredProcedure, parameters);
}

  

今天就分享到这里,都是自己积累的心得分享,若有疑问请到http://xunwn.com/1010100联系我,谢谢,今后也会分享更多给大家

更多分享:http://xunwn.com/photo/1010100

C#连接、访问MySQL数据库的更多相关文章

  1. 在Eclipse中使用JDBC访问MySQL数据库的配置方法

    在Eclipse中使用JDBC访问MySQL数据库的配置方法 分类: DATABASE 数据结构与算法2009-10-10 16:37 5313人阅读 评论(10) 收藏 举报 jdbcmysql数据 ...

  2. C#访问MySQL数据库(winform+EF)

    原文:C#访问MySQL数据库(winform+EF) 以前都是C#连接SQLServer,现在MySQL也比较火了,而且是开源跨平台的,这里连接使用一下,主要是体会一下整个流程,这里使用的是winf ...

  3. 连接远程MySQL数据库项目启动时,不报错但是卡住不继续启动的,

    连接远程MySQL数据库项目启动时,不报错但是卡住不继续启动的, 2018-03-12 17:08:52.532DEBUG[localhost-startStop-1]o.s.beans.factor ...

  4. 关于利用PHP访问MySql数据库的逻辑操作以及增删改查实例操作

    PHP访问MySql数据库 <?php //造连接对象$db = new MySQLi("localhost","root","",& ...

  5. Jmeter配置通过SSH连接的mysql数据库

    jmeter连接配置mysql数据库时,如果数据库服务器没有通过ssh连接,则只需要添加配置相应的jdbc参数就可以了.如果数据库服务器是通过SSH连接的,则需要通过中间远程连接工具来登录,此处使用的 ...

  6. MySQL入门很简单: 15 java访问MySQL数据库

    1. 连接数据库 1.1 下载安装驱动 java通过JDBC(Java Database Connectivity,Java数据库连接)来访问MySQL数据库.JDBC的编程接口提供的接口和类与MyS ...

  7. 怎么设置才能让外网ip可以访问mysql数据库[转]

    转自: http://www.hongyanliren.com/89.html 使用mysql中,很多人都会遇到这样的问题:在vps服务器或者云服务器上安装了mysql后,使用其他工具在外网ip之下根 ...

  8. android开发 如何通过web服务器访问MYSQL数据库并且使其数据同步到android SQLite数据库?

    通过web服务器访问MYSQL数据库有以下几个过程: 1.在MySql下创建自己的数据库和自己的表单 2.连接数据库. 3.访问数据库 1.创建web工程 (服务器端) 在Myeclipse下新建一个 ...

  9. Linux下C++访问MySQL数据库

    由于想要开始了解并学习用LAMP进行web开发,所以昨晚我在Fedora上安装了MySQL,学习了MySQL的几个常用命令.想着在学习进行web开发(PHP访问数据库)之前,先用我熟悉的C++连接数据 ...

随机推荐

  1. java 中关于synchronized的通常用法

    package j2se.thread.test; /*** * synchronized(class)很特别,它会让另一个线程在任何需要获取class做为monitor的地方等待. * class与 ...

  2. Jquery与CSS选择器参考手册

  3. 转自“脚本之家”!!JDBC之PreparedStatement类中预编译的综合应用解析

    JDK 文档:SQL 语句被预编译并存储在 PreparedStatement 对象中(PreparedStatement是存储在JDBC里的,初始化后,缓存到了JDBC里),然后可以使用此对象多次高 ...

  4. Waring:This LinearLayout layout or its FrameLayout parent is useless; transfer the background attribute to the other view

    解决方法请参考: You have a single component (row) vertical linear layout, containing another linear layout. ...

  5. 关于CSS的图像放大问题的解决,需要借助jQuery等直接用css3设置

    W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发, ...

  6. BZOJ 2142: 礼物

    模非素数下的排列组合,简直凶残 调着调着就过了= = 都不知道怎么过的= = 直接上链接http://hi.baidu.com/aekdycoin/blog/item/147620832b567eb4 ...

  7. Java面向对象——类,对象和方法

    1.类的概念 在生活中,说到类,可以联想到类别,同类,会想到一类人,一类事物等等.而这一类人或事物都是具有相同特征或特点和行为的,我们根据不同的特征或特点和行为将他们归类或分类.同时,当我们认识一个新 ...

  8. sklearn错误

    1.No module named 'sklearn.cross_validation' sklearn.cross_validation会报错,关键在于新版本的sklearn没有cross_vali ...

  9. 安装edusoho

    1.更新第三方源并升级系统 (CentOS默认的标准源里没有nginx软件包) 1.1.安装CentOS第三方yum源 #安装下载工具wget yum install wget #下载atomic y ...

  10. LINE@生活圈招募好友秘笈

    什么是「获得更多好友」页面? 您可从  LINE@ app >管理>获得更多好友  进入此页面. ▼ 「获得更多好友」新介面中,募集好友的四大秘诀 秘诀一.「以社群网站或电子邮件分享」 • ...