龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

ASP.NET防SQL注入之SqlParameter的使用介绍

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
string strOrderID = this.textBox1.Text.Trim().ToString(); string strCustomerID = this.textBox2.Text.Trim().ToString(); //被SQL注入的拼SQL的句子是这么写的 //string strSql = "Select * from [Orders] Where OrderId = " + strOrderID

string strOrderID = this.textBox1.Text.Trim().ToString();  
           string strCustomerID = this.textBox2.Text.Trim().ToString();  
           //被SQL注入的拼SQL的句子是这么写的  
           //string strSql = "Select * from [Orders] Where OrderId = '" + strOrderID + "' AND CustomerID = '" + strCustomerID + "'";  
           //防止SQL注入的拼SQL的句子是这么写的  
           string strSql = "Select * from [Orders] Where OrderId = @strOrderID  AND CustomerID = @strCustomerID";  
           //加sqlParameter变量  
           SqlCommand myCommand = new SqlCommand(strSql, con);  
           SqlParameter prOrderId = new SqlParameter("@strOrderID", SqlDbType.VarChar, 64);  
           prOrderId.Value = strOrderID;  
           myCommand.Parameters.Add(prOrderId);  
           //加sqlParameter变量  
           SqlParameter prCustomerID = new SqlParameter("@strCustomerID", SqlDbType.VarChar, 64);  
           prCustomerID.Value = strCustomerID;  
           myCommand.Parameters.Add(prCustomerID);

精彩图集

赞助商链接