c#操作MySql.Data.DLL
2013-03-08 15:49
309 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using MySql.Data.MySqlClient; using System.IO; namespace comback { class ServerDataBase { MySqlConnection conn; string connString = "Persist Security Info=False;database=tldb;server=127.0.0.1;user id=root;pwd=123456"; private void OpenConn() { try { conn = new MySqlConnection(); conn.ConnectionString = connString; conn.Open(); } catch (Exception ee) { System.Windows.Forms.MessageBox.Show("打开数据库异常:" + ee.Message); } } /// <summary> /// 修改功能 /// </summary> /// <param name="sql">修改语句</param> /// <returns></returns> public int ExcuteUser(string sql) { MySqlCommand cmd = null; MySqlTransaction trans = null; int count = 0; try { if (conn == null || conn.State == ConnectionState.Closed) { OpenConn(); } cmd = conn.CreateCommand(); cmd.CommandText = sql; //创建事务 trans = conn.BeginTransaction(); count = cmd.ExecuteNonQuery(); //事务提交 trans.Commit(); } catch { //事务回滚 trans.Rollback(); } finally { if (conn != null && (conn.State != ConnectionState.Closed)) { cmd.Dispose(); conn.Close(); conn.Dispose(); } } return count; } //检验登录用户 public string CheckUser(string username,string password) { string role = ""; MySqlCommand cmd = null; MySqlDataReader reader = null; try { if (conn == null || conn.State == ConnectionState.Closed) { OpenConn(); } cmd = conn.CreateCommand(); cmd.CommandText = "select role from tluser where username='" + username + "' and password='" + password + "'"; reader = cmd.ExecuteReader(); if (reader.HasRows) { if (reader.Read()) { role = reader.GetString("role"); } } else { role = ""; } } catch (Exception ee) { System.Windows.Forms.MessageBox.Show("服务器检查登录的用户合法性时异常:" + ee.Message); } finally { if (conn != null && (conn.State != ConnectionState.Closed)) { if (reader.IsClosed) { reader.Close(); reader.Dispose(); } cmd.Dispose(); conn.Close(); conn.Dispose(); } } return role; } } }
/// <summary> /// 获得数据集DataSet /// </summary> /// <param name="ds"></param> /// <param name="sql"></param> /// <returns></returns> public DataSet getDS(DataSet ds, string sql) { //打开连接 getConnection(); try { //查询结果 MySqlDataAdapter mda = new MySqlDataAdapter(sql, conn); mda.Fill(ds, "data"); } catch (System.Exception e) { MessageBox.Show("getDS()异常:" + e.Message); } finally { conn.Close(); conn.Dispose(); } return ds; }
相关文章推荐
- C#利用Mysql.Data.dll操作Mysql数据库
- c#操作MySql.Data.DLL 分类: .NET 2013-03-08 15:49 4177人阅读 评论(2) 收藏
- C#调用MySQL数据库(使用MySql.Data.dll连接)mysql-connector-net-6.10.4.msi
- c#对数据库的操作代码(MySql.Data.dll包)
- c#连接mysql中文乱码解决方案(MySql.Data.dll)
- c#连接mysql中文乱码解决方案(MySql.Data.dll)
- C#添加 mysql.data.dll
- C#调用MySQL数据库(使用MySql.Data.dll连接)
- c#连接mysql中文乱码解决方案(MySql.Data.dll)
- c# asp.net MySql.Data.DLL6.2下载,.net连接mysql数据库的第三方插件
- Asp.net通过MySql.Data.dll操作mysql数据库(二)
- .NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper
- C# 使用mysql.data.dll,读写mysql数据库
- .NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper
- C#实现操作MySql数据层类MysqlHelper实例
- C# 与 MySql 的操作介绍 下载 远程访问
- C#中Mysql操作的异常: MySQL Connector/Net does not currently support distributed transa
- C#:操作 Excel 异常:Old format or invalid type library. (Exception from HRESULT: 0x80028018 (TYPE_E_INVDATAREAD))
- C#操作IIS站点 Microsoft.Web.Administration.dll
- C#中用MySql.Data.MySqlClient连接MySql