Using system.data; using system.data.sqlclient; using system.windows.forms;
Namespace Datalibrary {/// /// Data connection setting /// summary> public class sqlconnect {static string SqlConnStr;
/// /// Connection string /// summary> public string connString {set {sqlconnstr = value;} get {return sqlConnStr;}}
/// /// acquisition /// summary> /// returns> 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 /// summary> public class datacatena: idisposable {private sqlconnection _connection; /// /// data configuration /// summary> public datacatena () {SqlConnect SqlConnect = new SqlConnect (); _Connection = SqlConnect.getsqlConnection (); _Connection.close ();} /// /// data configuration /// summary> /// Connection string param> public datacatena (string connStr) {_connection = new SqlConnection (connStr);
/// /// gets dataView /// summary> /// connection string param> /// data view returns> 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 /// summary> public void dispose () {dispose (true); gc.suppressFinalize (TRUE);}
/// /// Clean all the resources being used. /// summary> /// param> protected {if (! Disposing) return;
IF (this._connection! = null) {this._Connection.dispose ();}}
/// /// acquired data adapter /// summary> /// SQL statement param> /// data adapter returns> public sqldataadapter GetSqlda (String Sqlstr) {sqldataadapter mycommand = new sqldataadapter (Sqlstr, _Connection); return mycommand;} /// /// acquisition data table /// //// SQL statement param> /// data table returns> 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 /// Summary> /// SQL statement param> /// returns> 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 //// summary> /// SQL statement param> /// number of rows returns> 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 /// summary> public class singletable {private datacatena datacatena; private sqldataadapter mydataadapter; private dataset ds; /// /// single table operation / // summary> /// param> public singleletable (string sqlstr) {datacatena = new datacatena (); mydataadapter = datacatena.getsqlda (sqlstr); DS = new dataset () MyDataAdapter.Fill (DS, "Table1");
/// /// Update a table /// summary> /// param> public void setupdateCommand (SQLCMMAND) {MyDataAdapter.UpdateCommand = New SQLCommand (SQLSTR , mydataadapter.selectcommand.connection;
/// /// Return data table /// summary> /// returns> public dataable getdataable () {return ds.tables ["Table1"];
/// /// return data adapter /// summary> /// returnapter getdataadapter () {return mydataadapter;}
/// /// update table /// summary> public void update () {MyDataAdapter.Update (DS, "Table1");}} /// /// automatic update /// / summary> public class AutoUpdateObj {private DataCatena DataCatena; private SqlDataAdapter myDataAdapter; private SqlCommandBuilder cb; private DataSet ds; /// /// automatic update /// summary> /// param> 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 /// summary> /// param> public void setupdateCommand (SQLCAPTER.UPDateCommand = new SQLCOMMAND (SQLSTR , mydataadapter.selectcommand.connection;
/// /// Set SELECT statement /// summary> /// param> public void setselectcommand (SQLCAPTER.SELECTCOMMAND = New SQLCOMMAND (SQLSTR , mydataadapter.selectcommand.connection; cb.dataadapter = myDataAdapter; ds.tables.clear ();}
/// /// acquisition data table /// ////// returns> public datatable getDataTable () {ds.clear (); mydataadapter.fill (DS, "Table1" Return DS.Tables ["Table1"];
/// /// acquisition data adapter /// summary> /// returnapter getDataadapter () {return mydataadapter;}