Ado. NET tutorial (2) To implement additions and deletions to check and change

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.