C # to the general class of underlying data operation

zhaozj2021-02-16  101

Using system.data; using system.data.sqlclient; using system.windows.forms;

Namespace Datalibrary {///

/// Data connection setting /// public class sqlconnect {static string SqlConnStr;

///

/// Connection string /// public string connString {set {sqlconnstr = value;} get {return sqlConnStr;}}

///

/// acquisition /// /// public sqlConnection getSqlConnection () {sqlConnection _Connection = null; if (SqlConnstr == Null || sqlconnstr == "") {string SystemDir = System.Environment.SystemDirectory; DisposalConductService DisposalConductService1 = new DisposalConductService (SystemDir "// CBERP_Dis.DLL"); sqlconnstr = "server =" DisposalConductService1.GetValue ( "ServerName", "(local)") "; user id =" DisposalConductService1.GetValue ( "SqlAccount", "sa") "; password =" DisposalConductService1.GetValue ( "passWord", "jiayu520") "; Database =" DisposalConductService1.GetValue ( "Database", "cberp");} try {_connection = new sqlConnection (SqlConnstr); _Connection.Open ();} catch (sqlexception se) {if (se.Number == 17 || se.Number == 18456) {If ("" database access parameter setting is incorrect, is it necessary to set it? "," Prompt ", messageboxbuttons.yesno, messageboxicon.question == DialogResult.yes) {sqlconnstr =" "; Disposalconduct Config = new Disposalconduct (); config.showdialog (); _Connection = getSqlConnection ();} else {messagebox.show ("Building database connection failed, programs can not continue to run!", "Wrong", messageboxbuttons.ok, messageboxicon.stop; application }} Else {messagebox.show (se.Message, "Tips", MessageBoxButtons.ok, MessageBoxicon.information; _Connection.Close ();}}}

///

/// data configuration /// public class datacatena: idisposable {private sqlconnection _connection; /// /// data configuration /// public datacatena () {SqlConnect SqlConnect = new SqlConnect (); _Connection = SqlConnect.getsqlConnection (); _Connection.close ();} /// /// data configuration /// /// Connection string public datacatena (string connStr) {_connection = new SqlConnection (connStr);

///

/// gets dataView /// /// connection string /// data view public dataview GetDataView (String sqlstr) {DataView dv = null; SqlDataAdapter myCommand = new SqlDataAdapter (sqlstr, _connection); DataSet ds = new DataSet (); try {myCommand.Fill (ds, "table"); dv = ds.Tables [ " "]. defaultview;} catch (sqlexception eXC) {debugbox db = new debugbox (SQLSTR " / r / n " exc.Message); db.show ();} Return DV;}

///

/// Clean up resources /// public void dispose () {dispose (true); gc.suppressFinalize (TRUE);}

///

/// Clean all the resources being used. /// /// protected {if (! Disposing) return;

IF (this._connection! = null) {this._Connection.dispose ();}}

///

/// acquired data adapter /// /// SQL statement /// data adapter public sqldataadapter GetSqlda (String Sqlstr) {sqldataadapter mycommand = new sqldataadapter (Sqlstr, _Connection); return mycommand;} /// /// acquisition data table /// SQL statement /// data table public DataTable GetDT (String sqlstr) {SqlDataAdapter myCommand = new SqlDataAdapter (sqlstr, _connection); DataSet ds = new DataSet (); try {myCommand. Fill (DS, "Table");} catch (SQLException EXC) {DebugBox DB = New DebugBox (SQLSTR "/ R / N" EXC.TOSTRING ()); DB.SHOW ();} Return DS.Tables "table"];

///

/// Execute Statement /// /// SQL statement /// public object execwithvalue (String sqlstr) {object result = null; _connection.Open (); SqlCommand myCommand = new SqlCommand (sqlstr, _connection); try {result = myCommand.ExecuteScalar ();} catch (SqlException exc) {DebugBox db = new DebugBox (sqlstr "/ r / n" EXC.TOString ()); db.show (); //messagebox.show(Exc.Message); }_Connection.Close (); Return Result;}

///

/// Returns the corresponding number of rows //// /// SQL statement /// number of rows public int Exec (String sqlstr) {int sucess = 0; SqlCommand myCommand = new SqlCommand (sqlstr, _connection); _connection.Open (); try {myCommand.ExecuteNonQuery ();} catch (SqlException exc) {DebugBox db = new DebugBox (SQLSTR "/ R / N" EXC.TOSTRING ()); db.show (); // messagebox.show (exc.Message); Sucess = Exc.Number; }_Connection.Close (); Return Sucess; }}}} /// /// single table operation /// public class singletable {private datacatena datacatena; private sqldataadapter mydataadapter; private dataset ds; /// /// single table operation / // /// public singleletable (string sqlstr) {datacatena = new datacatena (); mydataadapter = datacatena.getsqlda (sqlstr); DS = new dataset () MyDataAdapter.Fill (DS, "Table1");

///

/// Update a table /// /// public void setupdateCommand (SQLCMMAND) {MyDataAdapter.UpdateCommand = New SQLCommand (SQLSTR , mydataadapter.selectcommand.connection;

///

/// Return data table /// /// public dataable getdataable () {return ds.tables ["Table1"];

///

/// return data adapter /// ///

///

/// update table /// public void update () {MyDataAdapter.Update (DS, "Table1");}} /// /// automatic update /// / public class AutoUpdateObj {private DataCatena DataCatena; private SqlDataAdapter myDataAdapter; private SqlCommandBuilder cb; private DataSet ds; /// /// automatic update /// /// public AutoUpdateObj (string sqlstr) {DataCatena = new DataCatena (); myDataAdapter = DataCatena.GetSqlDA (sqlstr); cb = new SqlCommandBuilder (myDataAdapter); ds = new DataSet (); myDataAdapter.Fill ( DS, "Table1");

///

/// Update SQL statement /// /// public void setupdateCommand (SQLCAPTER.UPDateCommand = new SQLCOMMAND (SQLSTR , mydataadapter.selectcommand.connection;

///

/// Set SELECT statement /// /// public void setselectcommand (SQLCAPTER.SELECTCOMMAND = New SQLCOMMAND (SQLSTR , mydataadapter.selectcommand.connection; cb.dataadapter = myDataAdapter; ds.tables.clear ();}

///

/// acquisition data table /// public datatable getDataTable () {ds.clear (); mydataadapter.fill (DS, "Table1" Return DS.Tables ["Table1"];

///

/// acquisition data adapter /// ///
转载请注明原文地址:https://www.9cbs.com/read-10714.html

New Post(0)