<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqladvice.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">The Penton-izer</title><subtitle type="html">Communication Penton Style</subtitle><id>http://sqladvice.com/blogs/dpenton/atom.aspx</id><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/default.aspx" /><link rel="self" type="application/atom+xml" href="http://sqladvice.com/blogs/dpenton/atom.aspx" /><generator uri="http://communityserver.org" version="2.1.60809.935">Community Server</generator><updated>2004-02-23T08:17:00Z</updated><entry><title>A better, more manageable DataReader</title><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx" /><id>http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx</id><published>2004-09-20T12:57:00Z</published><updated>2004-09-20T12:57:00Z</updated><content type="html">&lt;FONT face=Tahoma size=2&gt;I had mentioned to the &lt;A href="http://www.pdnug.net/"&gt;Plano DNUG&lt;/A&gt; when I &lt;A href="http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx"&gt;presented&lt;/A&gt; a while back that I didn't like the idea of passing back a DataReader at all from a DAL.&amp;nbsp; You lose control of what happens to it when you do (as in the DAL no longer has the responsibility of closing the reader and ensuring its disposal).&amp;nbsp; Well, Teemu Kieski had a great idea for how to handle this situation in a new article &amp;#8220;&lt;A href="http://aspalliance.com/526"&gt;Using Delegates with DataReaders to control DAL Responsibility&lt;/A&gt;&amp;#8220; on &lt;A href="http://www.AspAlliance.com/"&gt;AspAlliance&lt;/A&gt;.&amp;nbsp; For a typical developer, it is still a bit advanced but I think the complexity that it hides and the elegance of execution far outweigh that.&amp;nbsp; I'll be updating my DAL for this...&lt;/FONT&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx&amp;amp;;subject=A+better%2c+more+manageable+DataReader" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx&amp;amp;;title=A+better%2c+more+manageable+DataReader" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx&amp;amp;title=A+better%2c+more+manageable+DataReader" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx&amp;amp;;title=A+better%2c+more+manageable+DataReader" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx&amp;amp;;title=A+better%2c+more+manageable+DataReader&amp;amp;;top=1" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/20/4234.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqladvice.com/aggbug.aspx?PostID=4234" width="1" height="1"&gt;</content><author><name>dpenton</name><uri>http://sqladvice.com/members/dpenton.aspx</uri></author><category term="General Programming" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/General+Programming/default.aspx" /><category term="SQL Server" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/SQL+Server/default.aspx" /><category term="ASP.Net" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/ASP.Net/default.aspx" /><category term="C#" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/C_2300_/default.aspx" /></entry><entry><title>Not-so Dynamic Sql</title><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx" /><id>http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx</id><published>2004-09-08T18:02:00Z</published><updated>2004-09-08T18:02:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Suppose you need to exclude or include parameters to&amp;nbsp;a sql statement.&amp;nbsp; Let's take this example:&lt;/FONT&gt;&lt;/P&gt;&lt;PRE style="BACKGROUND-COLOR: white"&gt;&lt;FONT color=#008000 size=2&gt;&lt;FONT color=#0000ff&gt;CREATE PROCEDURE dbo.myProc&lt;BR&gt;    @pkA int&lt;BR&gt;    , @pkB varchar(10)&lt;BR&gt;    , @pkC decimal(12, 2)&lt;BR&gt;&lt;BR&gt;AS&lt;BR&gt;&lt;BR&gt;SELECT&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#0000ff&gt;t&lt;/FONT&gt;.colA, &lt;FONT color=#0000ff&gt;t&lt;/FONT&gt;.colB, &lt;FONT color=#0000ff&gt;t&lt;/FONT&gt;.colC&lt;BR&gt;&lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#808080&gt;dbo.myTable&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;t&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;WHERE&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.pkA = @pkA&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#808080&gt;AND&lt;/FONT&gt; t.pkB = @pkB&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color=#808080&gt;AND&lt;/FONT&gt; t.pkC = @pkC&lt;/FONT&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;This&amp;nbsp;procedure expects three parameters, as does the sql statement.&amp;nbsp; So we want to omit the parameters based on being set to NULL.&amp;nbsp; There are several ways to do this:&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Use dynamic sql in your client language&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Use dynamic sql in your stored procedure&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Use conditional logic in your query with&lt;/FONT&gt;&lt;FONT face=Tahoma size=2&gt;&amp;nbsp;various combinations of &lt;FONT color=#808080&gt;OR&lt;/FONT&gt;&lt;FONT color=#000080&gt;,&lt;/FONT&gt; &lt;FONT color=#808080&gt;AND&lt;/FONT&gt; or&amp;nbsp;&lt;FONT color=#ff1493&gt;COALESCE/ISNULL&lt;/FONT&gt; in your query&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Use structured dynamic sql in a stored procedure&lt;/FONT&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Let me give an example of each:&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Build the sql in a string such as this:&lt;BR&gt;&lt;BR&gt;string sql = &amp;#8220;&lt;FONT color=#0000ff&gt;SELECT&lt;/FONT&gt; colA &lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt; tblA&amp;#8220;;&lt;BR&gt;if(prmB != null&amp;nbsp;&amp;amp;&amp;amp; prmB.Length != 0) sql += &amp;#8220; &lt;FONT color=#0000ff&gt;WHERE&lt;/FONT&gt; colB = '&amp;#8220; + prmB.Replace(&amp;#8220;'&amp;#8220;, &amp;#8220;''&amp;#8220;) + &amp;#8220;'&amp;#8220;;&amp;nbsp;&lt;BR&gt;&lt;BR&gt;You have to contend with ensuring that your logic in the final query is correct.&amp;nbsp; This is easy with a single parameter, but when you end up having more than one in your WHERE clause (or other clauses) then the logic can get quite complex.&amp;nbsp; We also needed to deal with SQL Injection.&amp;nbsp; Of course, this is more easily dealt with by using parameters on command objects (like using a SqlParameter with a SqlCommand object).&amp;nbsp; &lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;This is similar to the above but you are doing the logic in the stored procedure.&amp;nbsp; You still need to contend with sql injection in this case.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Conditional logic in your query means resorting to using &lt;FONT color=#808080&gt;OR&lt;/FONT&gt; and &lt;FONT color=#808080&gt;AND&lt;/FONT&gt; when needed:&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;SELECT&lt;/FONT&gt; colA &lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt; tblA&lt;BR&gt;&lt;FONT color=#0000ff&gt;WHERE&lt;/FONT&gt; (colB = @colB &lt;FONT color=#808080&gt;OR&lt;/FONT&gt; @colB &lt;FONT color=#0000ff&gt;IS&lt;/FONT&gt; &lt;FONT color=#808080&gt;NULL&lt;/FONT&gt;)&lt;BR&gt;&lt;BR&gt;-- OR --&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;SELECT&lt;/FONT&gt; colA &lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt; tblA&lt;BR&gt;&lt;FONT color=#0000ff&gt;WHERE&lt;/FONT&gt; colB = &lt;FONT color=#ff1493&gt;COALESCE&lt;/FONT&gt;(@colB, colB)&lt;BR&gt;&lt;BR&gt;This suffers from branching logic in your query.&amp;nbsp; It becomes more an more difficult for SQL Server to make a realistic execution plan for this type of query, especially when more conditions are present.&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Use structured dynamic sql in your procedure: &lt;/FONT&gt;&lt;PRE style="BACKGROUND-COLOR: white"&gt;&lt;FONT color=#008000&gt;&lt;FONT color=#0000ff&gt;&lt;FONT size=2&gt;CREATE PROCEDURE dbo.myProc&lt;BR&gt;    @pkA int&lt;BR&gt;    , @pkB varchar(10)&lt;BR&gt;    , @pkC decimal(12, 2)&lt;BR&gt;&lt;BR&gt;A&lt;FONT color=#0000ff&gt;S&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#0000ff size=2&gt;SET NOCOUNT ON &lt;BR&gt;&lt;BR&gt;DECLARE &lt;FONT color=#000000&gt;@sql nvarchar(4000)&lt;/FONT&gt; &lt;BR&gt;&lt;BR&gt;SET &lt;FONT color=#000000&gt;@sql =&lt;/FONT&gt; N' &lt;BR&gt;SELECT &lt;BR&gt;&amp;nbsp; &lt;FONT color=#008000&gt;&lt;FONT color=#0000ff&gt;t&lt;/FONT&gt;.colA, &lt;FONT color=#0000ff&gt;t&lt;/FONT&gt;.colB, &lt;FONT color=#0000ff&gt;t&lt;/FONT&gt;.colC&lt;/FONT&gt; &lt;BR&gt;FROM &lt;BR&gt;&amp;nbsp; &lt;FONT color=#808080&gt;dbo.myTable&lt;/FONT&gt; t &lt;BR&gt;WHERE &lt;BR&gt;&lt;FONT color=#000000&gt;&amp;nbsp; 1 = 1 &lt;BR&gt;&amp;nbsp; @pkAREP &lt;BR&gt;&amp;nbsp; @pkBREP &lt;BR&gt;&amp;nbsp; @pkCREP'&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;IF&lt;/FONT&gt; @pkA &lt;FONT color=#0000ff&gt;IS &lt;/FONT&gt;&lt;FONT color=#808080&gt;NOT NULL&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;SET&lt;/FONT&gt; @sql = &lt;FONT color=#ff1493&gt;REPLACE&lt;/FONT&gt;(@sql, '@pkAREP', 'AND a.pkA = @pkA')&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;ELSE&lt;BR&gt;&amp;nbsp;&amp;nbsp;SET&lt;/FONT&gt; @sql = &lt;FONT color=#ff1493&gt;REPLACE&lt;/FONT&gt;(@sql, '@pkAREP', '')&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;IF&lt;/FONT&gt; @pkB &lt;FONT color=#0000ff&gt;IS&lt;/FONT&gt; &lt;FONT color=#808080&gt;NOT NULL&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;SET&lt;/FONT&gt; @sql = &lt;FONT color=#ff1493&gt;REPLACE&lt;/FONT&gt;(@sql, '@pkBREP', 'AND a.pkB = @pkB')&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;ELSE&lt;BR&gt;&amp;nbsp;&amp;nbsp;SET&lt;/FONT&gt; @sql = &lt;FONT color=#ff1493&gt;REPLACE&lt;/FONT&gt;(@sql, '@pkBREP', '')&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;IF&lt;/FONT&gt; @pkC &lt;FONT color=#0000ff&gt;IS&lt;/FONT&gt; &lt;FONT color=#808080&gt;NOT NULL&lt;BR&gt;&lt;/FONT&gt;&amp;nbsp;&amp;nbsp;&lt;FONT color=#0000ff&gt;SET&lt;/FONT&gt; @sql = &lt;FONT color=#ff1493&gt;REPLACE&lt;/FONT&gt;(@sql, '@pkCREP', 'AND a.pkC = @pkC')&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;ELSE&lt;BR&gt;&amp;nbsp;&amp;nbsp;SET&lt;/FONT&gt; @sql = &lt;FONT color=#ff1493&gt;REPLACE&lt;/FONT&gt;(@sql, '@pkCREP', '')&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;EXECUTE&lt;/FONT&gt; "dbo"."sp_executesql" &lt;BR&gt;&amp;nbsp; @sql &lt;BR&gt;&amp;nbsp; , N'@pkA int, @pkB varchar(10), @pkC decimal(12, 0)' &lt;BR&gt;&amp;nbsp; , @pkA = @pkA &lt;BR&gt;&amp;nbsp; , @pkB = @pkB &lt;BR&gt;&amp;nbsp; , @pkC = @pkC &lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;RETURN&lt;/FONT&gt; 0&lt;/FONT&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/PRE&gt;&lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;I want to discuss option #4 above.&amp;nbsp; I believe it to do a number of things for the developer:&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Preventing SQL injection because you are using parameterized queries&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Caching query plans because you are using sp_executesql&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Controlling the data coming in to the procedure&lt;/FONT&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Preventing branching logic in your WHERE clause in the query&lt;/FONT&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;FONT face=Tahoma size=2&gt;
&lt;P&gt;I actually would do a little more with the '1=1' part, but for clarity in this post I am not.&amp;nbsp; Also, in my queries I typically have values that are always required so the '1=1' isn't necessary.&amp;nbsp; Of course, as the developer you need to ensure that you don't inadvertantly put any sql injection points in your dynamic portions of your sql.&amp;nbsp; But this would get caught in a code review, right?&amp;nbsp; ;-)&lt;/FONT&gt;&lt;/P&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx&amp;amp;;subject=Not-so+Dynamic+Sql" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx&amp;amp;;title=Not-so+Dynamic+Sql" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx&amp;amp;title=Not-so+Dynamic+Sql" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx&amp;amp;;title=Not-so+Dynamic+Sql" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx&amp;amp;;title=Not-so+Dynamic+Sql&amp;amp;;top=1" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/09/08/4233.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqladvice.com/aggbug.aspx?PostID=4233" width="1" height="1"&gt;</content><author><name>dpenton</name><uri>http://sqladvice.com/members/dpenton.aspx</uri></author><category term="SQL Server" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/SQL+Server/default.aspx" /></entry><entry><title>More on Interfaces and ADO.Net</title><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx" /><id>http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx</id><published>2004-06-03T14:28:00Z</published><updated>2004-06-03T14:28:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;I spoke at the &lt;A href="http://www.pdnug.net/"&gt;Plano DNUG&lt;/A&gt; last night and had a pretty good time.&amp;nbsp; Basically I presented some information on interfaces, talked about the interfaces in System.Data, discussed what happens behind the scenes with SqlConnection.Close() and SqlConnection.Dispose(), presented issues with creating a data layer, then some discussion on the &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbda/html/daab-rm.asp"&gt;DAAB&lt;/A&gt;.&amp;nbsp; One of the things I didn't show in the meeting last night is how you would deal with parameters.&amp;nbsp; It is a little different.&amp;nbsp; First off, how about a link to my files?&amp;nbsp; :-)&lt;BR&gt;&lt;BR&gt;&lt;A href="http://www.davidpenton.com/PresentationFiles.pdnug.20040602.zip"&gt;Presentation Files&lt;/A&gt;&lt;BR&gt;&lt;BR&gt;So, here is a code snip for executing a command on a SQL Server database:&lt;BR&gt;&lt;BR&gt;using(DbHelper db = new DbHelper(Configuration.ConnectionString, DbLibrary.SqlClient, true))&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IDbDataParameter prmDiv = db.GetDbDataParameter("@division", DbType.Int16);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; prmDiv.Value = System.Int16.Parse(this.ddlDivisions.SelectedItem.Value);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IDbDataParameter prmOrgStore = db.GetDbDataParameter("@original_store_num", DbType.Int32);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; prmOrgStore.Value = System.Int32.Parse(orgStore);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IDbDataParameter prmTrgStores = db.GetDbDataParameter("@target_store_num_xml", DbType.AnsiString);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; prmTrgStores.Value = xml.InnerXml;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; IDbDataParameter prmExecutingLoginId = db.GetDbDataParameter("@executing_login_id", DbType.AnsiString, 20);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; prmExecutingLoginId.Value = EM.Web.Configuration.WebUserId;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; db.ExecuteNonQuery(CommandType.StoredProcedure&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , spStorePlanCopy, prmDiv, prmOrgStore, prmTrgStores, prmExecutingLoginId);&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;This means that instead of using things like SqlDbType you are using the base DbType instead.&lt;BR&gt;&lt;BR&gt;Something&amp;nbsp;that I don't have in this [and that I want] is Parameter Caching.&amp;nbsp; That would be great, but I opted to not put that in here for now.&amp;nbsp; Why?&amp;nbsp; Because I'd rather examine the Microsoft DAAB a little closer to see if there are some other ways to accomplish that.&amp;nbsp; Also, not every provider/driver accepts parameterized sql statements (at least with some of the things I have developed before).&amp;nbsp; So, before I add it in, I wanted to make sure that I do the right things with it.&amp;nbsp; That could mean some sql statement parsing, so I wanted to let that go until later.&lt;BR&gt;&lt;BR&gt;So, if you download my presentation, at a minimum check out the DataAccess project.&amp;nbsp; That was the meat and potatoes of this presentation.&amp;nbsp; Let me know what you think!&lt;/FONT&gt;&lt;/P&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx&amp;amp;;subject=More+on+Interfaces+and+ADO.Net" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx&amp;amp;;title=More+on+Interfaces+and+ADO.Net" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx&amp;amp;title=More+on+Interfaces+and+ADO.Net" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx&amp;amp;;title=More+on+Interfaces+and+ADO.Net" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx&amp;amp;;title=More+on+Interfaces+and+ADO.Net&amp;amp;;top=1" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/06/03/4232.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqladvice.com/aggbug.aspx?PostID=4232" width="1" height="1"&gt;</content><author><name>dpenton</name><uri>http://sqladvice.com/members/dpenton.aspx</uri></author><category term="General Programming" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/General+Programming/default.aspx" /><category term="SQL Server" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/SQL+Server/default.aspx" /><category term="C#" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/C_2300_/default.aspx" /></entry><entry><title>More on inserting XML to SQL Server</title><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx" /><id>http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx</id><published>2004-04-15T04:48:00Z</published><updated>2004-04-15T04:48:00Z</updated><content type="html">&lt;FONT face=Tahoma size=2&gt;First off....I know I &lt;U&gt;&lt;STRONG&gt;badly&lt;/STRONG&gt;&lt;/U&gt; need to blog more.&amp;nbsp; Life just keeps running me over (enough of the excuses).&amp;nbsp; My friend &lt;A href="http://adminblogs.com/steve/"&gt;Steve Schofield&lt;/A&gt; wanted me to blog more on inserting XML into SQL Server.&amp;nbsp; So, I am going to post a number of things.&amp;nbsp; All that actually work by downloading the files.&lt;BR&gt;&lt;BR&gt;[1]&amp;nbsp;Table layout (dbo.TestMessages.TAB)&lt;BR&gt;[2] Stored Procedure (dbo.InsertMessageXml.PRC)&lt;BR&gt;[3] C# code with some XML&lt;BR&gt;&lt;BR&gt;I am also posting a &lt;A href="http://www.davidpenton.com/XmlInsert.zip"&gt;zip of everything&lt;/A&gt; (compiled EXE also).&amp;nbsp; Please change XmlInsert.exe.config file to point to your database.&amp;nbsp; Then, take the dbo.InsertMessageXml.PRC and dbo.TestMessages.TAB files and execute them to put those objects in your database.&lt;BR&gt;&lt;BR&gt;These files are based on the code in my &lt;A href="http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx"&gt;previous&lt;/A&gt; blog entry.&lt;/FONT&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx&amp;amp;;subject=More+on+inserting+XML+to+SQL+Server" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx&amp;amp;;title=More+on+inserting+XML+to+SQL+Server" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx&amp;amp;title=More+on+inserting+XML+to+SQL+Server" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx&amp;amp;;title=More+on+inserting+XML+to+SQL+Server" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx&amp;amp;;title=More+on+inserting+XML+to+SQL+Server&amp;amp;;top=1" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/04/15/4231.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqladvice.com/aggbug.aspx?PostID=4231" width="1" height="1"&gt;</content><author><name>dpenton</name><uri>http://sqladvice.com/members/dpenton.aspx</uri></author><category term="SQL Server" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/SQL+Server/default.aspx" /></entry><entry><title>SQLXML:  Steps to usage</title><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx" /><id>http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx</id><published>2004-03-22T13:43:00Z</published><updated>2004-03-22T13:43:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Tahoma&gt;&lt;FONT size=2&gt;Every once in a while people post to the lists that I am on (&lt;/FONT&gt;&lt;A href="http://www.aspadvice.com/"&gt;&lt;FONT size=2&gt;AspAdvice&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=2&gt;, &lt;/FONT&gt;&lt;A href="http://www.sswug.org/"&gt;&lt;FONT size=2&gt;SSWUG&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=2&gt;, &lt;/FONT&gt;&lt;A href="http://www.15seconds.com/"&gt;&lt;FONT size=2&gt;15seconds&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=2&gt;, &lt;/FONT&gt;&lt;A href="http://peach.ease.lsoft.com/archives/visbas-l.html"&gt;&lt;FONT size=2&gt;VISBAS-L&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=2&gt;) asking about how to pass lists of data into stored procedures.&amp;nbsp; There are a number of ways to accomplish this, some of which include (but not limited to):&lt;BR&gt;&lt;BR&gt;[1]&amp;nbsp; Insert into a temporary table on the client, use that in the procedure&lt;BR&gt;[2]&amp;nbsp; Call the stored procedure multiple times with the next value&lt;BR&gt;[3]&amp;nbsp; Pass in a delimited string to the procedure.&amp;nbsp; Then, have a UDF or other procedure to split this into a table so set processing can be done with it.&lt;BR&gt;[4] &amp;nbsp;Pass in an XML string and use it as a table&lt;BR&gt;&lt;BR&gt;There are pros and cons about each method listed above.&amp;nbsp; I will focus on the fourth method.&lt;BR&gt;&lt;BR&gt;One of the benefits to passing in an xml string of data is ease of ability to maintain a heirarchy.&amp;nbsp; You can pass in multiple levels of data and multiple values for a single logical row of data as well.&amp;nbsp; This offers much flexibility to the developer in terms of passing data to a SQL Server database.&amp;nbsp; I'll focus on SLQ Server 2000 (there are much more XML capabilities in Yukon).&amp;nbsp; So, let's start with some basics.&amp;nbsp; You can have element-centric documents, attribute-centric documents, and mixed.&amp;nbsp; Here are examples of that:&lt;BR&gt;&lt;BR&gt;Element-centric data:&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma&gt;&lt;FONT size=2&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;messages&lt;/SPAN&gt;&amp;gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;message&lt;/SPAN&gt;&amp;gt;This is a message&amp;lt;&lt;SPAN style="COLOR: blue"&gt;po&lt;/SPAN&gt;&amp;gt;123454321&amp;lt;/&lt;SPAN style="COLOR: blue"&gt;po&lt;/SPAN&gt;&amp;gt;&amp;lt;/&lt;SPAN style="COLOR: blue"&gt;message&lt;/SPAN&gt;&amp;gt;&amp;lt;/&lt;SPAN style="COLOR: blue"&gt;messages&lt;/SPAN&gt;&amp;gt; &lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=2&gt;Attribute-Centric data:&lt;BR&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;messages&lt;/SPAN&gt;&amp;gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;message&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;text&lt;/SPAN&gt;=&amp;#8220;This is a message&amp;#8221; &lt;SPAN style="COLOR: blue"&gt;po&lt;/SPAN&gt;=&amp;#8220;123454321&amp;#8221; /&amp;gt;&amp;lt;/&lt;SPAN style="COLOR: blue"&gt;messages&lt;/SPAN&gt;&amp;gt;&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma size=2&gt;Mixed&amp;nbsp;data:&lt;BR&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;messages&lt;/SPAN&gt;&amp;gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;message&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;text&lt;/SPAN&gt;=&amp;#8220;This is a message&amp;#8221; &lt;SPAN style="COLOR: blue"&gt;po&lt;/SPAN&gt;=&amp;#8220;123454321&amp;#8221;&amp;gt;Here is inner text&amp;lt;/&lt;SPAN style="COLOR: blue"&gt;message&lt;/SPAN&gt;&amp;gt;&amp;lt;/&lt;SPAN style="COLOR: blue"&gt;messages&lt;/SPAN&gt;&amp;gt;&lt;BR&gt;&lt;BR&gt;SQL Server can use any of these types, but I tend to use Attribute-centric data more often, mostly for the smaller payload.&amp;nbsp; These can come into a stored procedure with any of the character datatypes (char/varchar, nchar/nvarchar, text/ntext)&amp;nbsp; I typically use TEXT because I don't deal with National Text (extended character sets) very often.&amp;nbsp; And, since TEXT has an uppe rlimit of 2GB of data, I am pretty confident that I will not overflow with whatever I send.&amp;nbsp; Now, let's look at the methods we would use to utilize this xml in SQL Server.&amp;nbsp; When this data is passed into a stored procedure we have to tell SQL Server that this is XML.&amp;nbsp; We use sp_xml_preparedocument for that.&amp;nbsp; &lt;B&gt;&lt;I&gt;sp_xml_preparedocument&lt;/I&gt;&lt;/B&gt; is used to associate some text [that is in XML format] in a node tree that can be XPath searched.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;It can then be binded to a rowset provider [such as OPENXML] and be treated like any other table or view.&amp;nbsp; With this binding, we must be able to deallocate it.&amp;nbsp; &lt;B&gt;&lt;I&gt;sp_xml_removedocument&lt;/I&gt;&lt;/B&gt; is used to deallocate an xml node tree.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;This is vital to SQL Server as neglecting to release resources can lead to memory leaks.&amp;nbsp; The main construct in SQL Server ot use tihs isthe OPENXML clause in a query:&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;
&lt;DIV class=O v:shape="_x0000_s1026"&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;FONT size=2&gt;OPENXML&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;B&gt;&lt;FONT size=2&gt;(&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/B&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma&gt;&lt;FONT size=2&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;I&gt;idoc&lt;/I&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;B&gt;int&lt;/B&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; [&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;B&gt;in&lt;/B&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;], &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;I&gt;rowpattern&lt;/I&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;B&gt;nvarchar&lt;/B&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;[&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;B&gt;in&lt;/B&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;], [&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;I&gt;flags&lt;/I&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;B&gt;byte&lt;/B&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;[&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;B&gt;in&lt;/B&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;]]&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;B&gt;)&lt;/B&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma&gt;&lt;FONT size=2&gt;&lt;SPAN style="COLOR: blue"&gt; &lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;[WITH (&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;I&gt;SchemaDeclaration&lt;/I&gt;&lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt; | &lt;/SPAN&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;I&gt;TableName&lt;/I&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="COLOR: blue"&gt;&lt;FONT size=2&gt;)]&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face=Tahoma&gt;&amp;nbsp;&lt;/FONT&gt; &lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;
&lt;DIV class=O v:shape="_x0000_s1026"&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma&gt;&lt;FONT size=2&gt;&lt;B&gt;&lt;I&gt;idoc&lt;/I&gt;&lt;/B&gt; is a handle to an XML document that was prepared with sp_xml_preparedocument &lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma&gt;&lt;FONT size=2&gt;&lt;B&gt;&lt;I&gt;rowpattern&lt;/I&gt;&lt;/B&gt; is an XPath string that tells OPENXML where to start or search &lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma&gt;&lt;FONT size=2&gt;&lt;B&gt;&lt;I&gt;flags&lt;/I&gt;&lt;/B&gt; is a bitmask value that tells OPENXML to use element or attribute centric mapping &lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma&gt;&lt;FONT size=2&gt;&lt;B&gt;&lt;I&gt;SchemaDeclaration&lt;/I&gt;&lt;/B&gt; is a table layout mapped to SQL data types &lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma&gt;&lt;FONT size=2&gt;&lt;B&gt;&lt;I&gt;TableName&lt;/I&gt;&lt;/B&gt; is an existing table that OPENXML can use to map elements/attributes directly to columns by name &lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;
&lt;DIV class=O v:shape="_x0000_s1026"&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma size=2&gt;The rowpattern is a typical XPath query:&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /messages/message/segment&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; /messages/message/segment[@unit_cost&amp;gt;5.0] &lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma size=2&gt;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;BR&gt;&lt;FONT face=Tahoma size=2&gt;This is an NVARCHAR string and can be assigned programmatically in your stored procedures.&amp;nbsp;&amp;nbsp; &lt;STRONG&gt;Flags&lt;/STRONG&gt; are used to define the mapping to the relation:&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;
&lt;DIV class=O v:shape="_x0000_s1026"&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1; mso-margin-left-alt: 216"&gt;&lt;FONT face=Tahoma size=2&gt;[] &amp;nbsp;0: Defaults to attribute-centric mapping&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma size=2&gt;[]&amp;nbsp; 1: Use attribute-centric mapping&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma size=2&gt;[]&amp;nbsp; 2: Use element-centric mapping&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma size=2&gt;[]&amp;nbsp; 8: Can combine element and attribute centric mappings (deals with @mp:xmltext as well)&lt;BR&gt;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1; mso-margin-left-alt: 216"&gt;&lt;FONT face=Tahoma size=2&gt;Even if you are using flags 1 or 2 you can still access values across those boundaries (i.e. use attribute-centric mapping and still access element values) but there is a performance penalty.&amp;nbsp; &lt;B&gt;&lt;I&gt;SchemaDeclaration or TableName&lt;/I&gt;&lt;/B&gt;: This tells OPENXML the name, type and location of data it is using.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Using the TableName is much easier, but it is not flexible.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;SchemaDeclarations are much more versatile.&lt;BR&gt;&lt;BR&gt;TableName: &lt;/FONT&gt;
&lt;DIV v:shape="_x0000_s1026"&gt;
&lt;DIV class=O1 style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT * FROM OPENXML(@idoc, &amp;#8216;/ROOT&amp;#8217;, 1)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WITH dbo.MyTable &lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV class=O style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;BR&gt;&lt;FONT face=Tahoma size=2&gt;SchemaDeclaration: &lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV class=O1 style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma size=2&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; SELECT a.* FROM OPENXML(@idoc, &amp;#8216;/ROOT&amp;#8217;, 1)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WITH (&lt;BR&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;colA varchar(10) &amp;#8216;@myAttributeA&amp;#8217;&lt;BR&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;, colB int &amp;#8216;@myAttributeB&amp;#8217;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ) AS&amp;nbsp;a&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 10pt"&gt;&lt;B&gt;&lt;I&gt;&lt;FONT face=Tahoma&gt;More information about SchemaDeclarations &lt;/FONT&gt;&lt;/I&gt;&lt;/B&gt;&lt;/SPAN&gt;
&lt;DIV v:shape="_x0000_s1026"&gt;
&lt;DIV class=O1 style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma&gt;&lt;SPAN style="FONT-SIZE: 10pt"&gt;Be sure that you have the right datatypes defined.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Conversions are not always automatic.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;For instance, &amp;#8216;&amp;#8217; (a zero-length string) will implicitly convert &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt"&gt;to an integer but will not implicitly convert to a decimal/numeric.&amp;nbsp; &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt"&gt;If your data is variable (as in it could be a ZLS) then you can set your ColumnPattern to a VARCHAR and then manage your conversions in the &lt;/SPAN&gt;&lt;/FONT&gt;&lt;SPAN style="FONT-SIZE: 10pt"&gt;&lt;FONT face=Tahoma&gt;SELECT clause.&lt;BR&gt;&lt;BR&gt;Let's look at some basic code for this (leaving out error code for simplicity):&lt;BR&gt;&lt;BR&gt;StreamReader sr = new StreamReader(fileName); &lt;BR&gt;SqlConnection conn = new SqlConnection(connString); &lt;BR&gt;SqlCommand cmd = new SqlCommand(storedProcedureName, conn); &lt;BR&gt;cmd.CommandType = CommandType.StoredProcedure; &lt;BR&gt;cmd.Parameters.Add("return", SqlDbType.Int, 4).Direction = ParameterDirection.ReturnValue; &lt;BR&gt;cmd.Parameters.Add("@xml", SqlDbType.Text).Value = sr.ReadToEnd(); &lt;BR&gt;sr.Close(); &lt;BR&gt;conn.Open(); &lt;BR&gt;cmd.ExecuteNonQuery(); &lt;BR&gt;cmd.Dispose(); &lt;BR&gt;conn.Close(); &lt;BR&gt;conn.Dispose(); &lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Tahoma size=2&gt;The XML we want to pass in:&lt;BR&gt;&lt;BR&gt;&amp;lt;?&lt;FONT color=#0000ff&gt;xml&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;version&lt;/FONT&gt;="1.0"?&amp;gt;&lt;BR&gt;&amp;lt;&lt;FONT color=#0000ff&gt;messages&lt;/FONT&gt; currentDate="2003-09-08T15:40:31"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;message&lt;/FONT&gt; po="100000006980" version_nbr="1" not_before_date="2003-09-11"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;segment&lt;/FONT&gt; item="5234004210108" unit_cost="6.25" qty="6" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;&lt;FONT color=#0000ff&gt;segment&lt;/FONT&gt; &lt;/FONT&gt;item="5234004210109" unit_cost="26.25" qty="6" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;segment&lt;/FONT&gt; item="5234004210110" unit_cost="16.25" qty="6" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;segment&lt;/FONT&gt; item="5234004210111" unit_cost="26.25" qty="6" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;segment&lt;/FONT&gt; item="5234004210112" unit_cost="26.25" qty="6" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;segment&lt;/FONT&gt; item="5234004210113" unit_cost="26.25" qty="6" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;FONT color=#0000ff&gt;message&lt;/FONT&gt;&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;message&lt;/FONT&gt; po="100000006982" version_nbr="1" not_before_date="2003-09-29"&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;segment&lt;/FONT&gt; item="5238052010705" unit_cost="145.40" qty="4" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;segment&lt;/FONT&gt; item="5238052010706" unit_cost="145.40" qty="4" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;segment&lt;/FONT&gt; item="5238053010605" unit_cost="15.40" qty="20" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;&lt;FONT color=#0000ff&gt;segment&lt;/FONT&gt; item="5238053010606" unit_cost="15.40" qty="20" /&amp;gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;/&lt;FONT color=#0000ff&gt;message&lt;/FONT&gt;&amp;gt;&lt;BR&gt;&amp;lt;/&lt;FONT color=#0000ff&gt;messages&lt;/FONT&gt;&amp;gt;&lt;BR&gt;&lt;BR&gt;And the SQL:&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;CREATE PROCEDURE&lt;/FONT&gt; dbo.MyProcedure&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; @xml TEXT&lt;BR&gt;AS&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV class=O1 style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma color=#0000ff size=2&gt;SET NOCOUNT ON&lt;BR&gt;&lt;BR&gt;DECLARE &lt;FONT color=#000080&gt;@ret int, @iXml int&lt;BR&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV class=O1 style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma size=2&gt;&lt;FONT color=#0000ff&gt;EXECUTE&lt;/FONT&gt; @ret = [master].[dbo].[sp_xml_preparedocument] @iXml OUTPUT, @xml&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV class=O1 style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma size=2&gt;&lt;FONT color=#0000ff&gt;&lt;BR&gt;INSERT INTO&lt;/FONT&gt; [dbo].[XmlTestTable]&lt;BR&gt;&lt;FONT color=#0000ff&gt;SELECT&lt;/FONT&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; a.po, a.version_nbr, a.not_before_date, a.item, a.unit_cost, a.qty&lt;BR&gt;&lt;FONT color=#0000ff&gt;FROM&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; OPENXML&lt;/FONT&gt;(@iXml, '/messages/message/segment', 1) -- start at the message segment node&lt;BR&gt;&lt;FONT color=#0000ff&gt;WITH&lt;/FONT&gt; (&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; po decimal(12, 0) '../@po'&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , version_nbr int '../@version_nbr'&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , not_before_date datetime '../@not_before_date'&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , item char(13)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , unit_cost decimal(8, 2)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , qty int&lt;BR&gt;) &lt;FONT color=#0000ff&gt;AS&lt;/FONT&gt; a&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV class=O1 style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma size=2&gt;&lt;BR&gt;-- release the XML document now that we are done&lt;BR&gt;&lt;FONT color=#0000ff&gt;EXECUTE&lt;/FONT&gt; [master].[dbo].[sp_xml_removedocument] @iXml&lt;/FONT&gt;&lt;/DIV&gt;
&lt;DIV class=O1 style="mso-line-spacing: '100 50 0'; mso-char-wrap: 1; mso-kinsoku-overflow: 1"&gt;&lt;FONT face=Tahoma size=2&gt;GO&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;There is quite a bit more of this.&amp;nbsp; I'll post more later in the week.&lt;/DIV&gt;&lt;/FONT&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx&amp;amp;;subject=SQLXML%3a++Steps+to+usage" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx&amp;amp;;title=SQLXML%3a++Steps+to+usage" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx&amp;amp;title=SQLXML%3a++Steps+to+usage" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx&amp;amp;;title=SQLXML%3a++Steps+to+usage" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx&amp;amp;;title=SQLXML%3a++Steps+to+usage&amp;amp;;top=1" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/22/4230.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqladvice.com/aggbug.aspx?PostID=4230" width="1" height="1"&gt;</content><author><name>dpenton</name><uri>http://sqladvice.com/members/dpenton.aspx</uri></author><category term="SQL Server" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/SQL+Server/default.aspx" /></entry><entry><title>"My eBooks" folder: Die!</title><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx" /><id>http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx</id><published>2004-03-18T11:43:00Z</published><updated>2004-03-18T11:43:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Tahoma&gt;In case anyone *hates* the "My eBooks" folder that gets created in "My Documents" when &lt;BR&gt;Adobe Acrobat opens, here is the *fix*:&lt;BR&gt;&lt;BR&gt;&lt;/FONT&gt;&lt;FONT face=Tahoma&gt;&lt;A href="http://www.annoyances.org/exec/forum/winxp/n1072006739"&gt;http://www.annoyances.org/exec/forum/winxp/n1072006739&lt;/A&gt;&lt;BR&gt;&lt;BR&gt;My...that thing was annoying!&lt;/FONT&gt;&lt;/P&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx&amp;amp;;subject=%22My+eBooks%22+folder%3a+Die!" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx&amp;amp;;title=%22My+eBooks%22+folder%3a+Die!" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx&amp;amp;title=%22My+eBooks%22+folder%3a+Die!" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx&amp;amp;;title=%22My+eBooks%22+folder%3a+Die!" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx&amp;amp;;title=%22My+eBooks%22+folder%3a+Die!&amp;amp;;top=1" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/18/4229.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqladvice.com/aggbug.aspx?PostID=4229" width="1" height="1"&gt;</content><author><name>dpenton</name><uri>http://sqladvice.com/members/dpenton.aspx</uri></author><category term="Miscellaneous" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/Miscellaneous/default.aspx" /></entry><entry><title>More on Interfaces for System.Data</title><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx" /><id>http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx</id><published>2004-03-16T18:09:00Z</published><updated>2004-03-16T18:09:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Aaron &lt;A href="http://sqladvice.com/blogs/aweiker/archive/2004/03/09/4239.aspx"&gt;blogged&lt;/A&gt; about interfaces and asked about people usage.&amp;nbsp; I have been using them more and more in code.&amp;nbsp; Here are some examples of closing and disposing of System.Data (and Microsoft.Data) resources using interfaces: &lt;PRE&gt;&lt;FONT size=2&gt;static void DisposeIDbDataAdapter(ref IDbDataAdapter da)
{
 if(da != null)
 {
   if(da is IDisposable) ((IDisposable)da).Dispose();
   da = null;
 }
}
static void DisposeIDataReader(ref IDataReader dr)
{
 if(dr != null)
 {
   dr.Dispose();
   dr = null;
 }
}
static void DisposeIDbCommand(ref IDbCommand cmd)
{
 if(cmd != null)
 {
   cmd.Dispose();
   cmd = null;
 }
}

static void DisposeIDbConnection(ref IDbConnection conn)
{
 if(conn != null)
 {
   if(conn.State != ConnectionState.Closed) conn.Close();

   conn.Dispose();

   conn = null;
 } 
}&lt;/FONT&gt;&lt;/PRE&gt;&lt;FONT face=tahoma size=2&gt;Ok...some questions I am sure will come of this.&amp;nbsp; First off, why do this?&amp;nbsp; Well, these methods can be used for any of the major providers (SqlClient, Odbc, OleDb, OracleClient and others).&amp;nbsp; Much less code to write and maintain.&amp;nbsp; Why the &lt;STRONG&gt;ref&lt;/STRONG&gt; keyword?&amp;nbsp; Well, if you have been keeping up with Paul Wilson (&lt;A href="http://weblogs.asp.net/pwilson/archive/2004/02/20/77422.aspx"&gt;here&lt;/A&gt;, &lt;A href="http://weblogs.asp.net/pwilson/archive/2004/02/20/77429.aspx"&gt;here&lt;/A&gt;, and &lt;A href="http://weblogs.asp.net/pwilson/archive/2004/02/20/77435.aspx"&gt;here&lt;/A&gt;) you'd see that his tests show there is some benefit to setting variables to null (Nothing in VB.Net).&amp;nbsp; I use the &lt;STRONG&gt;ref&lt;/STRONG&gt; keyword to allow me to operate on the original variable (not only the original object).&amp;nbsp; Why the check on IDbDataAdapter?&amp;nbsp; Well, System.Data.IDbDataAdapter does not implement IDisposable, but the major providers do implement IDisposable (it is in its inheritance chain).&amp;nbsp; Also, why do I call IDbConnection.Close() &lt;STRONG&gt;AND&lt;/STRONG&gt; IDbConnection.Dispose()?&amp;nbsp; If you check System.Data.SqlClient.SqlConnection in &lt;A href="http://www.saurik.com/net/exemplar/"&gt;Anakrino&lt;/A&gt;, you'l see that Dispose(bool disposing) does indeed call Close().&amp;nbsp; Note the signature diferences?&amp;nbsp; We can only call the publically available Dispose() method, not the protected Dispose(bool disposing) method.&amp;nbsp; We have to depend on the framework to manage that.&amp;nbsp; So, even if doing all of this doesn't buy me much, what it does buy me is refactored and self-documented code.&amp;nbsp; Of course, don't forget the C# comments that belong on these functions. :-)&lt;BR&gt;&lt;BR&gt;One other thing to add: The &lt;STRONG&gt;using&lt;/STRONG&gt; keyword.&amp;nbsp; Why not always use that?&amp;nbsp; You can use it when your coding methodology calls for it.&amp;nbsp; In fact, use it as much as you can.&amp;nbsp; Where it makes sense.&lt;BR&gt;&lt;BR&gt;Are there better ways of doing this?&amp;nbsp; I am sure that there are.&amp;nbsp; It would be short order to deal with that with this framework.&amp;nbsp; If anything&amp;nbsp;refactoring and self-documentation are&amp;nbsp;worth it to me.&lt;/FONT&gt;&lt;/FONT&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx&amp;amp;;subject=More+on+Interfaces+for+System.Data" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx&amp;amp;;title=More+on+Interfaces+for+System.Data" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx&amp;amp;title=More+on+Interfaces+for+System.Data" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx&amp;amp;;title=More+on+Interfaces+for+System.Data" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx&amp;amp;;title=More+on+Interfaces+for+System.Data&amp;amp;;top=1" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4228.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqladvice.com/aggbug.aspx?PostID=4228" width="1" height="1"&gt;</content><author><name>dpenton</name><uri>http://sqladvice.com/members/dpenton.aspx</uri></author><category term="General Programming" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/General+Programming/default.aspx" /><category term="SQL Server" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/SQL+Server/default.aspx" /><category term="ASP.Net" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/ASP.Net/default.aspx" /><category term="C#" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/C_2300_/default.aspx" /></entry><entry><title>SQL Server 2000 System Table Map</title><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx" /><id>http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx</id><published>2004-03-16T12:56:00Z</published><updated>2004-03-16T12:56:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;I thought it would be worth mentioning again that there is a compiled help file called &amp;#8220;SQL Server 2000 System Table Map&amp;#8221; which is right &lt;A href="http://www.microsoft.com/sql/techinfo/productdoc/2000/systables.asp"&gt;here&lt;/A&gt;.&amp;nbsp; I believe it to be a very valuable documentation tool for the database administrator, as well as the database programmer.&amp;nbsp; An understanding of the lower levels of a product is always a good thing, no?&lt;BR&gt;&lt;BR&gt;Yes, I know it will change for Yukon, but people will be on SQL 2000 for some time still.&lt;/FONT&gt;&lt;/P&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx&amp;amp;;subject=SQL+Server+2000+System+Table+Map" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx&amp;amp;;title=SQL+Server+2000+System+Table+Map" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx&amp;amp;title=SQL+Server+2000+System+Table+Map" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx&amp;amp;;title=SQL+Server+2000+System+Table+Map" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx&amp;amp;;title=SQL+Server+2000+System+Table+Map&amp;amp;;top=1" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/03/16/4227.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqladvice.com/aggbug.aspx?PostID=4227" width="1" height="1"&gt;</content><author><name>dpenton</name><uri>http://sqladvice.com/members/dpenton.aspx</uri></author><category term="SQL Server" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/SQL+Server/default.aspx" /></entry><entry><title>Security Warning</title><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx" /><id>http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx</id><published>2004-02-23T13:59:00Z</published><updated>2004-02-23T13:59:00Z</updated><content type="html">&lt;FONT face=Tahoma size=2&gt;Something I was reading on a list I am on made me think of security.&amp;nbsp; Periodically I do searches on &lt;A href="http://www.google.com/"&gt;Google&lt;/A&gt; to see how secure some sites are designed.&amp;nbsp; I won't put my actual search here, but if you search for specific parts of connection strings you may occasionally find more information than you bargained for (such as user names and passwords).&amp;nbsp; Most people are pretty vigilant about not exposing any of this.&amp;nbsp; Sometimes this information comes through from comments on a webpage.&amp;nbsp; Typically when I make this type of search I'll find 2 or 3 sites that have these errors and I promptly notify them.&amp;nbsp; This is just a heads-up to developers out there to make sure you aren't subject to this kind of error.&lt;/FONT&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx&amp;amp;;subject=Security+Warning" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx&amp;amp;;title=Security+Warning" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx&amp;amp;title=Security+Warning" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx&amp;amp;;title=Security+Warning" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx&amp;amp;;title=Security+Warning&amp;amp;;top=1" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4226.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqladvice.com/aggbug.aspx?PostID=4226" width="1" height="1"&gt;</content><author><name>dpenton</name><uri>http://sqladvice.com/members/dpenton.aspx</uri></author><category term="General Programming" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/General+Programming/default.aspx" /><category term="SQL Server" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/SQL+Server/default.aspx" /><category term="ASP.Net" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/ASP.Net/default.aspx" /></entry><entry><title>The Axiomatic Method</title><link rel="alternate" type="text/html" href="http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx" /><id>http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx</id><published>2004-02-23T13:17:00Z</published><updated>2004-02-23T13:17:00Z</updated><content type="html">&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;So, I have started blogging.&amp;nbsp; I had been thinking about how and what I would start this off.&amp;nbsp; It occured to me that I should talk about something that I had learned throughout my life but never knew the name of until college.&amp;nbsp; Even after that, I wasn't sure ow to describe the methods I take when designing systems.&amp;nbsp; &lt;/FONT&gt;&lt;A href="http://www.britannica.com/eb/article?eu=11615" target=_blank&gt;&lt;FONT face=Tahoma size=2&gt;Encyclop&amp;#230;dia Britannica&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Tahoma size=2&gt; defines the Axoimatic Method:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&lt;FONT face=Tahoma size=2&gt;in logic, a procedure by which an entire system (e.g., a science) is generated in accordance with specified rules by logical deduction from certain basic propositions (axioms or postulates), which in turn are constructed from a few terms taken as primitive&lt;/FONT&gt;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Simply put, we have it as a "system" with "rules".&amp;nbsp; Looking at this from another angle, each rule could be defined as a system by itself.&amp;nbsp; I suppose this could be likened to a parent-child relationship in the sense that you could have something like&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;system&lt;BR&gt;&amp;nbsp; --&amp;gt; rule1&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --&amp;gt; rule3&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --&amp;gt; rule4&lt;BR&gt;&amp;nbsp; --&amp;gt; rule2&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --&amp;gt; rule5&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;where rule1 and rule2 are "systems" themselves.&amp;nbsp; Enough of this meta-speak...so how do I want to relate this to programming in general?&amp;nbsp; Actually, I'd say more specifically system design in general.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Suppose your boss comes in to your office/cubicle and tells you "I need a system that will give me a report on widgets in the warehouse.&amp;nbsp; I need it as soon as possible."&amp;nbsp; Some paths you can take include [] make a basic system from existing resources based on what your boss told you and [] ask questions.&amp;nbsp; From the beginning, you have to think about how this is to be designed.&amp;nbsp; Of course, your design could be a simple, static webpage that you put together on demand (when your boss tells you to).&amp;nbsp;&amp;nbsp; You could make it where someone enters all of the data into your system.&amp;nbsp; You might even want to pull data from an existing system.&amp;nbsp; All somewhat viable options.&amp;nbsp; But, you of course need to dig deeper into what your boss' vision might be.&amp;nbsp; You will most likely need some sort of input mechanism.&amp;nbsp; Possibly some filtering and sorting.&amp;nbsp; Perhaps different style reports (aggregates and such).&amp;nbsp; Each of these functional requirements depends on rules for definition.&amp;nbsp; Each of the functional requirements make up the system you are defining.&amp;nbsp; The smaller each "rule" is, the easier it is to understand and adjust when needed.&amp;nbsp; Also, and this is fairly important...the easier it is to reuse the rule anywhere else in the system when appropriate.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;This moves into another topic that I'll just mention here: &lt;/FONT&gt;&lt;A href="http://www.amazon.com/exec/obidos/ASIN/0201485672/qid=1077545039/sr=2-1/ref=sr_2_1/002-0990208-9063237" target=_blank&gt;&lt;FONT face=Tahoma size=2&gt;refactoring&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Tahoma size=2&gt;.&amp;nbsp; This book is probably one of the most important books to get you started down the path of good system design.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;How do you use this in real life?&amp;nbsp; Start looking at your designs from the perspective that even if your "system" is small, you can add to it easily.&amp;nbsp; Consider that youmay need to modify bisiness rules in the future.&amp;nbsp; Make that easier to do without reworking the system design.&amp;nbsp; Realizing what scope your rules are makes these processes easier.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Apply at least some of these ideas to your current programmatic methods and I'll bet a better system will come of it.&lt;/FONT&gt;&lt;/P&gt;
&lt;div class = "shareblock"&gt;&lt;strong&gt;Share this post:&lt;/strong&gt; &lt;a href = "mailto:?body=Thought you might like this: http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx&amp;amp;;subject=The+Axiomatic+Method" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx&amp;amp;;title=The+Axiomatic+Method" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx&amp;amp;;phase=2" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx&amp;amp;title=The+Axiomatic+Method" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx&amp;amp;;title=The+Axiomatic+Method" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;;mkt=en-us&amp;amp;;url=http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx&amp;amp;;title=The+Axiomatic+Method&amp;amp;;top=1" target="_blank" title = "Post http://sqladvice.com/blogs/dpenton/archive/2004/02/23/4225.aspx"&gt;live it!&lt;/a&gt;&lt;/div&gt;&lt;img src="http://sqladvice.com/aggbug.aspx?PostID=4225" width="1" height="1"&gt;</content><author><name>dpenton</name><uri>http://sqladvice.com/members/dpenton.aspx</uri></author><category term="General Programming" scheme="http://sqladvice.com/blogs/dpenton/archive/tags/General+Programming/default.aspx" /></entry></feed>