實用的資料庫幫助類MySQLHelper

2021-09-24 14:13:00 字數 3243 閱讀 7247

程式中我們可能經常會在某個語句或者某方法後去修改資料庫的內容(增刪查改的一系列操作),但如果我們直接在程式中寫這部分的**會使得我們的**量很大,並且我們可能經常做這種重複的操作,所有我們就把我們會用到的資料庫操作封裝起來放到乙個類中,用的時候直接呼叫,方便編碼和提高**的可閱讀性,這樣我們就需要乙個資料庫幫助類。

class mysqlhelper

catch (exception e)}}

}/// /// 查詢語句,返回查詢到的int型數值

///

/// 查詢語句

/// 命令字串型別

/// sql語句對應引數

/// 查詢到的值

public static int getcount(string cmdtext, commandtype cmdtype = commandtype.text, params mysqlparameter commandparameters)

}catch (exception e)}}

}/// /// 執行查詢語句返回list

///

///

/// 查詢語句

/// 命令字串型別

/// sql語句對應引數

/// dataset

public static ilistgetlist(string cmdtext, commandtype cmdtype = commandtype.text, params mysqlparameter commandparameters)

}catch (exception e)}}

}/// /// 用指定的資料庫連線字串執行乙個命令並返回乙個資料集的第一列

///

/// ///例如: object obj = executescalar(connstring, commandtype.storedprocedure, "publishorders", new mysqlparameter("@prodid", 24));

///

/// 儲存過程名稱或者sql命令語句

/// 命令型別(儲存過程, 文字, 等等)

/// 執行命令所用引數的集合

/// 用 convert.to把型別轉換為想要的

public static object executescalar(string cmdtext, commandtype cmdtype, params mysqlparameter commandparameters)

catch (exception e)}}

}/// /// 執行多條sql語句,實現資料庫事務。

///

/// 多條sql語句

/// 對應sql語句列表中每一條語句的引數集合列表

/// 命令型別(儲存過程, 文字, 等等)

public static void executebatchsql(ilistsqlstringlist, ilistcommandparameterslist, commandtype cmdtype = commandtype.text)

//建立mysqlcommand執行命令語句物件

using (mysqlcommand cmd = new mysqlcommand())

//一次性提交事務

tx.commit();

}catch (system.data.sqlclient.sqlexception e)}}

}/// /// 返回dataset

///

/// 儲存過程名稱或者sql命令語句

/// 命令型別(儲存過程, 文字, 等等)

/// 執行命令所用引數的集合

/// 結果資料集

public static dataset getdataset(string cmdtext, commandtype cmdtype = commandtype.text, params mysqlparameter commandparameters)

catch (exception e)

return null;

}/// /// 用執行的資料庫連線執行乙個返回資料集的sql命令

///

/// /// 舉例:

/// mysqldatareader r = executereader(connstring, commandtype.storedprocedure, "publishorders", new mysqlparameter("@prodid", 24));

///

/// 儲存過程名稱或者sql命令語句

/// 命令型別(儲存過程, 文字, 等等)

/// 執行命令所用引數的集合

/// 包含結果的讀取器

public static mysqldatareader executereader(string cmdtext, commandtype cmdtype = commandtype.text, params mysqlparameter commandparameters)

catch (exception e)

return null;

}/// /// 執行命令前預處理

///

/// mysqlcommand執行命令語句物件

/// 資料庫連線物件

/// 事務

/// 命令型別例如 儲存過程或者文字

/// 命令文字,例如:select * from products

/// 執行命令的引數

private static void preparecommand(mysqlcommand cmd, mysqlconnection con, mysqltransaction trans, commandtype cmdtype, string cmdtext, mysqlparameter cmdprams)}}

/// /// dataset資料轉為ilist集合

///

/// 轉換型別

/// 資料來源

/// 需要轉換表的索引

///

public static ilistdatasettolist(dataset dataset, int tableindex)

else

break;}}

}list.add(_t);

}return list;}}

資料庫幫助類

新建乙個類庫,專門來處理資料。我覺得這個和ef的思想是差不多的,等有時間我好好研究一下ef,以後就不再用自己寫sql了 public class sqlhelper string sql select from table sqlcommand com new sqlcommand sql,conn...

Sql Server資料庫幫助類

using system using system.collections.generic using system.linq using system.web using system.data.sqlclient using system.data using system.configurat...

Sql Server 資料庫幫助類

注 資料庫操作幫助封裝類,簡單易用,減少 冗餘 資料庫鏈結 也可配置在web.config 檔案中 public static readonly string connectionstring server 資料庫鏈結 uid 賬號 pwd 密碼 database 資料庫名 執行乙個不需要返回值的s...