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

2021-10-17 15:10:20 字數 3451 閱讀 7885

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 選擇 C 操作MySQL的類

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

FindTask類使用方法

findtask類特點 支援多圖層查詢 支援字段查詢 只能進行屬性查詢,不能進行空間查詢 不能指定返回字段,只能返回 displayfieldname string,feature graphic,foundfieldname string,layerid number,layername stri...

PDOMySQLDriver類使用方法

這裡向大家介紹一下這位自我說明 早在2015年我就開始寫這個php類庫了,平時在工作中,將一些好的想法,一些問題的解決方法等融合進來,歷時兩年多,經過不斷的實踐,我感覺它已經很成熟了,於是它來到了你面前!查詢資料 查詢id為5的那行資料的年齡資訊 age pdo mapfield t person ...