15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

968 ❘ ChaPTer 34 .net prOGrAmminG with sQl server<br />

SqlComm<strong>and</strong> comm<strong>and</strong> = new SqlComm<strong>and</strong>();<br />

comm<strong>and</strong>.Connection = connection;<br />

comm<strong>and</strong>.Comm<strong>and</strong>Text = "SELECT SalesOrderID, OrderDate, DueDate, " +<br />

"ShipDate " +<br />

"FROM Sales.SalesOrderHeader " +<br />

"WHERE (CustomerID = @CustomerID)" +<br />

"ORDER BY SalesOrderID";<br />

comm<strong>and</strong>.Parameters.Add("@CustomerID", SqlDbType.Int);<br />

comm<strong>and</strong>.Parameters["@CustomerID"].Value = customerId;<br />

};<br />

}<br />

SqlDataReader reader = comm<strong>and</strong>.ExecuteReader();<br />

SqlPipe pipe = SqlContext.Pipe;<br />

pipe.Send(reader);<br />

connection.Close();<br />

code snippet SqlSamplesUsingAdventureWorks/GetCustomerOrdersCLR.cs<br />

CLR stored procedures are deployed to SQL Server either using Visual Studio or with the CREATE<br />

PROCEDURE statement. With this SQL statement, the parameters of the stored procedure are defined, as well<br />

as the name of the assembly, class, <strong>and</strong> method:<br />

CREATE PROCEDURE GetCustomerOrdersCLR<br />

(<br />

@CustomerID nchar(5)<br />

)<br />

AS EXTERNAL NAME Demo.StoredProcedures.GetCustomerOrdersCLR<br />

using stored Procedures<br />

The CLR stored procedure can be invoked just like a T-SQL stored procedure by using classes from the<br />

namespace System.Data.SqlClient. First, a SqlConnection object is created. The CreateComm<strong>and</strong>()<br />

method returns a SqlComm<strong>and</strong> object. With the comm<strong>and</strong> object, the name of the stored procedure<br />

GetCustomerOrdersCLR is set to the Comm<strong>and</strong>Text property. As with all stored procedures, the<br />

Comm<strong>and</strong>Type property must be set to Comm<strong>and</strong>Type.StoredProcedure. The method ExecuteReader()<br />

returns a SqlDataReader object to read record by record:<br />

using System;<br />

using System.Data;<br />

using System.Data.SqlClient;<br />

//...<br />

string connectionString =<br />

@"server=(local);database=AdventureWorks;trusted_connection=true";<br />

var connection = new SqlConnection(connectionString);<br />

SqlComm<strong>and</strong> comm<strong>and</strong> = connection.CreateComm<strong>and</strong>();<br />

comm<strong>and</strong>.Comm<strong>and</strong>Text = "GetCustomerOrdersCLR";<br />

comm<strong>and</strong>.Comm<strong>and</strong>Type = Comm<strong>and</strong>Type.StoredProcedure;<br />

var param = new SqlParameter("@customerId", 3);<br />

comm<strong>and</strong>.Parameters.Add(param);<br />

connection.Open();<br />

SqlDataReader reader =<br />

comm<strong>and</strong>.ExecuteReader(Comm<strong>and</strong>Behavior.CloseConnection);<br />

while (reader.Read())<br />

{<br />

Console.WriteLine("{0} {1:d}", reader["SalesOrderID"], reader["OrderDate"]);<br />

}<br />

reader.Close();<br />

code snippet UsingSP/Program.cs<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!