Reference: http://www.cnblogs.com/liuhaorain/archive/2012/02/06/2340409.html
increase, delete, change:
1 Public voidInsertData (intXinty)2 {3 #regionUsing4 stringConnStr = configurationmanager.connectionstrings["ConnStr"]. ToString ();5 using(SqlConnection conn =NewSqlConnection (connstr))6 {7 stringSqlstr ="INSERT INTO CalculatorService (num1,num2) VALUES (@x,@y)";8 9SqlCommand cmd =NewSqlCommand (SQLSTR, conn);Ten OneSqlparameter[] Paras =Newsqlparameter[]{ A NewSqlParameter ("@x", SqlDbType.Int), - NewSqlParameter ("@y", SqlDbType.Int) - }; theparas[0]. Value =x; -paras[1]. Value =y; - foreach(varIinchparas) - { + cmd. Parameters.Add (i); - } + A Try at { - Conn. Open (); - cmd. ExecuteNonQuery (); - } - Catch(Exception ex) - { in // - } to } + #endregion -}
Check:
1 Publiclist<int> Searchid (intXinty)2 {3 stringConnStr = configurationmanager.connectionstrings["ConnStr"]. ToString ();4 using(SqlConnection conn =NewSqlConnection (connstr))5 {6 stringSqlstr ="SELECT * from CalculatorService where [email protected] and [email protected]";7 8SqlCommand cmd =NewSqlCommand (SQLSTR, conn);9 TenSqlparameter[] Paras =Newsqlparameter[]{ One NewSqlParameter ("@x", SqlDbType.Int), A NewSqlParameter ("@y", SqlDbType.Int) - }; -paras[0]. Value =x; theparas[1]. Value =y; - foreach(varIinchparas) - { - cmd. Parameters.Add (i); + } -list<int> L =Newlist<int>(); + Try A { at Conn. Open (); -SqlDataReader reader =cmd. ExecuteReader (); - - if(Reader! =NULL&&Reader. HasRows) { - while(reader. Read ()) { - for(inti =0; I < reader. FieldCount; i++) { in if(Reader. GetName (i) = ="ID") { - L.add (Convert.ToInt32 (reader). GetValue (i))); to } + } - } the } * Reader. Close (); $ returnl;Panax Notoginseng } - Catch(Exception ex) the { + returnl; A } the } + -}
Ado. NET Basic Example