Code//Execute SQL statement Public Static voidExecsqlcommand () {DBHelper db=NewDBHelper (); DbCommand Command= db. Getsqlstringcommond ("SELECT * from T1_insert"); Db. ExecuteNonQuery (command); } //Executing stored procedures Public Static voidexecstoredprocedure () {DBHelper db=NewDBHelper (); DbCommand Command= db. Getstoredproccommond ("T1_insert"); Db. Addinparameter (Command,"@id", Dbtype.string,"Heihei"); Db. Addinparameter (Command,"@id", Dbtype.string,"Heihei"); Db. ExecuteNonQuery (command); } //Back to DataTable Public Static voidgetdatatable () {DBHelper db=NewDBHelper (); DbCommand Command= db. Getsqlstringcommond ("T1_findall"); DataTable DT=db. executedatatable (command); } //get various return values Public Static voidGetallparameter () {DBHelper db=NewDBHelper (); DbCommand Command= db. Getstoredproccommond ("T2_insert"); Db. Addinparameter (Command,"@timeticks", Dbtype.int64, DateTime.Now.Ticks); Db. Addoutparameter (Command,"@outString", Dbtype.string, -); Db. Addreturnparameter (Command,"@returnValue", Dbtype.int32); Db. ExecuteNonQuery (command); strings = db. GetParameter (Command,"@outString"). Value as string;//Out parameter intR = Convert.ToInt32 (db). GetParameter (Command,"@returnValue"). Value);//return value } //Datereader Operation Public Static voidGetdatareader () {DBHelper db=NewDBHelper (); DbCommand Command= db. Getstoredproccommond ("T2_insert"); Db. Addinparameter (Command,"@timeticks", Dbtype.int64, DateTime.Now.Ticks); Db. Addoutparameter (Command,"@outString", Dbtype.string, -); Db. Addreturnparameter (Command,"@returnValue", Dbtype.int32); using(DbDataReader reader =db. ExecuteReader (command)) {//dt. Load (reader); } strings = db. GetParameter (Command,"@outString"). Value as string;//Out parameter intR = Convert.ToInt32 (db). GetParameter (Command,"@returnValue"). Value);//return value } //Get DataSet Public Static voidGetDataSet () {DBHelper db=NewDBHelper (); DbCommand Command= db. Getsqlstringcommond ("SELECT * from T1"); DataSet DS=db. ExecuteDataset (command); } //the use of transactions. (Code-level transactions are essential when you need to combine basic database operations into a complete business flow in your project.) Public voiddobusiness () {using(Trans t =NewTrans ()) { Try{D1 (t); Throw NewException ();//If there is an exception, the drop will be rolled backD2 (t); T.commit (); } Catch{t.rollback (); } } } Public voidD1 (Trans t) {DBHelper db=NewDBHelper (); DbCommand cmd= db. Getstoredproccommond ("T2_insert"); Db. Addinparameter (cmd,"@timeticks", Dbtype.int64, DateTime.Now.Ticks); Db. Addoutparameter (cmd,"@outString", Dbtype.string, -); Db. Addreturnparameter (cmd,"@returnValue", Dbtype.int32); if(T = =NULL) db. ExecuteNonQuery (CMD); Elsedb. ExecuteNonQuery (cmd,t); strings = db. GetParameter (CMD,"@outString"). Value as string;//Out parameter intR = Convert.ToInt32 (db). GetParameter (CMD,"@returnValue"). Value);//return value } Public voidD2 (Trans t) {DBHelper db=NewDBHelper (); DbCommand cmd= db. Getsqlstringcommond ("insert T1 (ID) VALUES (' ... ')"); if(T = =NULL) db. ExecuteNonQuery (CMD); Elsedb. ExecuteNonQuery (cmd, t); }
Where DbCommand is located in the using System.Data.Common namespace
DBHelper database Common class usage method