﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-AndyZhang-随笔分类-ADO.NET</title><link>http://www.blogjava.net/AndyZhang/category/51552.html</link><description>welcome to java world</description><language>zh-cn</language><lastBuildDate>Tue, 29 May 2012 14:32:08 GMT</lastBuildDate><pubDate>Tue, 29 May 2012 14:32:08 GMT</pubDate><ttl>60</ttl><item><title>创建更稳健的数据命令(参数化数据命令)</title><link>http://www.blogjava.net/AndyZhang/archive/2012/05/29/379440.html</link><dc:creator>SkyDream</dc:creator><author>SkyDream</author><pubDate>Tue, 29 May 2012 07:13:00 GMT</pubDate><guid>http://www.blogjava.net/AndyZhang/archive/2012/05/29/379440.html</guid><description><![CDATA[<p>
 
　　　在参数化数据命令中，使用参数作为占位符来替代硬编码的值。这些参数将被分别添加，并自动进行特殊字符的编码处理。例如下面的ＳＱＬ语句：　select * from customers where customerID='Alfki' &nbsp;可以转换为一个参数化的sql语句：select * from customers where customerID=@customerID</p><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;不同的数据提供程序，参数化数据命令的语法是不同的。对于sqlserver数据提供程序，参数化的数据命令是使用唯一的命名占位符作为参数。参数名可以任意选取，但是必须是以@字符开头。通常情况下我们是以字段名作为相应的参数名（比如上面的语句中使用@customerID作为customerID字段的参数名）。ole DB数据提供程序则采用了不同的语法。它要求每一个参数使用一个问号（？）来表示，在其sql语句中，参数并不是通过参数名来标识的，而是根据参数在sql语句中出现的位置来标识的。如下面：select * from customers where customerID=?</p><p>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;无论用哪种方式来标识数据命令中的参数，都需要为sql语句中的每一个参数提供相应的Parameter对象，每一个Parameter对象都将被添加到Command.Parameters参数集合中。对于ole &nbsp;DB数据提供程序，一定要按照参数在sql语句中出现的顺序来添加相应的Parameter对象。对于sql　server数据提供程序来说，添加参数的顺序是无关紧要的，因为参数将根据参数名来匹配相应的占位符。</p><p>&nbsp; &nbsp; &nbsp; protected void cmdInsert_Click(object sender, EventArgs e)</p><p>&nbsp; &nbsp; &nbsp;{</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string insertSQL;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insertSQL="insert into authors(";</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insertSQL+="au_id,au_fname,au_lname,
contract
) ";</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; insertSQL+="values(@au_id,@au_fname,@au_lname,@contract)";</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sqlConnection con=new sqlConnection(connectionstring);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sqlCommand cmd=new sqlCommand(insertSQL,con);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //添加相应的参数</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cmd.Parameters.AddWithValue("@au_id",txtID.text);</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;cmd.Parameters.AddWithValue("@au_fname",txtFirstName.text);&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;cmd.Parameters.AddWithValue("@au_lname",txtLastName.text);&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;cmd.Parameters.AddWithValue("@
contract
",Convert.ToInt16(chkContract.Checked));&nbsp;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int added=0;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
{</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;con.Open();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;added=cmd.ExecuteNonQuery();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lblstatus.Text=added.ToString()+"条记录已插入";</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; </p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;　Catch(Exception err)</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lblstatus.Text="错误："+err.Message;</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p><p>　　　　finally</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; con.Close();</p><p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}</p><p>&nbsp; &nbsp; }</p><p>&nbsp; &nbsp;&nbsp;使用参数化的数据命令，参数值已经从sql命令中移除，并添加到了Parameters集合中。这样，在参数值中出现的引号或者sql语句片段将不会对sql命令的执行造成任何问题。这样也就可以防sql注入式攻击。</p><p>&nbsp; &nbsp;&nbsp;增、删、改功能都可以用这种参数化数据命令写sql语句。</p><img src ="http://www.blogjava.net/AndyZhang/aggbug/379440.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/AndyZhang/" target="_blank">SkyDream</a> 2012-05-29 15:13 <a href="http://www.blogjava.net/AndyZhang/archive/2012/05/29/379440.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>