代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
MySQL 数据库操作类
作者:
Dezai.CN
/ 发布于
2011/7/11
/
659
<div>using System;</div> <div>using System.Collections.Generic;</div> <div>using System.Text;</div> <div>using System.Data;</div> <div>using MySql.Data.MySqlClient;</div> <div></div> <div>namespace DevSDK.Data</div> <div>{</div> <div> public class MySQL</div> <div> {</div> <div> private string _ConnectionString = "";</div> <div></div> <div> private string _CommandText = "";</div> <div></div> <div> private MySqlConnection Conn = null;</div> <div></div> <div> private DataSet _DSAFDataSet = null;</div> <div> /// <summary></div> <div> /// 获取或设置数据库连接字串</div> <div> /// </summary></div> <div> public string ConnectionString { get { return this._ConnectionString; } set { this._ConnectionString = value; } } <div> /// <summary></div> <div> /// 获取或设置SQL语句</div> <div> /// </summary></div> <div> public string CommandText { get { return this._CommandText; } set { this._CommandText = value; } } <div></div> <div> public MySQL()</div> <div> {</div> <div> this._DSAFDataSet = new DataSet();</div> <div> } <div> /// <summary></div> <div> /// 执行 SQL 语句,并把执行结果集填充到一个DataSet容器中</div> <div> /// </summary></div> <div> /// <returns></returns></div> <div> public DataSet Execute()</div> <div> {</div> <div> if (string.IsNullOrEmpty(_ConnectionString)) { throw (new Exception("无传入数据连接字串")); } <div></div> <div> if (string.IsNullOrEmpty(_CommandText)) { throw (new Exception("无传入SQL语句")); } <div></div> <div> if (!DSAFConnectionTest()) { throw (new Exception("尝试数据库连接失败")); } <div></div> <div> try</div> <div> {</div> <div> Conn = new MySqlConnection(); Conn.ConnectionString = _ConnectionString;</div> <div></div> <div> MySqlDataAdapter MySqlDa = new MySqlDataAdapter();</div> <div></div> <div> MySqlDa.SelectCommand.CommandText = _CommandText; MySqlDa.SelectCommand.Connection = Conn;</div> <div></div> <div> MySqlDa.Fill(_DSAFDataSet); MySqlDa.Dispose();</div> <div> } <div> catch (MySqlException MySqlEx) { throw MySqlEx; } <div> catch (Exception ex) { throw ex; } <div></div> <div> return _DSAFDataSet;</div> <div> } <div> /// <summary></div> <div> /// 执行 SQL 语句,并返回受影响的行数</div> <div> /// </summary></div> <div> /// <returns></returns></div> <div> public int ExecuteNonQuery()</div> <div> {</div> <div> if (string.IsNullOrEmpty(_ConnectionString)) { throw (new Exception("无传入数据连接字串")); } <div></div> <div> if (string.IsNullOrEmpty(_CommandText)) { throw (new Exception("无传入SQL语句")); } <div></div> <div> if (!DSAFConnectionTest()) { throw (new Exception("尝试数据库连接失败")); } <div></div> <div> int iQuery = 0; MySqlTransaction Transaction = null;</div> <div></div> <div> try</div> <div> {</div> <div> MySqlCommand Comm = new MySqlCommand(); Conn = new MySqlConnection();</div> <div></div> <div> Conn.ConnectionString = _ConnectionString; Comm.Connection = Conn; Comm.CommandText = _CommandText;</div> <div></div> <div> Transaction = Conn.BeginTransaction(); Comm.Transaction = Transaction;</div> <div></div> <div> iQuery = Comm.ExecuteNonQuery(); Transaction.Commit(); return iQuery;</div> <div> } <div> catch (MySqlException MySqlEx) { Transaction.Rollback(); throw MySqlEx; } <div> catch (Exception ex) { throw ex; } <div> finally { Transaction.Dispose(); Conn.Close(); Conn.Dispose(); } <div> } <div> /// <summary></div> <div> /// 执行 SQL 语句,并返回结果集中的第一行第一列。忽略其它行或列</div> <div> /// </summary></div> <div> /// <returns></returns></div> <div> public object ExecuteScalar()</div> <div> {</div> <div> if (string.IsNullOrEmpty(_ConnectionString)) { throw (new Exception("无传入数据连接字串")); } <div></div> <div> if (string.IsNullOrEmpty(_CommandText)) { throw (new Exception("无传入SQL语句")); } <div></div> <div> if (!DSAFConnectionTest()) { throw (new Exception("尝试数据库连接失败")); } <div></div> <div> object iQuery = null; MySqlTransaction Transaction = null;</div> <div></div> <div> try</div> <div> {</div> <div> MySqlCommand Comm = new MySqlCommand(); Conn = new MySqlConnection();</div> <div></div> <div> Conn.ConnectionString = _ConnectionString; Comm.Connection = Conn; Comm.CommandText = _CommandText;</div> <div></div> <div> Transaction = Conn.BeginTransaction(); Comm.Transaction = Transaction;</div> <div></div> <div> iQuery = Comm.ExecuteScalar(); Transaction.Commit(); return iQuery;</div> <div> } <div> catch (MySqlException MySqlEx) { Transaction.Rollback(); throw MySqlEx; } <div> catch (Exception ex) { throw ex; } <div> finally { Transaction.Dispose(); Conn.Close(); Conn.Dispose(); } <div> } <div> /// <summary></div> <div> /// 测试数据库连接</div> <div> /// </summary></div> <div> /// <returns></returns></div> <div> private bool DSAFConnectionTest()</div> <div> {</div> <div> try</div> <div> {</div> <div> Conn = new MySqlConnection(); Conn.ConnectionString = _ConnectionString;</div> <div></div> <div> Conn.Open(); Conn.Close();</div> <div> } <div> catch { return (false); } <div></div> <div> return (true);</div> <div> } <div></div> <div> /// <summary></div> <div> /// 释放由 DevSDK.Data.MySql 使用的所有资源</div> <div> /// </summary></div> <div> public void Dispose()</div> <div> {</div> <div> if (Conn != null) { this.Conn.Dispose(); } <div></div> <div> if (_DSAFDataSet != null) { this._DSAFDataSet = null; } <div> } <div> } <div>} <div> </div>
试试其它关键字
MySQL数据库操作类
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3