Replace parameter with value

Source: Internet
Author: User

This method comes to hubbledotnetSource codeMethod

Path: Hubble. Data --> Hubble. sqlclient --> buildsqlwithsqlparameter method in the hubblecommand class

Thank you for choosing eaglet.

View code

  # Region Static Methods Private   Static   String Buildsqlwithsqlparameter ( String  SQL, system. Data. Common. dbparameter [] paras) {system. Data. Common. dbparameter [] arrparas = New System. Data. Common. dbparameter [paras. Length];  For ( Int I = 0 ; I <paras. length; I ++ ) {Arrparas [I] = Paras [I];} array. Sort (arrparas,  New  Dbparameterforsort ());  Object [] Objparas = New   Object  [Arrparas. Length]; For ( Int I = 0 ; I <arrparas. length; I ++ ) {SQL = SQL. Replace (arrparas [I]. parametername, "  {  " + I. tostring () + "  }  "  );  Switch  (Arrparas [I]. dbtype ){ Case  System. Data. dbtype. ansistring:  Case  System. Data. dbtype. ansistringfixedlength:  Case  System. Data. dbtype. String:  If (Arrparas [I]. value = Null  ) {Objparas [I] = Null  ;}  Else {Objparas [I] = Arrparas [I]. value ;}  Break  ;  Case  System. Data. dbtype. stringfixedlength:  Case  System. Data. dbtype. xml: objparas [I] = Arrparas [I]. Value As   String  ;  Break ;  Case  System. Data. dbtype. boolean:  If (Arrparas [I]. value = Null  ) {Objparas [I] = Null  ;}  Else  {Objparas [I] = Arrparas [I]. value. tostring ();} Break  ;  Case  System. Data. dbtype. Date:  If (Arrparas [I]. value = Null  ) {Objparas [I] = Null  ;}  Else  {Objparas [I] = (Datetime) arrparas [I]. Value). tostring ( " Yyyy-mm-dd  "  );}  Break  ;  Case  System. Data. dbtype. datetime:  Case  System. Data. dbtype. datetime2:  If (Arrparas [I]. value = Null  ) {Objparas [I] = Null ;}  Else  {Objparas [I] = Convert. todatetime (arrparas [I]. value ); //  (Datetime) arrparas [I]. Value). tostring ("yyyy-mm-dd hh: mm: SS ");  }  Break  ;  Case  System. Data. dbtype. Time:  If (Arrparas [I]. value =Null  ) {Objparas [I] = Null  ;}  Else  {Objparas [I] = (Datetime) arrparas [I]. Value). tostring ( "  Hh: mm: SS  "  );}  Break  ; Case  System. Data. dbtype. byte:  Case  System. Data. dbtype. uint16:  Case  System. Data. dbtype. uint32:  Case  System. Data. dbtype. uint64:  If (Arrparas [I]. value = Null  ) {Objparas [I] = Null ;}  Else  {Objparas [I] = Ulong  . Parse (arrparas [I]. value. tostring ());}  Break  ;  Case  System. Data. dbtype. Decimal:  Case  System. Data. dbtype. Double:  Case System. Data. dbtype. Single:  If (Arrparas [I]. value = Null  ) {Objparas [I] = Null  ;}  Else  {Objparas [I] = Double  . Parse (arrparas [I]. value. tostring ());}  Break ;  Case  System. Data. dbtype. int16:  Case  System. Data. dbtype. int32:  Case  System. Data. dbtype. int64:  Case  System. Data. dbtype. sbyte:  If (Arrparas [I]. value = Null  ) {Objparas [I] =Null  ;}  Else  {Objparas [I] = Long  . Parse (arrparas [I]. value. tostring ());}  Break  ;  Default  :  Throw   New System. Data. dataexception (String . Format ( "  Invalid parameter datatype: {0}  "  , Arrparas [I]. dbtype ));}}  Return  Buildsql (SQL, objparas );}  Class Dbparameterforsort: icomparer <system. Data. Common. dbparameter> {  # Region Icomparer <dbparameter> Members Public   Int Compare (system. Data. Common. dbparameter X, system. Data. Common. dbparameter y ){  Return  Y. parametername. compareto (X. parametername );}  # Endregion  }  Public   Static   String Buildsql ( String SQL, Object  [] Paras ){  If (Paras =Null  ){  Return  SQL ;}  Else  {  Object [] Parameters = New   Object  [Paras. Length]; paras. copyto (parameters,  0  );  For ( Int I =0 ; I <parameters. length; I ++ ){  Object OBJ = Parameters [I];  If (OBJ = Null  ) {Parameters [I] = "  Null  "  ;}  Else   If (OBJIs   String  ) {Parameters [I] = String . Format ( "  '{0 }'  "  ,((  String ) OBJ). Replace ( "  '  " , "  ''  " ));}  Else   If (OBJ Is   Bool  ) {Parameters [I] = String . Format ( "  '{0 }'  "  , OBJ );}  Else   If (OBJ Is Datetime) {parameters [I] = String . Format ( "  '{0 }'  "  , (Datetime) OBJ). tostring (  "  Yyyy-mm-dd hh: mm: SS  "  ));}  Else   If (OBJ Is   Byte []) {Stringbuilder sb = New  Stringbuilder (); sb. append (  "  0x  "  );  Foreach ( Byte B In ( Byte  []) OBJ) {sb. appendformat (  " {0: X1}  "  , B);} parameters [I] = SB. tostring ();}  Else  {Parameters [I] = OBJ. tostring ();}}  Return   String  . Format (SQL, parameters );}}  # Endregion 

 

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.