illustrates how to call a SQL Server stored procedure : Store Procedure « Database ADO.net « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Database ADO.net » Store ProcedureScreenshots 
illustrates how to call a SQL Server stored procedure

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example23_7.cs illustrates how to call a SQL Server
  stored procedure
*/

using System;
using System.Data;
using System.Data.SqlClient;

public class Example23_7
{

  public static void Main()
  {

    // formulate a string containing the details of the
    // database connection
    string connectionString =
      "server=localhost;database=Northwind;uid=sa;pwd=sa";

    // create a SqlConnection object to connect to the
    // database, passing the connection string to the constructor
    SqlConnection mySqlConnection =
      new SqlConnection(connectionString);

    // formulate a string containing the name of the
    // stored procedure
    string procedureString =
      "Ten Most Expensive Products";

    // create a SqlCommand object to hold the SQL statement
    SqlCommand mySqlCommand = mySqlConnection.CreateCommand();

    // set the CommandText property of the SqlCommand object to
    // procedureString
    mySqlCommand.CommandText = procedureString;

    // set the CommandType property of the SqlCommand object
    // to CommandType.StoredProcedure
    mySqlCommand.CommandType = CommandType.StoredProcedure;

    // open the database connection using the
    // Open() method of the SqlConnection object
    mySqlConnection.Open();

    // run the stored procedure
    mySqlCommand.ExecuteNonQuery();

    // create a SqlDataAdapter object
    SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();

    // set the SelectCommand property of the SqlAdapter object
    // to the SqlCommand object
    mySqlDataAdapter.SelectCommand = mySqlCommand;

    // create a DataSet object to store the results of
    // the stored procedure call
    DataSet myDataSet = new DataSet();

    // use the Fill() method of the SqlDataAdapter object to
    // retrieve the rows from the stored procedure call,
    // storing the rows in a DataTable named Products
    mySqlDataAdapter.Fill(myDataSet, "Products");

    // display the rows in the Products DataTable
    Console.WriteLine("The ten most expensive products are:");
    DataTable products = myDataSet.Tables["Products"];
    foreach (DataRow product in products.Rows)
    {
      Console.WriteLine("Product name = " +
        product["TenMostExpensiveProducts"]);
      Console.WriteLine("Unit price = " +
        product["UnitPrice"]);

    }

    // close the database connection using the Close() method
    // of the SqlConnection object
    mySqlConnection.Close();

  }

}


           
       
Related examples in the same category
1.Add parameters to SqlCommand to call stored procedure
2.Call the SQL Server AddProduct() stored procedure with SqlCommand
3.Illustrates simple stored procedures with unnamed parameters in the queryIllustrates simple stored procedures with unnamed parameters in the query
4.Populate a DataSet object using a store procedure
5.Call the SQL Server AddProduct() store procedure
6.Get Return from SQL Server store procedure
7.Call Simple Store Procedure
8.Call a store procedure
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.