![]() |
Database »
Database »
Databases
Intermediate
Stored Procedure Invocation Code Generator for VB, C# and JScript.NETBy Simon WilsonStored Procedure Invocation Code Generator for VB, C# and JScript.NET |
C#, VB, JScript .NET, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

Detailed documentation (including a tutorial) is provided in spinvoke_v2_1.pdf which is included in the download.
SP/Invoke generates code to allow C#, VB or JScript.NET programmers to invoke SQL Server stored procedures as if they were static methods on CLR classes. It was objectnation's submission to Chris Sell's 'Spend a Day With .NET' contest where it won the 'Best Use of SQL' prize (click here for information about the winners). The version featured here includes numerous improvements which were made subsequent to the original submission.
This code serves as a useful example for the following:
For example, for the following stored procedure (taken from the Northwind SQL Server example database) ...
CREATE PROCEDURE CustOrderHist @CustomerID nchar(5)
AS
SELECT
ProductName,
Total=SUM(Quantity)
FROM
Products P,
[Order Details] OD,
Orders O,
Customers C
WHERE
C.CustomerID = @CustomerID AND
C.CustomerID = O.CustomerID AND
O.OrderID = OD.OrderID AND
OD.ProductID = P.ProductID
GROUP BY
ProductName
... define an XML file in your C#/VB project and describe the stored procedure(s) for which code should be generated:
<?xml version="1.0" encoding="UTF-8" ?>
<storedProcedures>
<server name="localhost">
<database name="Northwind">
<storedProcedureClass source="CustOrderHist">
<rowClass></rowClass>
</storedProcedureClass>
</database>
</server>
</storedProcedures>
You then associate the XML file with SP/Invoke:

SP/Invoke generates the code ...

... and adds it to the project ...

... allowing you to invoke the stored procedure using ...
SqlConnection conn = new SqlConnection("...");
conn.Open();
CustOrderHist.Result r = CustOrderHist.Invoke(conn, "OTTIK");
if (r.ReturnValue != 0)
return;
foreach (CustOrderHist.Row order in r)
{
Console.WriteLine(order.ProductName);
Console.WriteLine(order.Total);
}
SqlConnection parameter, the other with an SqlTransaction.
OUTPUT parameters. Always generates ref params (OUTPUT parameters can be in/out and out-only).
objectnation.SPInvoke.Inspector at the command-line for a description of the tool's parameters.xmlns="http://www.objectnation.com/Schemas/SPInvoke"<storedProcedureClass> and <rowClass> elements support a new attribute: access. This can be used to control the access/visibility of the generated classes. By default, the generated visibility is not public (i.e. internal for C#). A class can be made public by specifying public for the access attribute.SqlConnection, SqlCommand, SqlParameter, SqlTransaction.These classes are no longer used. Instead, the following provider-independent interfaces are used:IDbConnection, IDbCommand, IDbDataParameter, IDbTransaction.This enables the use of the OleDb or Oracle managed providers with code generated by SP/Invoke at run-time. SP/Invoke still however, only supports SQL Server at design-time.PreExecute, PostExecute, Exception.These events can be handled to add centralised diagnostic tracing, input parameter validation, SqlException-to-persistence-layer exception mapping etc. to your application.This version of SP/Invoke is provided in source-code only form. Visual Studio .NET is required to build the assemblies before use. NOTE: spinvoke_v2_1.pdf (in the downloadable .ZIP file) contains detailed instructions for compiling the source code and installing the binaries.
Check objectnation's downloads page for updates. Please send feedback and suggestions to feedback@objectnation.com
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Jun 2003 Editor: Barry Lapthorn |
Copyright 2002 by Simon Wilson Everything else Copyright © CodeProject, 1999-2009 Web18 | Advertise on the Code Project |