<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>算法交易-Bali &#187; db</title>
	<atom:link href="http://libaocun.com/tag/db/feed" rel="self" type="application/rss+xml" />
	<link>http://libaocun.com</link>
	<description>Think globally, act locally.</description>
	<lastBuildDate>Fri, 06 Jan 2012 02:51:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Calling SQL Server Stored Procedures with ADO.NET in 5 minutes</title>
		<link>http://libaocun.com/calling-sql-server-stored-procedures-with-ado-net-in-5-minutes</link>
		<comments>http://libaocun.com/calling-sql-server-stored-procedures-with-ado-net-in-5-minutes#comments</comments>
		<pubDate>Sun, 02 Aug 2009 02:35:01 +0000</pubDate>
		<dc:creator>Bali</dc:creator>
				<category><![CDATA[一些老文章]]></category>
		<category><![CDATA[ACT]]></category>
		<category><![CDATA[db]]></category>

		<guid isPermaLink="false">http://www.balionweb.com/?p=12</guid>
		<description><![CDATA[A stored procedure is an already written SQL statement that is saved in the database. It can take parameters; return objects you specified, just like what happens in any other programming languages you are familiar with. Why stored procedures instead of random SQL? For me: 1. Modular Programming- Stored procedures allow developers to encapsulate business [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: 'Verdana','sans-serif'; font-size: 10pt;" lang="EN-US"><span style="font-family: Calibri; font-size: small;">A stored procedure is an already written SQL statement that is saved in the database. It can take parameters; return objects you specified, just like what happens in any other programming languages you are familiar with.</span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span lang="EN-US"><span style="font-family: Calibri; font-size: small;"><span style="font-family: Calibri; font-size: small;"> </span></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt;"><span style="font-family: 'Verdana','sans-serif'; font-size: 10pt;" lang="EN-US"><span style="font-family: Calibri; font-size: small;">Why stored procedures instead of random SQL? For me:</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;"><span style="font-family: 'Verdana','sans-serif'; font-size: 10pt;" lang="EN-US"><span><span style="font-family: Calibri; font-size: small;">1.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span></span><span style="font-family: 'Verdana','sans-serif'; font-size: 10pt;" lang="EN-US"><span style="font-family: Calibri; font-size: small;">Modular Programming- <em>Stored procedures</em> allow developers to encapsulate business functionality and provide callers with a simple interface. Once interfaces are settled down, caller and callee coding work can be assigned to different team/persons.</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;"><span style="font-family: 'Verdana','sans-serif'; font-size: 10pt;" lang="EN-US"><span><span style="font-family: Calibri; font-size: small;">2.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span></span><span style="font-family: 'Verdana','sans-serif'; font-size: 10pt;" lang="EN-US"><span style="font-family: Calibri; font-size: small;">Security Enhancement- Users can be granted permission to execute a stored procedure. Use parameterized queries—not string concatenation—to build queries.</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;"><span style="font-family: 'Verdana','sans-serif'; font-size: 10pt;" lang="EN-US"><span><span style="font-family: Calibri; font-size: small;">3.<span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span></span><span style="font-family: 'Verdana','sans-serif'; font-size: 10pt;" lang="EN-US"><span style="font-family: Calibri; font-size: small;">Reduce network traffic – Benefits can be easily noticed if you have frequently called T-SQL code of hundreds of lines.</span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;"><span style="font-family: 'Verdana','sans-serif';" lang="EN-US"><span><span style="font-family: Calibri; font-size: small;"><span style="font-size: small;">4.</span><span style="font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; font-size: 7pt; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none;"> </span></span></span></span><span style="font-family: 'Verdana','sans-serif'; font-size: 10pt;" lang="EN-US"><span style="font-family: Calibri; font-size: small;">Performance – Stored procedures are registered at servers, and as a result DBAs/servers get more change to optimize them.</span></span><span style="font-family: 'Verdana','sans-serif';" lang="EN-US"> </span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;"><span lang="EN-US"> </span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;"><span lang="EN-US"><span style="font-family: Calibri; font-size: small;">Coding time now. </span></span><span lang="EN-US"><span style="font-family: Calibri; font-size: small;">Firstly, you need run below SQL script in SQL2005. </span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;">
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;"><span lang="EN-US"><span style="font-family: Calibri; font-size: small;"><strong><span style="text-decoration: underline;">SQL Script</span></strong></span></span></p>
<p class="MsoNormal" style="margin: 0cm 0cm 0pt; text-align: left;" align="left"><span style="font-family: 'Courier New'; font-size: 10pt;" lang="EN-US"><strong> </strong></span></p>
<table class="MsoTableGrid" style="border: medium none ; background: #eeece1 none repeat scroll 0% 0%; border-collapse: collapse; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="border: medium none black; padding: 0in 5.4pt; background-color: transparent; width: 6.15in;" width="900" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: green; font-size: 8pt;">&#8211; Create a test DB</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">USE</span><span style="font-family: 'Courier New'; font-size: 8pt;"> [master] </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;">GO</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">CREATE</span><span style="font-family: 'Courier New'; font-size: 8pt;"> <span style="color: blue;">DATABASE</span> SpTestDB</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;">GO</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">USE</span><span style="font-family: 'Courier New'; font-size: 8pt;"> SpTestDB</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;">GO</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: green; font-size: 8pt;">&#8211; Create a test Table</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">CREATE</span><span style="font-family: 'Courier New'; font-size: 8pt;"> <span style="color: blue;">TABLE</span> dbo<span style="color: gray;">.</span>TestTable <span style="color: gray;">(</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>id [nvarchar]<span style="color: gray;">(</span>50<span style="color: gray;">)</span> <span style="color: gray;">NULL,</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>SomeValue [int] <span style="color: gray;">NULL</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: gray; font-size: 8pt;">)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;">GO</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: green; font-size: 8pt;">&#8211; Create read SP</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">CREATE</span><span style="font-family: 'Courier New'; font-size: 8pt;"> <span style="color: blue;">PROCEDURE</span> dbo<span style="color: gray;">.</span>ReadData</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>@id <span style="color: blue;">nvarchar</span> <span style="color: gray;">(</span>50<span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">AS</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">BEGIN</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">SELECT</span> <span style="color: gray;">*</span> <span style="color: blue;">FROM</span> dbo<span style="color: gray;">.</span>TestTable</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">WHERE</span> id <span style="color: gray;">=</span> @id<span style="color: gray;">;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span style="color: gray;"><span style="color: #0000ff;">END</span></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;">GO</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: green; font-size: 8pt;">&#8211; Create write SP</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">CREATE</span><span style="font-family: 'Courier New'; font-size: 8pt;"> <span style="color: blue;">PROCEDURE</span> dbo<span style="color: gray;">.</span>WriteData</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>@id <span style="color: blue;">nvarchar</span> <span style="color: gray;">(</span>50<span style="color: gray;">),</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>@SomeValue <span style="color: blue;">int</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">AS</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">BEGIN</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">INSERT</span> <span style="color: blue;">INTO</span> dbo<span style="color: gray;">.</span>TestTable <span style="color: gray;">(</span>id<span style="color: gray;">,</span> SomeValue<span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">VALUES</span> <span style="color: gray;">(</span>@id<span style="color: gray;">,</span> @SomeValue<span style="color: gray;">);</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span style="color: gray;"><span style="color: #0000ff;">END</span></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;">GO</span></p>
</td>
</tr>
</tbody>
</table>
<p><strong> </strong></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;"><span lang="EN-US"><span style="font-family: Calibri; font-size: small;">Next, you can call in ADO.NET. </span></span></p>
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;">
<p class="MsoListParagraph" style="margin: 0cm 0cm 0pt 18pt; text-indent: -18pt;"><span lang="EN-US"><span style="font-family: Calibri; font-size: small;"><strong><span style="text-decoration: underline;">ADO.NET C# code</span></strong></span></span></p>
<table class="MsoTableGrid" style="border: medium none ; background: #eeece1 none repeat scroll 0% 0%; border-collapse: collapse; -moz-background-clip: border; -moz-background-origin: padding; -moz-background-inline-policy: continuous;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="border: medium none black; padding: 0in 5.4pt; background-color: transparent; width: 6.15in;" width="900" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">using</span><span style="font-family: 'Courier New'; font-size: 8pt;"> System;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">using</span><span style="font-family: 'Courier New'; font-size: 8pt;"> System.Collections.Generic;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">using</span><span style="font-family: 'Courier New'; font-size: 8pt;"> System.Text;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">using</span><span style="font-family: 'Courier New'; font-size: 8pt;"> System.Data.SqlClient;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; color: blue; font-size: 8pt;">namespace</span><span style="font-family: 'Courier New'; font-size: 8pt;"> ConsoleApplication4</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;">{</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">class</span> <span style="color: #2b91af;">Program</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>{</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: green;">// NOTICE: You MUST replace &#8216;localhost\baligoal&#8217; with your own DB instance name</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">const</span> <span style="color: blue;">string</span> ConnString = <span style="color: #a31515;">@&#8221;Data Source=localhost\baligoal;Initial Catalog=SpTestDB;Integrated Security=True&#8221;</span>;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;summary&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: gray;">///</span><span style="color: green;"> Write a record to DB with stored procedure &#8220;WriteData&#8221;,</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: gray;">///</span><span style="color: green;"> and then read it out with stored procedure &#8220;ReadData&#8221;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;/summary&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: gray;">///</span><span style="color: green;"> </span><span style="color: gray;">&lt;param name=&#8221;args&#8221;&gt;&lt;/param&gt;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">static</span> <span style="color: blue;">void</span> Main(<span style="color: blue;">string</span>[] args)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>{</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">const</span> <span style="color: blue;">string</span> TestID = <span style="color: #a31515;">&#8220;firstid&#8221;</span>;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">const</span> <span style="color: blue;">int</span> TestValue = 500;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: green;">// Firstly, write a record with store procedure</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span> </span><span style="color: blue;">using</span> (<span style="color: #2b91af;">SqlConnection</span> conn = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlConnection</span>(ConnString))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>{</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: green;">// Specify &#8216;WriteData&#8217; procedure in the params</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">using</span> (<span style="color: #2b91af;">SqlCommand</span> cmd = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlCommand</span>(<span style="color: #a31515;">&#8220;WriteData&#8221;</span>, conn))</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>{</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>cmd.CommandType = System.Data.<span style="color: #2b91af;">CommandType</span>.StoredProcedure;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: green;">// The param names are exactly the same with SP WriteData&#8217;s</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>cmd.Parameters.AddWithValue(<span style="color: #a31515;">&#8220;@id&#8221;</span>, TestID);</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>cmd.Parameters.AddWithValue(<span style="color: #a31515;">&#8220;@SomeValue&#8221;</span>, TestValue);</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>cmd.Connection.Open();</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>cmd.ExecuteNonQuery();</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: green;">// If you set BP here, and check you DB table, you should find &#8216;firstid, 500&#8242; there</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">&#8220;Write: done.&#8221;</span>);</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: green;">// Next, read it out with store procedure</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">using</span> (<span style="color: #2b91af;">SqlConnection</span> conn = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlConnection</span>(ConnString)) {</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: green;">// Specify &#8216;ReadData&#8217; procedure in the params</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">using</span> (<span style="color: #2b91af;">SqlCommand</span> cmd = <span style="color: blue;">new</span> <span style="color: #2b91af;">SqlCommand</span>(<span style="color: #a31515;">&#8220;ReadData&#8221;</span>, conn)) {</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>cmd.CommandType = System.Data.<span style="color: #2b91af;">CommandType</span>.StoredProcedure;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: green;">// The param names is exactly the same with SP ReadData&#8217;s</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>cmd.Parameters.AddWithValue(<span style="color: #a31515;">&#8220;@id&#8221;</span>, TestID);</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>cmd.Connection.Open();</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">using</span> (<span style="color: #2b91af;">SqlDataReader</span> reader = cmd.ExecuteReader())</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>{</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: blue;">if</span> (reader.Read()) {</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">&#8220;read: id &#8211; &#8220;</span> + <span style="color: #2b91af;">Convert</span>.ToString(reader[0]));</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">&#8220;read: SomeValue &#8211; &#8220;</span> + <span style="color: #2b91af;">Convert</span>.ToInt32(reader[1]));</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span style="color: green;">// End for bp</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span><span> </span><span style="color: #2b91af;">Console</span>.WriteLine(<span style="color: #a31515;">&#8220;exit&#8221;</span>);</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;"><span> </span>}</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 8pt;">}</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; line-height: normal;"><span style="font-family: 'Courier New'; font-size: 10pt;"> </span></p>
</td>
</tr>
</tbody>
</table>
<p>That is it.</p>
]]></content:encoded>
			<wfw:commentRss>http://libaocun.com/calling-sql-server-stored-procedures-with-ado-net-in-5-minutes/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding column to existing table and populate with default value</title>
		<link>http://libaocun.com/adding-column-to-existing-table-and-populate-with-default-value</link>
		<comments>http://libaocun.com/adding-column-to-existing-table-and-populate-with-default-value#comments</comments>
		<pubDate>Wed, 29 Jul 2009 03:19:59 +0000</pubDate>
		<dc:creator>Bali</dc:creator>
				<category><![CDATA[一些老文章]]></category>
		<category><![CDATA[ACT]]></category>
		<category><![CDATA[db]]></category>

		<guid isPermaLink="false">http://www.balionweb.com/?p=28</guid>
		<description><![CDATA[From time to time, we run into the scenarios of adding new columns to existing table with millions of records to meet emerging business needs. And these new columns often need initialized with default value. In this post, I’d like to illustrate my solution for such problems. Let us assume, Database: TestDB Existing table: TestTable [...]]]></description>
			<content:encoded><![CDATA[<p>From time to time, we run into the scenarios of adding new columns  to existing table with millions of records to meet emerging business needs. And  these new columns often need initialized with default value. In this post, I’d  like to illustrate my solution for such problems. Let us  assume,</p>
<p>Database:  TestDB</p>
<p>Existing table:  TestTable</p>
<p>Existing columns: ID,  C1, C2</p>
<p>And need to add,</p>
<p>Column name:  “NewColumn”</p>
<p>Default  value=”0”</p>
<p>SQLType=”INT”</p>
<p>Most simple solution would be:</p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="568" valign="top">
<p align="left">USE TestDB;</p>
<p align="left">GO</p>
<p align="left">
<p align="left">&#8211; Add &#8216;NewColumn&#8217; column and poplulate this column as  0</p>
<p align="left">ALTER TABLE TestTable ADD NewColumn INT;</p>
<p align="left">GO</p>
<p align="left">UPDATE TestTable SET NewColumn = 0;</p>
<p align="left">GO</p>
</td>
</tr>
</tbody>
</table>
<p>Often things are not that simple. We often get other questions  regarding this problem. Hereby I list them as FAQs.</p>
<p align="left"><strong><em>Q1</em></strong><em>: My existing table is very complicated with lots of foreign keys and  indexes. Does this solution work for  that?</em></p>
<p><strong>A1</strong>: Maybe not. I strongly recommend you  solve this by creating a new table with the correct order, and copy all the  records from the existing one to the new one.</p>
<p align="left"><strong><em>Q2</em></strong><em>: Your solution will put newly-inserted columns to the last. Can we  put them to certain specific places? I am asking because our business rule needs  keep audit column in the end.</em></p>
<p><strong>A2</strong>: Unfortunately, there is no quick way  to add them in a specific place in the column order in T-SQL. One thing worth  mentioning is that column order does not matter because you can select the  columns in any order you want.</p>
<p align="left"><strong><em>Q3</em></strong><em>: Your solution looks fine for me, but I get hundreds of tables to do  the same thing. Can I put this into a stored  procedure?</em></p>
<p><strong>A3</strong>: Yes and No. Alter table can’t take  dynamic parameters. So it is impossible to create SP such  as:</p>
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td width="568" valign="top">
<p align="left">&#8211; INCORRECT SAMPLE. DO NOT TAKE IT.</p>
<p align="left">CREATE PROCEDURE sp_addcolumn</p>
<p align="left">@tablename varchar(50) = 0,</p>
<p align="left">@columnname varchar(50) = 0,</p>
<p align="left">@datatype varchar (50) =  0</p>
<p align="left">AS</p>
<p align="left">ALTER TABLE  @tablename</p>
<p align="left">ADD COLUMN  @columnname @datatype</p>
<p>END</td>
</tr>
</tbody>
</table>
<p>However we can do it  in another way like this:</p>
<table border="0" cellspacing="0" cellpadding="0" width="707">
<tbody>
<tr>
<td width="707" valign="top">
<p align="left">&#8211; Correct sample</p>
<p align="left">CREATE PROCEDURE sp_addcolumn</p>
<p align="left">@tableName VARCHAR(50) = 0,</p>
<p align="left">@colName VARCHAR(50) = 0,</p>
<p align="left">@dataType VARCHAR (50) =  0</p>
<p align="left">AS</p>
<p align="left">DECLARE @tsql VARCHAR (200)</p>
<p align="left">SET @tsql = &#8216;ALTER TABLE &#8216; +  @tableName + &#8216; ADD  &#8216; + @colName + &#8216; &#8216; + @dataType</p>
<p align="left">EXEC(@tsql)</p>
<p align="left">SET @tsql = &#8216;UPDATE &#8216; +  @tableName + &#8216; SET  &#8216; + @colName + &#8216;=  0&#8242;</p>
<p align="left">EXEC(@tsql)</p>
<p align="left">GO</p>
<p align="left">
<p align="left">&#8211; The way to use the SP is as followings:</p>
<p>&#8211; EXEC sp_addcolumn &#8216;TestTable&#8217;, &#8216;NewColumn&#8217;,  &#8216;INT&#8217;</td>
</tr>
</tbody>
</table>
]]></content:encoded>
			<wfw:commentRss>http://libaocun.com/adding-column-to-existing-table-and-populate-with-default-value/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

