A more convenient and convenient external database operation method!

Source: Internet
Author: User
View code

 1   Using  System;  2   Using  System. Data;  3   Using  System. Data. sqlclient;  4   Public   Class  Helper  5   { 6       Public   Static   Void  Main ()  7   {  8           //  Connection string  9           String Strcon = "  Server =.; database = blog; uid = sa; Pwd = 1  "  ;  10 Sqlhelper helper = New  Sqlhelper (strcon );  11           //  The employee table contains three fields: Neme (varchar (20), age (INT), and sex (BIT;  12           String SQL = "  Select * from employee  "  ;  13 Sqldatareader reader = helper. Reader (SQL, Null  ); 14           Using  (Reader)  15   {  16               While  (Reader. Read ())  17   {  18 Console. writeline (Reader [ "  Name  "  ]. Tostring ());  19  }  20   }  21           /*  String SQL = "insert into employee values (@ name, @ age, @ sex )";  22   Sqlparameter [] PS = new sqlparameter []  23   {  24   New sqlparameter ("@ name", sqldbtype. varchar, 20 ),  25   New sqlparameter ("@ age", sqldbtype. INT ), 26   New sqlparameter ("@ sex", sqldbtype. INT)  27   };  28               29   PS [0]. value = "Zhang Yu ";  30   PS [1]. value = 40;  31   PS [2]. value = 1;  32   Helper. executenonquery (SQL, PS );  */  33  }  34   }  35   Public   Class  Sqlhelper  36   {  37       Private Sqlconnection con = Null  ;  38       Private Sqlcommand cmd = Null ;  39       Public Sqlhelper ( String  Strcon)  40   {  41 Con = New  Sqlconnection (strcon );  42 Cmd = New  Sqlcommand ();  43 Cmd. Connection = Con; 44   }  45       //  Execute initialization command object  46       Private   Void Preparedcommand ( String SQL, Params  Sqlparameter [] PARAM)  47   {  48 Cmd. commandtext = SQL; 49           //  Clear the objects in parameteras  50   Cmd. Parameters. Clear ();  51           If (Param! = Null  )  52   {  53               Foreach (Sqlparameter P In  Param)  54  {  55   Cmd. Parameters. Add (P );  56   }  57   }  58   Con. open ();  59   }  60       /*  Execute non-query statements  */  61       // Parameters available  62       Public   Int Executenonquery ( String SQL, Params  Sqlparameter [] PARAM)  63   {  64   Preparedcommand (SQL, Param );  65           Int I = Cmd. executenonquery ();  66  Close ();  67           Return  I;  68   }  69       //  No Parameter  70       Public   Int Executenonquery ( String  SQL)  71   {  72 Preparedcommand (SQL, Null  );  73           Int I = Cmd. executenonquery ();  74   Close ();  75           Return  I;  76   }  77       /*  Execute the query statement  */ 78       //  Parameters available  79       Public Sqldatareader reader ( String SQL, Params  Sqlparameter [] PARAM)  80   {  81   Preparedcommand (SQL, Param );  82           Return  Cmd. executereader (); 83   }  84       //  No Parameter  85       Public Sqldatareader reader ( String  SQL)  86   {  87 Preparedcommand (SQL, Null  );  88           Return Cmd. executereader ();  89           90   }  91   92       Public   Void  Open ()  93   {  94   Con. open ();  95   }  96       Public  Void  Close ()  97   {  98   Cmd. Dispose ();  99   Con. Close ();  100   }  101   102 }

ThisProgramEncapsulate the methods used to operate external databases. It is a general version of external database operations! Can be referenced anytime, anywhere ...... Simple and Convenient database addition, deletion, modification, and query!

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.