C# 与MySQL
1. MySQL.Data.dll http://files.cnblogs.com/files/lwngreat/MySql.Data.rar
2.在工程中添加引用
3. 使用 MysqlHelper.cs 方便MySQL的应用
/******************************************** * MySQL数据库接口 * 文件明: MysqlHelper.cs *******************************************/ using System; using System.Data; using MySql.Data.MySqlClient; namespace DatabaseToShp { public class MysqlHelper { /// <summary> /// 数据库连接串 /// </summary> private string ConnString = "Database=amtiot_bak;Data Source=192.168.17.130;User Id=root;Password=sql5;pooling=false;CharSet=utf8;port=3306"; /// <summary> /// 数据库连接 /// </summary> private MySqlConnection Conn; /// <summary> /// 错误信息 /// </summary> public static string ErrorString = ""; /// <summary> /// 超时(秒) /// </summary> ; /// <summary> /// 初始化数据库链接 /// </summary> /// <param name="connString">数据库链接</param> public MysqlHelper(string connString) { ConnString = connString; ConnTo(); } /// <summary> /// 执行sql返回DataTable /// </summary> /// <param name="SqlString">SQL语句</param> /// <param name="parms">Sql参数</param> /// <returns>DataTable</returns> public DataTable ExecuteDataTable(string SqlString, MySqlParameter[] parms) { if (Conn == null || Conn.State != ConnectionState.Open) ConnTo(); try { MySqlCommand cmd = new MySqlCommand(); cmd.Connection = Conn; cmd.CommandType = CommandType.Text; cmd.CommandText = SqlString; cmd.CommandTimeout = TimeOut; if (parms != null) foreach (MySqlParameter pram in parms) cmd.Parameters.Add(pram); DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader()); return dt; } catch (Exception e) { AddError(e.Message, SqlString); return null; } } /// <summary> /// 返回第一行 /// </summary> /// <param name="SqlString"></param> /// <returns></returns> public DataRow ExecuteDataTableRow(string SqlString, MySqlParameter[] parms) { if (Conn == null || Conn.State != ConnectionState.Open) ConnTo(); try { MySqlCommand cmd = new MySqlCommand(); cmd.Connection = Conn; cmd.CommandType = CommandType.Text; cmd.CommandText = SqlString; cmd.CommandTimeout = TimeOut; if (parms != null) foreach (MySqlParameter pram in parms) cmd.Parameters.Add(pram); DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader()); ) ]; } catch (Exception e) { AddError(e.Message, SqlString); } return null; } /// <summary> /// 返回第一个值 /// </summary> /// <param name="SqlString"></param> /// <returns></returns> public string ExecuteFirst(string SqlString, MySqlParameter[] parms) { if (Conn == null || Conn.State != ConnectionState.Open) ConnTo(); try { MySqlCommand cmd = new MySqlCommand(); cmd.Connection = Conn; cmd.CommandType = CommandType.Text; cmd.CommandText = SqlString; cmd.CommandTimeout = TimeOut; if (parms != null) foreach (MySqlParameter pram in parms) cmd.Parameters.Add(pram); MySqlDataReader ss = cmd.ExecuteReader(); string xx = ""; if (ss.Read()) xx = ss[].ToString(); ss.Close(); return xx; } catch (Exception e) { AddError(e.Message, SqlString); } return null; } /// <summary> /// 返回第一个值 /// </summary> /// <param name="SqlString"></param> /// <returns></returns> public long ExecuteInsertId(string SqlString, MySqlParameter[] parms) { if (Conn == null || Conn.State != ConnectionState.Open) ConnTo(); try { MySqlCommand cmd = new MySqlCommand(); cmd.Connection = Conn; cmd.CommandType = CommandType.Text; cmd.CommandText = SqlString; cmd.CommandTimeout = TimeOut; if (parms != null) foreach (MySqlParameter pram in parms) cmd.Parameters.Add(pram); cmd.ExecuteNonQuery(); return cmd.LastInsertedId; } catch (Exception e) { AddError(e.Message, SqlString); } ; } /// <summary> /// 执行无返回SQL语句 /// </summary> /// <param name="SqlString">SQL语句</param> /// <param name="parms">Sql参数</param> ///<returns>是否执行成功</returns> public bool ExecuteNonQuery(string SqlString, MySqlParameter[] parms) { if (Conn == null || Conn.State != ConnectionState.Open) ConnTo(); try { MySqlCommand cmd = new MySqlCommand(); cmd.Connection = Conn; cmd.CommandType = CommandType.Text; cmd.CommandText = SqlString; cmd.CommandTimeout = TimeOut; if (parms != null) foreach (MySqlParameter pram in parms) cmd.Parameters.Add(pram); cmd.ExecuteNonQuery(); return true; } catch (Exception e) { AddError(e.Message, SqlString); return false; } } /// <summary> /// 查询是否存在 /// </summary> /// <param name="SqlString">SQL语句</param> /// <param name="parms">SQL参数</param> /// <returns>是否存在</returns> public bool ExecuteExists(string SqlString, MySqlParameter[] parms) { if (Conn == null || Conn.State != ConnectionState.Open) ConnTo(); try { MySqlCommand cmd = new MySqlCommand(); cmd.Connection = Conn; cmd.CommandType = CommandType.Text; cmd.CommandText = SqlString; cmd.CommandTimeout = TimeOut; if (parms != null) foreach (MySqlParameter pram in parms) cmd.Parameters.Add(pram); DataTable dt = new DataTable(); dt.Load(cmd.ExecuteReader()); ) return true; return false; } catch (Exception e) { AddError(e.Message, SqlString); return false; } } /// <summary> /// 连接数据库 /// </summary> private void ConnTo() { Close(); try { Conn = new MySqlConnection(ConnString); Conn.Open(); } catch (Exception e) { ErrorString += "数据库连接错误:" + e.Message + "\r\n连接串:" + ConnString + "\r\n"; ) ErrorString = null; } } /// <summary> /// 错误信息 /// </summary> /// <param name="message"></param> /// <param name="sql"></param> private void AddError(string message, string sql) { ErrorString += "数据库连接错误:" + message + "\r\nSQL语句:" + sql + "\r\n"; ) ErrorString = ""; } /// <summary> /// 关闭数据库链接 /// </summary> public void Close() { try { this.Conn.Close(); Conn = null; } catch { } } } }
//DbHelperMySQL.cs、放在项目中直接使用 using System; using System.Collections; using System.Collections.Specialized; using System.Data; using MySql.Data.MySqlClient; using System.Configuration; using System.Data.Common; using System.Collections.Generic; namespace Maticsoft.DBUtility { /// <summary> /// 数据访问抽象基础类 /// Copyright (C) 2004-2008 By LiTianPing /// </summary> public abstract class DbHelperMySQL { //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库. public DbHelperMySQL() { } #region 公用方法 /// <summary> /// 得到最大值 /// </summary> /// <param name="FieldName"></param> /// <param name="TableName"></param> /// <returns></returns> public static int GetMaxID(string FieldName, string TableName) { string strsql = "select max(" + FieldName + ")+1 from " + TableName; object obj = GetSingle(strsql, CommandInfo.constr_r); if (obj == null) { ; } else { return int.Parse(obj.ToString()); } } /// <summary> /// 是否存在 /// </summary> /// <param name="strSql"></param> /// <returns></returns> public static bool Exists(string strSql) { object obj = GetSingle(strSql, CommandInfo.constr_r); int cmdresult; if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = ; } else { cmdresult = int.Parse(obj.ToString()); } ) { return false; } else { return true; } } /// <summary> /// 是否存在(基于MySqlParameter) /// </summary> /// <param name="strSql"></param> /// <param name="cmdParms"></param> /// <returns></returns> public static bool Exists(string strSql, params MySqlParameter[] cmdParms) { object obj = GetSingle(strSql, CommandInfo.constr_r, cmdParms); int cmdresult; if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { cmdresult = ; } else { cmdresult = int.Parse(obj.ToString()); } ) { return false; } else { return true; } } #endregion #region 执行简单SQL语句 /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString, string connectionString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (MySql.Data.MySqlClient.MySqlException e) { throw e; } finally { connection.Close(); connection.Dispose(); } } } } public static int ExecuteSqlByTime(string SQLString, int Times, string connectionString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; int rows = cmd.ExecuteNonQuery(); return rows; } catch (MySql.Data.MySqlClient.MySqlException e) { throw e; } finally { connection.Close(); connection.Dispose(); } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">多条SQL语句</param> public static int ExecuteSqlTran(List<String> SQLStringList, string connectionString) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { conn.Open(); MySqlCommand cmd = new MySqlCommand(); cmd.Connection = conn; MySqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { ; ; n < SQLStringList.Count; n++) { string strsql = SQLStringList[n]; ) { cmd.CommandText = strsql; count += cmd.ExecuteNonQuery(); } } tx.Commit(); return count; } catch { tx.Rollback(); ; } finally { conn.Close(); conn.Dispose(); } } } /// <summary> /// 执行带一个存储过程参数的的SQL语句。 /// </summary> /// <param name="SQLString">SQL语句</param> /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString, string content, string connectionString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(SQLString, connection); MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@content", SqlDbType.NText); myParameter.Value = content; cmd.Parameters.Add(myParameter); try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (MySql.Data.MySqlClient.MySqlException e) { throw e; } finally { cmd.Dispose(); connection.Close(); connection.Dispose(); } } } /// <summary> /// 执行带一个存储过程参数的的SQL语句。 /// </summary> /// <param name="SQLString">SQL语句</param> /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param> /// <returns>影响的记录数</returns> public static object ExecuteSqlGet(string SQLString, string content, string connectionString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(SQLString, connection); MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@content", SqlDbType.NText); myParameter.Value = content; cmd.Parameters.Add(myParameter); try { connection.Open(); object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (MySql.Data.MySqlClient.MySqlException e) { throw e; } finally { cmd.Dispose(); connection.Close(); connection.Dispose(); } } } /// <summary> /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例) /// </summary> /// <param name="strSQL">SQL语句</param> /// <param name="fs">图像字节,数据库的字段类型为image的情况</param> /// <returns>影响的记录数</returns> public static int ExecuteSqlInsertImg(string strSQL, byte[] fs, string connectionString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(strSQL, connection); MySql.Data.MySqlClient.MySqlParameter myParameter = new MySql.Data.MySqlClient.MySqlParameter("@fs", SqlDbType.Image); myParameter.Value = fs; cmd.Parameters.Add(myParameter); try { connection.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (MySql.Data.MySqlClient.MySqlException e) { throw e; } finally { cmd.Dispose(); connection.Close(); connection.Dispose(); } } } /// <summary> /// 执行一条计算查询结果语句,返回查询结果(object)。 /// </summary> /// <param name="SQLString">计算查询结果语句</param> /// <returns>查询结果(object)</returns> public static object GetSingle(string SQLString, string connectionString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { connection.Open(); object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (MySql.Data.MySqlClient.MySqlException e) { throw e; } finally { connection.Close(); connection.Dispose(); } } } } public static object GetSingle(string SQLString, int Times, string connectionString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand(SQLString, connection)) { try { connection.Open(); cmd.CommandTimeout = Times; object obj = cmd.ExecuteScalar(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (MySql.Data.MySqlClient.MySqlException e) { throw e; } finally { connection.Close(); connection.Dispose(); } } } } /// <summary> /// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close ) /// </summary> /// <param name="strSQL">查询语句</param> /// <returns>MySqlDataReader</returns> public static MySqlDataReader ExecuteReader(string strSQL, string connectionString) { MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand cmd = new MySqlCommand(strSQL, connection); try { connection.Open(); MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); return myReader; } catch (MySql.Data.MySqlClient.MySqlException e) { connection.Close(); connection.Dispose(); throw e; } //finally //{ // connection.Clone(); // connection.Dispose(); //} } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public static DataSet Query(string SQLString, string connectionString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection); command.Fill(ds, "ds"); } catch (MySql.Data.MySqlClient.MySqlException ex) { throw new Exception(ex.Message); } finally { connection.Close(); connection.Dispose(); } return ds; } } public static DataSet Query(string SQLString, int Times, string connectionString) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { DataSet ds = new DataSet(); try { connection.Open(); MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection); command.SelectCommand.CommandTimeout = Times; command.Fill(ds, "ds"); } catch (MySql.Data.MySqlClient.MySqlException ex) { throw new Exception(ex.Message); } finally { connection.Close(); connection.Dispose(); } return ds; } } #endregion #region 执行带参数的SQL语句 /// <summary> /// 执行SQL语句,返回影响的记录数 /// </summary> /// <param name="SQLString">SQL语句</param> /// <returns>影响的记录数</returns> public static int ExecuteSql(string SQLString, string connectionString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); int rows = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return rows; } catch (MySql.Data.MySqlClient.MySqlException e) { throw e; } finally { connection.Close(); connection.Dispose(); } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[])</param> public static void ExecuteSqlTran(Hashtable SQLStringList, string connectionString) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { conn.Open(); using (MySqlTransaction trans = conn.BeginTransaction()) { MySqlCommand cmd = new MySqlCommand(); try { //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = myDE.Key.ToString(); MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Value; PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); } trans.Commit(); } catch { trans.Rollback(); throw; } finally { conn.Close(); conn.Dispose(); } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[])</param> public static int ExecuteSqlTran(System.Collections.Generic.List<CommandInfo> cmdList, string connectionString) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { conn.Open(); using (MySqlTransaction trans = conn.BeginTransaction()) { MySqlCommand cmd = new MySqlCommand(); try { ; //循环 foreach (CommandInfo myDE in cmdList) { string cmdText = myDE.CommandText; MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters; PrepareCommand(cmd, conn, trans, cmdText, cmdParms); if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine) { ) { trans.Rollback(); ; } object obj = cmd.ExecuteScalar(); bool isHave = false; if (obj == null && obj == DBNull.Value) { isHave = false; } isHave = Convert.ToInt32(obj) > ; if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave) { trans.Rollback(); ; } if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave) { trans.Rollback(); ; } continue; } int val = cmd.ExecuteNonQuery(); count += val; ) { trans.Rollback(); ; } cmd.Parameters.Clear(); } trans.Commit(); return count; } catch { trans.Rollback(); throw; } finally { conn.Close(); conn.Dispose(); } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[])</param> public static void ExecuteSqlTranWithIndentity(System.Collections.Generic.List<CommandInfo> SQLStringList, string connectionString) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { conn.Open(); using (MySqlTransaction trans = conn.BeginTransaction()) { MySqlCommand cmd = new MySqlCommand(); try { ; //循环 foreach (CommandInfo myDE in SQLStringList) { string cmdText = myDE.CommandText; MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Parameters; foreach (MySqlParameter q in cmdParms) { if (q.Direction == ParameterDirection.InputOutput) { q.Value = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = cmd.ExecuteNonQuery(); foreach (MySqlParameter q in cmdParms) { if (q.Direction == ParameterDirection.Output) { indentity = Convert.ToInt32(q.Value); } } cmd.Parameters.Clear(); } trans.Commit(); } catch { trans.Rollback(); throw; } finally { conn.Close(); conn.Dispose(); } } } } /// <summary> /// 执行多条SQL语句,实现数据库事务。 /// </summary> /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的MySqlParameter[])</param> public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList, string connectionString) { using (MySqlConnection conn = new MySqlConnection(connectionString)) { conn.Open(); using (MySqlTransaction trans = conn.BeginTransaction()) { MySqlCommand cmd = new MySqlCommand(); try { ; //循环 foreach (DictionaryEntry myDE in SQLStringList) { string cmdText = myDE.Key.ToString(); MySqlParameter[] cmdParms = (MySqlParameter[])myDE.Value; foreach (MySqlParameter q in cmdParms) { if (q.Direction == ParameterDirection.InputOutput) { q.Value = indentity; } } PrepareCommand(cmd, conn, trans, cmdText, cmdParms); int val = cmd.ExecuteNonQuery(); foreach (MySqlParameter q in cmdParms) { if (q.Direction == ParameterDirection.Output) { indentity = Convert.ToInt32(q.Value); } } cmd.Parameters.Clear(); } trans.Commit(); } catch { trans.Rollback(); throw; } finally { conn.Close(); conn.Dispose(); } } } } /// <summary> /// 执行一条计算查询结果语句,返回查询结果(object)。 /// </summary> /// <param name="SQLString">计算查询结果语句</param> /// <returns>查询结果(object)</returns> public static object GetSingle(string SQLString, string connectionString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { using (MySqlCommand cmd = new MySqlCommand()) { try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); object obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (MySql.Data.MySqlClient.MySqlException e) { throw e; } finally { connection.Close(); connection.Dispose(); } } } } /// <summary> /// 执行查询语句,返回MySqlDataReader ( 注意:调用该方法后,一定要对MySqlDataReader进行Close ) /// </summary> /// <param name="strSQL">查询语句</param> /// <returns>MySqlDataReader</returns> public static MySqlDataReader ExecuteReader(string SQLString, string connectionString, params MySqlParameter[] cmdParms) { MySqlConnection connection = new MySqlConnection(connectionString); MySqlCommand cmd = new MySqlCommand(); try { PrepareCommand(cmd, connection, null, SQLString, cmdParms); MySqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return myReader; } catch (MySql.Data.MySqlClient.MySqlException e) { connection.Close(); connection.Dispose(); throw e; } //finally //{ // cmd.Dispose(); // connection.Close(); // connection.Dispose(); //} } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="SQLString">查询语句</param> /// <returns>DataSet</returns> public static DataSet Query(string SQLString, string connectionString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connectionString)) { MySqlCommand cmd = new MySqlCommand(); PrepareCommand(cmd, connection, null, SQLString, cmdParms); using (MySqlDataAdapter da = new MySqlDataAdapter(cmd)) { DataSet ds = new DataSet(); try { da.Fill(ds, "ds"); cmd.Parameters.Clear(); } catch (MySql.Data.MySqlClient.MySqlException ex) { throw new Exception(ex.Message); } finally { connection.Close(); connection.Dispose(); } return ds; } } } private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, 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 = CommandType.Text;//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); } } } #endregion } }
4.原理介绍
string ConnString = "Database=amtiot_bak;Data Source=192.168.17.130;User Id=root;Password=sql5;pooling=false;CharSet=utf8;port=3306"; MySqlConnection Conn = new MySqlConnection(ConnString); //1.连接数据库 Conn.Open(); MySqlCommand mySqlCommand = new MySqlCommand(SqlString, Conn); //2.构建命令 DataTable dataTable =mySqlCommand.ExecuteReader(); //3.执行命令 foreach (DataRow dataRow in dataTablePoint.Rows) //4.遍历结果 Debug.WriteLine(dataRow[]); MySqlCommand mySqlCommand = new MySqlCommand(); //下面语句同 2步骤作用相同 mySqlCommand.Connection = Conn; mySqlCommand.CommandText = SqlString; mySqlCommand.ExecuteReader()
一些很用用的帮助类文件:http://tool.sufeinet.com/CodePreview/CodeView.aspx
C# 与MySQL的更多相关文章
- Hadoop 中利用 mapreduce 读写 mysql 数据
Hadoop 中利用 mapreduce 读写 mysql 数据 有时候我们在项目中会遇到输入结果集很大,但是输出结果很小,比如一些 pv.uv 数据,然后为了实时查询的需求,或者一些 OLAP ...
- mysql每秒最多能插入多少条数据 ? 死磕性能压测
前段时间搞优化,最后瓶颈发现都在数据库单点上. 问DBA,给我的写入答案是在1W(机械硬盘)左右. 联想起前几天infoQ上一篇文章说他们最好的硬件写入速度在2W后也无法提高(SSD硬盘) 但这东西感 ...
- LINUX篇,设置MYSQL远程访问实用版
每次设置root和远程访问都容易出现问题, 总结了个通用方法, 关键在于实用 step1: # mysql -u root mysql mysql> Grant all privileges o ...
- nodejs进阶(6)—连接MySQL数据库
1. 建库连库 连接MySQL数据库需要安装支持 npm install mysql 我们需要提前安装按mysql sever端 建一个数据库mydb1 mysql> CREATE DATABA ...
- MySQL高级知识- MySQL的架构介绍
[TOC] 1.MySQL 简介 概述 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,将数据保存在不同的表中,而 ...
- 闰秒导致MySQL服务器的CPU sys过高
今天,有个哥们碰到一个问题,他有一个从库,只要是启动MySQL,CPU使用率就非常高,其中sys占比也比较高,具体可见下图. 注意:他的生产环境是物理机,单个CPU,4个Core. 于是,他抓取了CP ...
- 我的MYSQL学习心得(一) 简单语法
我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(五) 运 ...
- Entity Framework Core 实现MySQL 的TimeStamp/RowVersion 并发控制
将通用的序列号生成器库 从SQL Server迁移到Mysql 遇到的一个问题,就是TimeStamp/RowVersion并发控制类型在非Microsoft SQL Server数据库中的实现.SQ ...
- Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境
首先为什么要自己编写Dockerfile来构建 nginx.php.mariadb这三个镜像呢?一是希望更深入了解Dockerfile的使用,也就能初步了解docker镜像是如何被构建的:二是希望将来 ...
- 当忘记mysql数据库密码时如何进行修改
因为长时间没有使用数据库了,或者把密码改完之后就忘了数据库密码,不能正常进入数据库,也无法修改密码,有一个简单的常用修改密码方式: 1.首先找到和打开mysql.exe和mysqld.exe所在的文件 ...
随机推荐
- appStore应用发布流程
原文转自: http://blog.sina.com.cn/s/blog_68661bd801019uzd.html 首先确定帐号是否能发布, https://developer.appl ...
- POJ 2135 Farm Tour (最小费用最大流模板)
题目大意: 给你一个n个农场,有m条道路,起点是1号农场,终点是n号农场,现在要求从1走到n,再从n走到1,要求不走重复路径,求最短路径长度. 算法讨论: 最小费用最大流.我们可以这样建模:既然要求不 ...
- Tango_with_django_17笔记
1. 在update Category tabole with SlugField中,起因是url会将空格自动转换成%20,为了把url变得美观,易读,可以用slugify,它可以把空格替换为连字符( ...
- 优先队列(和fence repair完全一样)
懒省事的小明 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 小明很想吃果子,正好果园果子熟了.在果园里,小明已经将所有的果子打了下来,而且按果子的不同种 ...
- Tomcat学习—Tomcat的简介和目录以及配置文件介绍(Windows环境)
tomcat学习(8) 版权声明:本文为博主原创文章,未经博主允许不得转载. 今天学习TOMCAT,主要学习的是Tomcat的目录结构,配置文件! 1:Tomcat简介 Tomcat 服务器是一个免费 ...
- linux搭建svn版本管理器
安装SVN [root@localhost modules]#yum install subversion 验证安装 检验已经安装的SVN版本信息[root@localhost modules]# s ...
- Java 学习 第四篇;面向对象(1)
1:关于继承为了保证父类的良好封装性,不会被子类随意改变,设计父类时通常隐藏父类的内部数据,把父类属性改为private如果父类中可以被重写,但不希望被其他类自由访问可用protected修饰;2:什 ...
- QCoreApplication::processEvents();的作用与TApplication::ProcessMessages的作用完全相同,但是没想到这种用法还有缺点
手动事件处理 最基本的解决方案是明确要求Qt在计算的某些时刻处理等待事件.要做到这一点,必须定期调用QCoreApplication::processEvents(). 下面的例子显示如何做到这一点: ...
- Delphi中动态创建的Panel无法改变颜色的解决办法(要把Panel的ParentBackground设为False)
刚开始代码如下: procedure TForm1.Button1Click(Sender: TObject); var Panel: TPanel; begin Panel := TPanel.Cr ...
- UESTC_In Galgame We Trust CDOJ 10
As we all know, there are many interesting (H) games in kennethsnow’s computer. But he sets a passwo ...