Declares a class, implements the method of adding and removing and checking and modifying in class
Public classadonet {//declaring a connection string Public stringSqlstr ="data source={0};d atabase={1}; User id={2};p wd={3}"; //declaring the ADO class PublicSqlCommand Command; PublicSqlConnection Conn; //declare constructor incoming connection string for stitching PublicADONet (stringDataSourcestringDatabasestringUseridstringpwd) { varCSTR =string. Format (Sqlstr, DataSource, Database, UserID, PWD); Conn=NewSqlConnection (CSTR); } //Increase Public voidAdonetadd (stringcmd) {//Open DatabaseConn.Open (); //use the using using to release resources when the command finishes executing using(SqlCommand sqlcmd =NewSqlCommand (Cmd,conn)) { //gets the SQL statement to executesqlcmd.commandtext=cmd; if(sqlcmd. ExecuteNonQuery () >0) {Console.WriteLine ("Increase Success"); } Else{Console.WriteLine ("Increase Failure"); } } } //Delete Public voidAdodelete (stringcmd) {conn.open (); using(SqlCommand sqlcmd =NewSqlCommand (cmd, Conn)) {Sqlcmd.commandtext=cmd; if(sqlcmd. ExecuteNonQuery () >0) {Console.WriteLine ("Delete succeeded"); } Else{Console.WriteLine ("Delete Failed"); } } } //Enquiry Public voidAdonetstate (stringcmd) {conn.open (); Command=NewSqlCommand (cmd, Conn); //Bulk QuerySqlDataAdapter ad =NewSqlDataAdapter (Command); DataTable DT=NewDataTable (); Ad. Fill (DT); vars1=dt. Select (); varArrlist1 = s1[0]. itemarray[0]; varArrlist2 = s1[0]. itemarray[1]; varArrlist3 = s1[1]. itemarray[0]; varArrlist4 = s1[1]. itemarray[1]; Console.WriteLine (ARRLIST1); Console.WriteLine (ARRLIST2); Console.WriteLine (ARRLIST3); Console.WriteLine (ARRLIST4); //Single query//SqlDataReader dataAdapter = Command.ExecuteReader (); //While (Dataadapter.read ())//{ //Console.WriteLine ("{0}", dataadapter["FieldName"]); //}Console.readkey (); } //Change Public voidAdoedit (stringcmd) {conn.open (); using(SqlCommand sqlcmd=NewSqlCommand (Cmd,conn)) {Sqlcmd.commandtext=cmd; if(sqlcmd. ExecuteNonQuery () >0) {Console.WriteLine ("Modification succeeded"); } Else{Console.WriteLine ("Modification Failed"); } } } }
View Code
Declaring SQL statement calls on the console entry method of adding and deleting changes
Static voidMain (string[] args) { //SQL connection String varADONet =NewADONet (".","Ttest","SA","123456"); ////Invoke Query method //var sqlstr = "Select * from Field"; //adonet.adonetstate (SQLSTR); //Increase//string strSQL = "INSERT into Field (FieldName) VALUES (' hahaha ')"; //Adonet.adonetadd (strSQL); //Delete//string strSQL = "Delete from Field where fieldname= ' hahaha '"; //Adonet.adodelete (strSQL); //Modify stringstrSQL ="Update Field set fieldname= ' Lisa ' where fieldname= ' Meiname '"; Adonet.adoedit (strSQL); }
View Code
Ado. NET tutorial (2) To implement additions and deletions to check and change