View code
1 Using System; 2 Using System. Collections. Generic; 3 Using System. text; 4 Using System. Data; 5 Using System. Data. sqlclient; 6 7 Namespace Goodsdal 8 { 9 /// <Summary> 10 /// Database Operation class 11 /// </Summary> 12 Public Class Sqlhelper 13 { 14 /// <Summary> 15 /// Database Connection Functions 16 /// </Summary> 17 Public Sqlconnection getconnection (Boolean isclose) 18 { 19 String constring = " Data Source =.; initial catalog = goods; user id = sa; Password = sa " ; 20 Sqlconnection con = New Sqlconnection (constring ); 21 If (Isclose) 22 { 23 Con. open (); 24 } 25 Else 26 { 27 Con. Close (); 28 } 29 Return Con; 30 } 31 32 /// <Summary> 33 /// Execute the query statement 34 /// </Summary> 35 /// <Param name = "plain text"> </param> 36 /// <Param name = "partition type"> </param> 37 /// <Param name = "par"> </param> 38 /// <Returns> Value in the first column of the First row </Returns> 39 Public Object Exescalar ( String Plain text, commandtype primitive type, Params Sqlparameter [] Par) 40 { 41 Sqlcommand cmd = New Sqlcommand (plain text, getconnection ( True )); 42 Cmd. commandtype = Struct type; 43 If (Par! = Null ) 44 { 45 Foreach (Sqlparameter P In PAR) 46 { 47 Cmd. Parameters. Add (P ); 48 } 49 } 50 Object Result =Convert. toint32 (CMD. executescalar ()); 51 Getconnection ( False ); 52 Return Result; 53 } 54 55 /// <Summary> 56 /// Execute update statement 57 /// </Summary> 58 /// <Param name = "plain text"> </param> 59 /// <Param name = "partition type"> </param> 60 /// <Param name = "par"> </param> 61 /// <Returns> Number of affected rows </Returns> 62 Public Int Exenonquery ( String Plain text, commandtype primitive type, Params Sqlparameter [] Par) 63 { 64 Sqlcommand cmd = New Sqlcommand (plain text, getconnection ( True )); 65 Cmd. commandtype = Struct type; 66 If (Par! = Null ) 67 { 68 Foreach (Sqlparameter P In PAR) 69 { 70 Cmd. Parameters. Add (P ); 71 } 72 } 73 Int Result = Cmd. executenonquery (); 74 Getconnection ( False ); 75 Return Result; 76 } 77 78 /// <Summary> 79 /// Read data from the database 80 /// </Summary> 81 /// <Param name = "plain text"> </param> 82 /// <Param name = "partition type"> </param> 83 /// <Param name = "par"> </param> 84 /// <Returns> Sqldatareader </Returns> 85 Public Sqldatareader exedatareader ( String Plain text, commandtype primitive type, Params Sqlparameter [] Par) 86 { 87 Sqlcommand cmd = New Sqlcommand (plain text, getconnection ( True )); 88 Cmd. commandtype = Struct type; 89 If (Par! = Null ) 90 { 91 Foreach (Sqlparameter P In PAR) 92 { 93 Cmd. Parameters. Add (P ); 94 } 95 } 96 Sqldatareader datareader = Cmd. executereader (); 97 Getconnection (False ); 98 Return Datareader; 99 } 100 101 /// <Summary> 102 /// Obtain a database table set 103 /// </Summary> 104 /// <Param name = "plain text"> </param> 105 /// <Param name = "partition type"> </param> 106 /// <Param name = "par"> </param> 107 /// <Returns> Dataset set </Returns> 108 Public Dataset exegetdataset ( String Plain text, commandtype primitive type, Params Sqlparameter [] Par) 109 { 110 Dataset DS = New Dataset (); 111 Sqlcommand cmd = New Sqlcommand (plain text, getconnection ( True )); 112 Cmd. commandtype = Struct type; 113 If (Par! = Null ) 114 { 115 Foreach (Sqlparameter P In PAR) 116 { 117 Cmd. Parameters. Add (P ); 118 } 119 } 120 Sqldataadapter dataadapter = New Sqldataadapter (CMD ); 121 Dataadapter. Fill (DS ); 122 Return DS; 123 } 124 } 125 }