Database SqlParameter insertion to prevent SQL Injection implementation code

Source: Internet
Author: User

Example: When you click the Button1 button, the data is inserted into the database.
Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Text;
Using System. Data. SqlClient;
Using System. Data;
Using System. Configuration;

Namespace ParaMeter
{
Public partial class Test: System. Web. UI. Page
{
Private string connectionStr; // The string connecting to the database
Private SqlConnection conDB; // Database Link
Private SqlTransaction _ trans; // transaction object

Protected void Page_Load (object sender, EventArgs e)
{
// ConnectionStr = ConfigurationSettings. deleettings ["constr"];
ConnectionStr = "server = 10.11.43.189 \ SQL2008; database = OA_WEB_DB; uid = sa; pwd = 123456 ";
ConDB = new SqlConnection (connectionStr );
}

Protected void button#click (object sender, EventArgs e)
{
StringBuilder strSql = new StringBuilder ();
StrSql. Append ("insert into [OA_WEB_DB]. [dbo]. [OA_RT_FileType] ([FileTypeName], [Deleted])");
StrSql. Append ("VALUES (@ fileName, @ delete )");
SqlParameter [] parameters = {
New SqlParameter ("@ fileName", SqlDbType. NVarChar, 100 ),
New SqlParameter ("@ delete", SqlDbType. Bit ),

};
Parameters [0]. Value = "file type ";
Parameters [1]. Value = false;
Bool IsSucc = ExecUpdateSql (strSql. ToString (), parameters );
If (IsSucc)
{
Label1.Text = "successfully inserted ";
}
Else
{
Label1.Text = "insertion failed ";
}

}
/// Execute an update statement
/// </Summary>
/// <Param name = "SQLString"> SQL statement to be executed. </Param>
/// <Param name = "parms"> execution parameter array </param>
/// <Returns> success returns True, failure returns False. </Returns>
Private bool ExecUpdateSql (string SQLString, params SqlParameter [] extends parms)
{
Using (SqlCommand cmd = new SqlCommand ())
{
Try
{
PrepareCommand (cmd, conDB, _ trans, SQLString, partition parms );
Int iret = cmd. ExecuteNonQuery ();
Return true;
}
Catch (System. Data. SqlClient. SqlException e)
{
Return false;
}
}
}
Private void PrepareCommand (SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string plain text, SqlParameter [] partition parms)
{
If (conn. State! = ConnectionState. Open)
Conn. Open ();
Cmd. Connection = conn;
Cmd. CommandText = plain text;
If (trans! = Null)
Cmd. Transaction = trans;
Cmd. CommandType = CommandType. Text; // specify type;
If (partition parms! = Null)
{
Foreach (SqlParameter parameter in parameter parms)
{
If (parameter. Direction = ParameterDirection. InputOutput | parameter. Direction = ParameterDirection. Input )&&
(Parameter. Value = null ))
{
Parameter. Value = DBNull. Value;
}
Cmd. Parameters. Add (parameter );
}
}
}

}
}

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.