以ADO形式操作mysql数据库
首先得需要一个连接mysql的helper类:
public class MySqlHelper
{
#region [ Connection ]
public static string connectionString = "Database=newspublish;Data Source=127.0.0.1;Port=3306;User Id=root;Password=123456;Charset=utf8;SslMode=none;";
public static MySqlConnection GetConnection
{
get
{
return new MySqlConnection(connectionString);
}
}
#endregion
#region [ ExecuteNonQuery ]
/// <summary>
/// 普通SQL语句执行增删改
/// </summary>
/// <param name="cmdText">SQL语句</param>
/// <param name="commandParameters">可变参数</param>
/// <returns>受影响行数</returns>
public static int ExecuteNonQuery(string cmdText, params MySqlParameter[] commandParameters)
{
return ExecuteNonQuery(cmdText, CommandType.Text, commandParameters);
}
/// <summary>
/// 存储过程执行增删改
/// </summary>
/// <param name="cmdText">存储过程</param>
/// <param name="commandParameters">可变参数</param>
/// <returns>受影响行数</returns>
public static int ExecuteNonQueryByProc(string cmdText, params MySqlParameter[] commandParameters)
{
return ExecuteNonQuery(cmdText, CommandType.StoredProcedure, commandParameters);
}
/// <summary>
/// 执行增删改
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="cmdType">命令类型</param>
/// <param name="commandParameters">可变参数</param>
/// <returns>受影响行数</returns>
public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params MySqlParameter[] commandParameters)
{
int result = 0;
using (MySqlConnection conn = GetConnection)
{
try
{
MySqlCommand command = new MySqlCommand();
PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
result = command.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
return result;
}
#endregion
#region [ ExecuteReader ]
/// <summary>
/// SQL语句得到 MySqlDataReader 对象
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">可变参数</param>
/// <returns>MySqlDataReader 对象</returns>
public static MySqlDataReader ExecuteReader(string cmdText, params MySqlParameter[] commandParameters)
{
return ExecuteReader(cmdText, CommandType.Text, commandParameters);
}
/// <summary>
/// 存储过程得到 MySqlDataReader 对象
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">可变参数</param>
/// <returns>MySqlDataReader 对象</returns>
public static MySqlDataReader ExecuteReaderByProc(string cmdText, params MySqlParameter[] commandParameters)
{
return ExecuteReader(cmdText, CommandType.StoredProcedure, commandParameters);
}
/// <summary>
/// 得到 MySqlDataReader 对象
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="cmdType">命令类型</param>
/// <param name="commandParameters">可变参数</param>
/// <returns>MySqlDataReader 对象</returns>
public static MySqlDataReader ExecuteReader(string cmdText, CommandType cmdType, params MySqlParameter[] commandParameters)
{
MySqlDataReader result = null;
using (MySqlConnection conn = GetConnection)
{
try
{
MySqlCommand command = new MySqlCommand();
PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
result = command.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
return result;
}
#endregion
#region [ ExecuteDataSet ]
/// <summary>
/// 执行SQL语句, 返回DataSet
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">可变参数</param>
/// <returns> DataSet </returns>
public static DataSet ExecuteDataSet(string cmdText, params MySqlParameter[] commandParameters)
{
return ExecuteDataSet(cmdText, CommandType.Text, commandParameters);
}
/// <summary>
/// 执行存储过程, 返回DataSet
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">可变参数</param>
/// <returns> DataSet </returns>
public static DataSet ExecuteDataSetByProc(string cmdText, params MySqlParameter[] commandParameters)
{
return ExecuteDataSet(cmdText, CommandType.StoredProcedure, commandParameters);
}
/// <summary>
/// 返回DataSet
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="cmdType">命令类型</param>
/// <param name="commandParameters">可变参数</param>
/// <returns> DataSet </returns>
public static DataSet ExecuteDataSet(string cmdText, CommandType cmdType, params MySqlParameter[] commandParameters)
{
DataSet result = null;
using (MySqlConnection conn = GetConnection)
{
try
{
MySqlCommand command = new MySqlCommand();
PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
MySqlDataAdapter adapter = new MySqlDataAdapter();
adapter.SelectCommand = command;
result = new DataSet();
adapter.Fill(result);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
return result;
}
#endregion
#region [ ExecuteDataTable ]
/// <summary>
/// 执行SQL语句, 返回DataTable
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">可变参数</param>
/// <returns> DataTable </returns>
public static DataTable ExecuteDataTable(string cmdText, params MySqlParameter[] commandParameters)
{
return ExecuteDataTable(cmdText, CommandType.Text, commandParameters);
}
/// <summary>
/// 执行存储过程, 返回DataTable
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="commandParameters">可变参数</param>
/// <returns> DataTable </returns>
public static DataTable ExecuteDataTableByProc(string cmdText, params MySqlParameter[] commandParameters)
{
return ExecuteDataTable(cmdText, CommandType.StoredProcedure, commandParameters);
}
/// <summary>
/// 返回DataTable
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="cmdType">命令类型</param>
/// <param name="commandParameters">可变参数</param>
/// <returns> DataTable </returns>
public static DataTable ExecuteDataTable(string cmdText, CommandType cmdType, params MySqlParameter[] commandParameters)
{
DataTable dtResult = null;
DataSet ds = ExecuteDataSet(cmdText, cmdType, commandParameters);
if (ds != null && ds.Tables.Count > 0)
{
dtResult = ds.Tables[0];
}
return dtResult;
}
#endregion
#region [ ExecuteScalar ]
/// <summary>
/// 普通SQL语句执行ExecuteScalar
/// </summary>
/// <param name="cmdText">SQL语句</param>
/// <param name="commandParameters">可变参数</param>
/// <returns>受影响行数</returns>
public static object ExecuteScalar(string cmdText, params MySqlParameter[] commandParameters)
{
return ExecuteScalar(cmdText, CommandType.Text, commandParameters);
}
/// <summary>
/// 存储过程执行ExecuteScalar
/// </summary>
/// <param name="cmdText">存储过程</param>
/// <param name="commandParameters">可变参数</param>
/// <returns>受影响行数</returns>
public static object ExecuteScalarByProc(string cmdText, params MySqlParameter[] commandParameters)
{
return ExecuteScalar(cmdText, CommandType.StoredProcedure, commandParameters);
}
/// <summary>
/// 执行ExecuteScalar
/// </summary>
/// <param name="cmdText">命令字符串</param>
/// <param name="cmdType">命令类型</param>
/// <param name="commandParameters">可变参数</param>
/// <returns>受影响行数</returns>
public static object ExecuteScalar(string cmdText, CommandType cmdType, params MySqlParameter[] commandParameters)
{
object result = null;
using (MySqlConnection conn = GetConnection)
{
try
{
MySqlCommand command = new MySqlCommand();
PrepareCommand(command, conn, cmdType, cmdText, commandParameters);
result = command.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
if (conn != null && conn.State != ConnectionState.Closed)
conn.Close();
}
}
return result;
}
#endregion
#region [ PrepareCommand ]
/// <summary>
/// Command对象执行前预处理
/// </summary>
/// <param name="command"></param>
/// <param name="connection"></param>
/// <param name="trans"></param>
/// <param name="cmdType"></param>
/// <param name="cmdText"></param>
/// <param name="commandParameters"></param>
private static void PrepareCommand(MySqlCommand command, MySqlConnection connection, CommandType cmdType, string cmdText, MySqlParameter[] commandParameters)
{
try
{
if (connection.State != ConnectionState.Open) connection.Open();
command.Connection = connection;
command.CommandText = cmdText;
command.CommandType = cmdType;
//command.CommandTimeout = 3600; //此处请自定义
//if (trans != null) command.Transaction = trans;
if (commandParameters != null)
{
foreach (MySqlParameter parm in commandParameters)
command.Parameters.Add(parm);
}
}
catch
{
}
}
#endregion
}
新建控制台应用程序:
static void Main(string[] args)
{
//string sql = "select title from news where id = 10";/*查询*/
//string result = (string)MySqlHelper.ExecuteScalar(sql);
//string sql = "INSERT INTO news(title,content,typeid) values('111','222',6)";/*新增*/
//string sql = "UPDATE news SET title = '666' WHERE id = 11";/*更新*/
//string sql = "DELETE FROM news WHERE id = 11";/*删除*/
//int i = MySqlHelper.ExecuteNonQuery(sql);
//Console.WriteLine(i);
Console.Read();
}
以ADO形式操作mysql数据库的更多相关文章
- 以EF形式操作mysql数据库
1.引入Nuget包: 2.书写EF操作上下文 public class MySqlContext:DbContext { protected override void OnConfiguring( ...
- ADO.NET操作MySQL数据库
前言 ADO.NET包括5大对象,分别是Connection.Command.DataReader.DataSet.DataAdapter,使用ADO.NET访问数据库有两个步骤:建立数据库连接.读取 ...
- 转 用C API 操作MySQL数据库
用C API 操作MySQL数据库 参考MYSQL的帮助文档整理 这里归纳了C API可使用的函数,并在下一节详细介绍了它们.请参见25.2.3节,“C API函数描述”. 函数 描述 mysql_a ...
- Code First操作Mysql数据库
前面博客也讲了,自己做一个网站,选用的是MVC+EF Code First+MySql+EasyUI,先说下技术选型.一.为什么选择MVC? 因为之前自己做的系统大部分是webForm,MVC的之前也 ...
- python【第十二篇下】操作MySQL数据库以及ORM之 sqlalchemy
内容一览: 1.Python操作MySQL数据库 2.ORM sqlalchemy学习 1.Python操作MySQL数据库 2. ORM sqlachemy 2.1 ORM简介 对象关系映射(英语: ...
- (转)防止人为误操作MySQL数据库技巧一例
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://oldboy.blog.51cto.com/2561410/1321061 防止人 ...
- 一、初识MySQL数据库 二、搭建MySQL数据库(重点) 三、使用MySQL数据库 四、认识MySQL数据库的数据类型 五、操作MySQL数据库的数据(重点)
一.初识MySQL数据库 ###<1>数据库概述 1. 数据库 长期存储在计算机内的,由组织的可共享的数据集合 存储数据的仓库 文件 ...
- 【转】python操作mysql数据库
python操作mysql数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库 ...
- python接口自动化(三十八)-python操作mysql数据库(详解)
简介 现在的招聘要求对QA人员的要求越来越高,测试的一些基础知识就不必说了,来说测试知识以外的,会不会一门或者多门开发与语言,能不能读懂代码,会不会Linux,会不会搭建测试系统,会不会常用的数据库, ...
随机推荐
- 懒汉单例安全basedao
package Dao; import java.sql.*; public class BaseDao { private String drname = "com.mysql.jdbc. ...
- div按照屏幕尺寸(设备大小)进行缩放
原理:利用css3 transform 属性 代码: body{ width: 810px; height: 340px; margin: 0px; padding: 0px; background- ...
- hibernate cascade的真正含义
hibernate cascade 是 @OneToOne @OneToMany @ManyToOne @ManyToMany等注解的属性,表示级联操作. /** * (Optional) The o ...
- 小白学flask之静态文件
引入css的方式有两种 1 那在flask中,如何处理静态文件? 做法很简单,只要在你的包或模块旁边创建一个名为 static 的文件夹就行了. flask的静态文件是位于应用的 /static 中的
- canvas画布基础知识
<canvas> </canvas>标签用于绘制图像(通过脚本,通常是 JavaScript). <canvas> 元素本身并没有绘制能力(它仅仅是图形的容器) - ...
- 获取当前时间CTime
std::string getcurtime(){ USES_CONVERSION; CTime z_CurTime; CString z_TimeStr; z_CurTime = CTime::Ge ...
- Eclipse如何设置编译文件.class输出路径
1.首先我发现我的eclipse中-->project-->build automatically 是勾选上的.好吧,把把前面的勾去掉. 2去掉以后我先clean --> Clean ...
- Infragist ics Ult imate 2015 Vol.1 - Product Keys
Ultimate ProductKeys 2015 Vol.1: 9122-1900164-4504144 9122-9300855-3994197 9122-7600717-4579130 9122 ...
- [问题记录]libpomelo的安装
1. 描述: 按照github上的操作完成 Windows in your libpomelo project root directory open git bash and type in mkd ...
- Markdown学习使用
本文记录Markdown的基础应用. 一.基础知识 Markdown 是一种标记语言 文件后缀名:.md 编辑工具:VSCode(visual studio code) VSCode中预览模式快捷键: ...