Asp.net anti-Injection Attack

Source: Internet
Author: User
Tags oracleconnection ways to prevent sql injection

There are many ways to prevent SQL injection attacks on the Internet. I used to pass the value in the form of parameters. RelatedCodeAs follows:

 


/// <Summary>
/// Execution Parameters
/// </Summary>
/// <Param name = "assql"> SQL statement </Param>
/// <Param name = "aspllist"> Parameter List </Param>
Public   Static   Void Executedmlparametersql ( String Assql, sttsqlparameterlist [] aspllist)
{
Sconnectionstring = Configurationmanager. configurettings. Get ( " Userconnection " );
Oracleconnection occonnection =   New Oracleconnection (sconnectionstring );
Occonnection. open ();
Oraclecommand occommand = Occonnection. createcommand ();
Occommand. commandtype = Commandtype. text;
Occommand. commandtext = Assql;
For ( Int I =   0 ; I < Aspllist. length; I ++ )
{
Oracleparameter opname =   New Oracleparameter ();
Opname. parametername = Aspllist [I]. Name;
Switch (Aspllist [I]. type)
{
Case   " String " :
Opname. oracletype = Oracletype. varchar;
Break ;
Default :
Opname. oracletype = Oracletype. varchar;
Break ;
}
Opname. Value = Aspllist [I]. value;
Occommand. Parameters. Add (opname );
}
Int Iresult = ( Int ) Occommand. executenonquery ();
Occommand. Dispose ();
Occonnection. Close ();
Occonnection. Dispose ();
}

///   <Summary>
/// Parameter structure, used to execute parameter statements
///   </Summary>
Public   Struct Sttsqlparameterlist
{
Private   String Sname;
Private   String Stype;
Private   String Svalue;

///   <Summary>
/// Constructor (initialization value)
///   </Summary>
///   <Param name = "asname">Parameter Name</Param>
///   <Param name = "astype">Parameter type (currently only for string type values)</Param>
///   <Param name = "asvalue">Parameter Value</Param>
Public Sttsqlparameterlist ( String Asname, String Astype, String Asvalue)
{
Sname = Asname;
Stype = Astype;
Svalue = Asvalue;
}

///   <Summary>
/// Parameter Name (same as the parameter name in the SQL statement, but not the: Number)
///   </Summary>
Public   String Name
{
Get
{
Return Sname;
}
Set
{
Sname = Value;
}
}

///   <Summary>
/// Parameter type (this parameter currently only accepts string type)
///   </Summary>
Public   String Type
{
Get
{
Return Stype;
}
Set
{
Stype = Value;
}
}

///   <Summary>
/// Parameter value, which corresponds to the parameter type
///   </Summary>
Public   String Value
{
Get
{
Return Svalue;
}
Set
{
Svalue = Value;
}
}
}

The call method is as follows:

  String Ssql =   " Update hp_requirement "
+   " Set rqtguage = 16, rqtupdatedate = to_date ('{1}', 'yyyy. Mm. dd '), "
+   " Rqtcheckerid = {2 }, "
+   " Rqtcheckerempid = '{3 }', "
+   " Rqtdevelopexplain =: checkerexplain, "   // Checkerexplain is the parameter name, which corresponds to the name in the parameter list
+   " Rqtanalysthour = {4 }, "
+   " Rqtdevelopworkload = {5 }, "
+   " Rqttesthour = {6 }, "
+   " Rqttoalhour = {7} "
+   " Where rqtid = '{0 }' " ;
Ssql =   String . Format (ssql, smainid, supdatedate, scheckerid, scheckerempid, sanalysthour, sworkload, stesthour, stotalhour );

// Assign Parameters (There is only one parameter here)
Sttsqlparameterlist [] sptlist =   New Sttsqlparameterlist [ 1 ];
Sptlist [ 0 ]. Name =   " Checkerexplain " ;
Sptlist [ 0 ]. Type =   " String " ;
Sptlist [ 0 ]. Value = Scheckerexplain;
Clshpdbmanage. executedmlparametersql (ssql, sptlist );

 

Related Article

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.