資料庫操作類實現 C ,SqlClient

2021-03-31 08:56:57 字數 3631 閱讀 1680

使用ado.***時,每次資料庫操作都要設定connection屬性、建立connection、使用***mand、事務處理等,比較繁瑣,有很多重複工作。能不能把這些繁瑣的、常用的操作再封裝一下,以更方便、安全地使用。下面這個類就是一種嘗試:

using system;

using system.data.sqlclient;

using system.text;

using system.data;

using system.collections;

using system.configuration;

public class dbaccess

;///

/// internal member for tracking ***mand progress

///

private ***mand ***mand;

///

/// string to hold error messages if a ***mand fails

///

private string error;

///

/// get a stored error message if execute***mand fails

///

public string errormessage}

///

/// bool holder for is open

///

private bool bopen;

///

/// check to see if a data base is open

///

public bool isopen}

///

/// declare a string object for the insert ***mand

///

public string insert***mand 

set }

///

/// declare a string object for the delete ***mand

///

public string delete***mand

set}

///

/// declare a string object for the update ***mand

///

public string update***mand

set}

///

/// declare a string object for the select ***mand

///

public string select***mand

set}

public string selectdataset***mand

set}

///

/// get the reader from the class

///

public sqldatareader getreader}}

public dataset getdataset}}

public dataset selectdataset()

catch (exception exp) }

///

/// execute the ***mand that has been set up previously

///

/// a boolean value indicating true or false

public bool execute***mand()

else if( ***mand == ***mand.select )

breturn = true;

///   return breturn;

}catch( sqlexception exp ) }

else if( ***mand == ***mand.dataset )

else

/// get the transaction object from the connection

sqltransaction trans = dbconnection.begintransaction();

try

/// execute the ***mand

switch( ***mand )

error = builderror.tostring();

error = builderror.tostring();

return breturn = false;

}finally

//trans.***mit();

breturn = true;

}else /// if something went wrong rollback

//trans.rollback();

breturn = false;}}

}

return breturn;}

#region select functions

///

/// get the select reader from the select ***mand

///

private sqldatareader selectreader}

dbdatareader = dbselect***mand.executereader();

return dbdatareader;}}

///

/// get the update reader from the update ***mand

///

private sqldatareader updatereader}

///

/// get the insert reader from the insert ***mand

///

private sqldatareader insertreader}

///

/// get the delete reader from the delete ***mand

///

private sqldatareader deletereader}

dbdatareader = dbselect***mand.executereader();

return dbdatareader;}}

#endregion

///

/// standard constructor

///

public dbaccess()

public void open()

catch (exception exp)

}

///

/// close the currently open connection

///

public void close()}

dbconnection.close();}

}

使用示例:

insert操作,新建使用者:

}

update操作,修改使用者資訊:

}

}

資料庫操作類實現 C ,SqlClient

使用ado.時,每次資料庫操作都要設定connection屬性 建立connection 使用 mand 事務處理等,比較繁瑣,有很多重複工作。能不能把這些繁瑣的 常用的操作再封裝一下,以更方便 安全地使用。下面這個類就是一種嘗試 using system using system.data.sql...

資料庫操作類

資料庫配置 return array db config array 資料庫配置 db host 127.0.0.1 伺服器地址 db name tmp 資料庫名 db user root 使用者名稱 db pwd 密碼 db encode utf8 編碼 db prefix dmtx 資料庫表字首...

資料庫操作類

using system using system.web using system.web.ui using system.web.ui.webcontrols using system.web.ui.htmlcontrols using system.data using system.data...

資料庫操作類

剛摘下來的資料庫操作類,以後再慢慢更新 using system using system.collections.generic using system.linq using system.text using system.data using system.data.sqlclient us...

php 資料庫操作類

class dbconnect if dbtable this dbchange dbtable 選擇要操作的資料庫 function dbchange dbtable type 0 返回執行完sql語句後的結果 不做處理 type 1 返回 看下面註釋 type 2 返回 看下面註釋 functi...