Microsoft KB Archive/310130

= How To Execute SQL Parameterized Stored Procedures by Using the ODBC .NET Provider and Visual C# .NET =

Article ID: 310130

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q310130



For a Microsoft Visual Basic .NET version of this article, see 309486.

For a Microsoft Visual C++ .NET version of this article, see 310142.

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * Microsoft.Data.Odbc

IN THIS TASK
SUMMARY
 * Call Syntax Examples
 * Test Project - Single Input Parameter
 * Test Project - Multiple Parameter Types
 * Troubleshooting

REFERENCES



SUMMARY
This step-by-step describes how to call a parameterized SQL Server stored procedure using the ODBC .NET Managed Provider and Visual C# .NET.

While executing a parameterized stored procedure using the ODBC .NET Provider is little different from executing the same procedure using the SQL or the OLE DB Provider, there is one important difference: the stored procedure must be called using the ODBC CALL syntax rather than the name of the stored procedure. For additional information on this CALL syntax, see the topic &quot;Procedure Calls&quot; in the ODBC Programmer's Reference in the MSDN Library.

back to the top

Call Syntax Examples
  Here is an example of the call syntax for an actual stored procedure in the Northwind sample database that expects one input parameter: {CALL CustOrderHist (?)}   Here is an example of the call syntax for a stored procedure that expects one input parameter and returns one output parameter and a return value. The first placeholder represents the return value: {? = CALL Procedure1 (?, ?)  The ODBC .NET Managed Provider, like the OLE DB Provider, processes parameters by ordinal position (zero-based) and not by name.

back to the top

Test Project - Single Input Parameter
 If you have not already done so, download and install the ODBC .NET Managed Provider from the following Microsoft Web site:

http://msdn.microsoft.com/dataaccess

 Start Visual Studio .NET, and then create a new Visual C# .NET Windows Application with a name of your choosing. On the Project menu, click Add reference, and then double-click Microsoft.Data.ODBC.dll to add it to the selected items list. Close the References dialog box.</li>  At very top of the code window, add the following statements: using System.Data; using Microsoft.Data.Odbc; </li> Drag a Button control from the toolbox to the default form.</li>  Double-click the command button to switch to the code window for the button's Click event. Enter or paste the code below in the Click event procedure, modifying the SQL Server connection string as necessary: OdbcConnection cn; OdbcCommand cmd; OdbcParameter prm; OdbcDataReader dr;

try { //Change the connection string to use your SQL Server. cn = new OdbcConnection(&quot;Driver={SQL Server};Server=servername;Database=Northwind;Trusted_Connection=Yes&quot;);

//Use ODBC call syntax. cmd = new OdbcCommand(&quot;{call CustOrderHist (?)}&quot;, cn);

prm = cmd.Parameters.Add(&quot;@CustomerID&quot;, OdbcType.Char, 5); prm.Value = &quot;ALFKI&quot;;

cn.Open;

dr = cmd.ExecuteReader;

//List each product. while (dr.Read) Console.WriteLine(dr.GetString(0));

//Clean up. dr.Close; cn.Close; } catch (OdbcException o) { MessageBox.Show(o.Message.ToString); }                   </li> Run the project. This code calls the &quot;CustOrderHist&quot; stored procedure, passing in the CustomerID as a single input parameter and returns a resultset. In the Output Window, you should see the list of products ordered by Northwind customer ALFKI.

NOTE: Press CTRL+ALT+O to open the Output Window.</li></ol>

back to the top

Test Project - Multiple Parameter Types
  Using Query Analyzer, create the following stored procedure in the Northwind sample database. This stored procedure accepts a CustomerID as an input parameter and returns a list of orders placed by the customer, returns the average freight per order paid by that customer as an output parameter, and returns the number of orders placed by the customer as a return value. CREATE PROCEDURE usp_TestParameters @CustID CHAR(5), @AvgFreight MONEY OUTPUT AS SELECT @AvgFreight = AVG(Freight) FROM Orders WHERE CustomerID = @CustID SELECT * FROM Orders WHERE CustomerID = @CustID RETURN @@ROWCOUNT </li>  Repeat steps 1 through 6 above, substituting the following code in the Click event procedure of the command button: OdbcConnection cn;

try { cn = new OdbcConnection(&quot;Driver={SQL Server};Server=servername;Database=Northwind;Trusted_Connection=Yes&quot;);

OdbcCommand cmd = new OdbcCommand(&quot;{? = call usp_TestParameters (?, ?)}&quot;, cn);

OdbcParameter prm = cmd.Parameters.Add(&quot;@RETURN_VALUE&quot;, OdbcType.Int); prm.Direction = ParameterDirection.ReturnValue;

prm = cmd.Parameters.Add(&quot;@CustomerID&quot;, OdbcType.Char, 5); prm.Value = &quot;ALFKI&quot;;

prm = cmd.Parameters.Add(&quot;@AvgFreight&quot;, OdbcType.Double); prm.Direction = ParameterDirection.Output;

cn.Open; OdbcDataReader dr = cmd.ExecuteReader;

while (dr.Read) Console.WriteLine(dr.GetString(0));

dr.Close; cn.Close;

Console.WriteLine(&quot;Average Freight (output param): {0}&quot;, cmd.Parameters[2].Value); Console.WriteLine(&quot;Order Count (return value): {0}&quot;, cmd.Parameters[0].Value);

}

catch (OdbcException o) { MessageBox.Show(o.Message.ToString); }                   </li> Run the project. This code calls the &quot;usp_TestParameters&quot; stored procedure that we created in step 1 above, passing in the CustomerID as a single input parameter, and returns a resultset, an output parameter, and a return value. In the Output window, you should see the list of orders placed by Northwind customer ALFKI, the average freight the customer paid per order, and the count of orders.

NOTE: Press CTRL+ALT+O to open the Output window.</li></ol>

back to the top

Troubleshooting

 * The ADO syntax that is usually used to call stored procedures, where the name of the procedure alone is provided as the CommandText, cannot be used with the ODBC .NET Managed Provider.
 * When a stored procedure returns a resultset, the output parameter(s) and return value are not available until the resultset has been accessed and closed. For example, if we omitted the line &quot;dr.Close&quot; in the second sample above, we would be unable to retrieve the values for the output parameter and the return value.
 * The ODBC .NET Managed Provider, like the OLE DB Provider, processes parameters by ordinal position (zero-based) and not by name.
 * The ODBC .NET Managed Provider does not ship with Visual Studio .NET, but must be downloaded separately.

back to the top

<div class="references_section">