c mysql 選擇 C 操作MySQL的類

2021-10-18 21:20:19 字數 3451 閱讀 2195

c#操作mysql的類

public class mysqlservice

private static log4net.ilog logger = log4net.logmanager.getlogger(typeof(mysqlservice));

//server=localhost;user id=root;password=root;persist security info=true;database=test

//data source=127.0.0.1;port=3306;initial catalog=tsyw;user id=root;password=q2ii3sfc;charset=gbk

private string conn_str = "server=localhost;user id=root;password=q2ii3sfc;persist security info=true;database=win008";

public mysqlservice()

/// 建構函式

public mysqlservice(string conn_str)

this.conn_str = conn_str;

/// 執行sql語句返回list-hashtable資料集

public list select(string sql)

list lst = new list();

using (mysqlconnection connection = new mysqlconnection(this.conn_str))

tryconnection.open();

mysqlcommand cmd = new mysqlcommand(sql, connection);

mysqldatareader mdr = cmd.executereader();

int columncount = mdr.fieldcount;

string _key = string.empty;

while (mdr.read())

hashtable ht = new hashtable();

for(int i = 0; i < columncount; i++)

_key = mdr.getname(i);

ht[_key] = mdr[i];

lst.add(ht);

mdr.close();

connection.close();

catch (exception ex)

logger.warn("執行操作語句異常, sql:" + sql, ex);

return lst;

public system.data.dataset query(string sql)

return null;

/// 執行操作語句 delete/insert/update

/// 返回影響行數

public int execute(string sql)

int rest = 0;

using (mysqlconnection connection = new mysqlconnection(this.conn_str))

tryconnection.open();

mysqlcommand cmd = new mysqlcommand(sql, connection);

rest = cmd.executenonquery();

rest = (int)(cmd.lastinsertedid>0?cmd.lastinsertedid:rest);

connection.close();

catch (exception ex)

logger.warn("執行操作語句異常, sql:" + sql, ex);

return rest;

public hashtable fetchrow(string sql)

hashtable ht = new hashtable();

using (mysqlconnection connection = new mysqlconnection(this.conn_str))

tryconnection.open();

mysqlcommand cmd = new mysqlcommand(sql, connection);

mysqldatareader mdr = cmd.executereader();

int columncount = mdr.fieldcount;

string _key = string.empty;

while (mdr.read())

for (int i = 0; i < columncount; i++)

_key = mdr.getname(i);

ht[_key] = mdr[i];

break;

mdr.close();

connection.close();

catch (exception ex)

logger.warn("執行操作語句異常, sql:" + sql, ex);

return ht;

/// 獲取語句第一行第一列資料

public t fetchfirst(string sql) {

t t;

using (mysqlconnection connection = new mysqlconnection(this.conn_str))

tryconnection.open();

mysqlcommand cmd = new mysqlcommand(sql, connection);

t = (t)cmd.executescalar();

connection.close();

catch (exception ex)

t = default(t);

logger.warn("執行操作語句異常, sql:" + sql, ex);

return t;

/// 檢查鏈結是否ok

public bool ping()

bool rest = false;

using (mysqlconnection connection = new mysqlconnection(this.conn_str))

tryconnection.open();

mysqlcommand cmd = new mysqlcommand("select 1", connection);

cmd.executescalar();

connection.close();

rest = true;

catch

rest = false;

return rest;

c#操作mysql的類

[c#cāozuò mysql de lèi]

c# operation mysql class

C mysql資料操作

using system using system.collections.generic using system.linq using system.text using system.data using mysql.data.mysqlclient using mysql.data name...

c mysql 操作 C 操作mysql資料庫

1 usingsystem 2 usingmysql.data.mysqlclient 3 4 namespacemysql資料庫操作5 29 30 31 endregion 33 region 插入 34 正常插入一條資料35 string username lj string password ...

c mysql類使用方法 C 操作MySQL的類

c 操作mysql的類 public class mysqlservice private static log4net.ilog logger log4net.logmanager.getlogger typeof mysqlservice server localhost user id roo...