1 /// <summary>2 ///database Access Help class3 /// </summary>4 Public Static classDBHelper5 {6 Private Const stringConnStr ="server=127.0.0.1;database=db_test; Uid=user1; pwd=ps123321;";7 8 9 /// <summary>Ten ///implementation of the increase, deletion, change One /// </summary> A /// <param name= "SQL" ></param> - /// <param name= "Sqlparams" ></param> - /// <returns></returns> the Public Static intExecuteNonQuery (stringSqlparamssqlparameter[] sqlparams) - { - using(SqlConnection conn =NewSqlConnection (connstr)) - { + Conn. Open (); - using(SqlCommand comm =Conn. CreateCommand ()) + { AComm.commandtext =SQL; atComm.commandtype =CommandType.Text; - if(Sqlparams! =NULL) - Comm. Parameters.addrange (sqlparams); - returnComm. ExecuteNonQuery (); - } - } in } - to /// <summary> + ///1 result (s) returned - /// </summary> the /// <param name= "SQL" ></param> * /// <param name= "Sqlparams" ></param> $ /// <returns></returns>Panax Notoginseng Public Static ObjectExecuteScalar (stringSqlparamssqlparameter[] sqlparams) - { the using(SqlConnection conn =NewSqlConnection (connstr)) + { A Conn. Open (); the using(SqlCommand comm =Conn. CreateCommand ()) + { -Comm.commandtext =SQL; $Comm.commandtype =CommandType.Text; $ if(Sqlparams! =NULL) - Comm. Parameters.addrange (sqlparams); - returnComm. ExecuteScalar (); the } - }Wuyi } the - /// <summary> Wu ///return query, return result set - /// </summary> About /// <param name= "SQL" ></param> $ /// <param name= "Sqlparams" ></param> - /// <returns></returns> - Public StaticDataTable ExecuteQuery (stringSqlparamssqlparameter[] sqlparams) - { A using(SqlConnection conn =NewSqlConnection (connstr)) + { the Conn. Open (); - using(SqlCommand comm =Conn. CreateCommand ()) $ { theComm.commandtext =SQL; theComm.commandtype =CommandType.Text; the if(sqlparams!=NULL) the Comm. Parameters.addrange (sqlparams); - using(SqlDataAdapter ADP =NewSqlDataAdapter (Comm)) in { theDataSet ds =NewDataSet (); the ADP. Fill (DS); About returnDs. tables[0]; the } the } the } + } -}
SQL Server database Access Help class