龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 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);

收藏文章
表情删除后不可恢复,是否删除
取消
确定
图片正在上传,请稍后...
评论内容为空!
还没有评论,快来抢沙发吧!

热评话题

按钮 内容不能为空!
立刻说两句吧! 查看0条评论
精彩图集

赞助商链接