---
title: "How to execute a stored procedure within C# program"  
description: "How to execute a stored procedure within C# program"  
author: "Takeshi Okada"  
published: 2014-01-29  
updated: 2014-01-29  
canonical: https://www.mindstick.com/forum/1905/how-to-execute-a-stored-procedure-within-c-sharp-program  
category: "c#"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# How to execute a stored procedure within C# program

I want to execute this [stored procedure](https://www.mindstick.com/articles/803/using-stored-procedure-in-asp-dot-net) from a [C# program](https://www.mindstick.com/forum/156830/how-do-i-make-a-c-sharp-program-that-checks-for-given-array-by-user-which-elements-are-perfect-numbers).

I have written the following stored procedure in a SqlServer query window and saved it as stored1:

use master

go

create procedure dbo.test as

DECLARE @command as varchar(1000), @i int

SET @i = 0

WHILE @i < 5

BEGIN

Print 'I VALUE ' +CONVERT(varchar(20),@i)

EXEC(@command)

SET @i = @i + 1

END

\

using System;

using System.Collections.Generic;

using System.Text;

using System.Data;

using System.Data.SqlClient;

namespace AutomationApp

{

class Program

{

[public void](https://www.mindstick.com/interview/2715/can-we-write-static-public-void-instead-of-public-static-void) RunStoredProc()

{

[SqlConnection](https://www.mindstick.com/forum/1209/sqlconnection-sqlcommand-sqldatareader-idisposable) conn = null;

SqlDataReader rdr = null;

Console.WriteLine("\nTop 10 Most Expensive Products:\n");

try

{

conn = new SqlConnection("Server=(local);DataBase=master;[Integrated](https://www.mindstick.com/forum/33532/handler-pagehandlerfactory-integrated-has-a-bad-module-managedpipelinehandler-in-its-module-list) Security=SSPI");

conn.Open();

[SqlCommand](https://www.mindstick.com/forum/91/difference-between-sqlcommand-and-sqlcommandbuilder) cmd = new SqlCommand("dbo.test", conn);

cmd.CommandType = CommandType.StoredProcedure;

rdr = cmd.ExecuteReader();

/*while (rdr.Read())

{

Console.WriteLine(

"Product: {0,-25} Price: ${1,6:####.00}",

rdr["TenMostExpensiveProducts"],

rdr["UnitPrice"]);

}*/

}

finally

{

if (conn != null)

{

conn.Close();

}

if (rdr != null)

{

rdr.Close();

}

}

}

static void Main(string[] args)

{

Console.WriteLine("[Hello World](https://www.mindstick.com/forum/12912/how-to-show-hello-world-backbone-marionette-js)");

Program p= new Program();

p.RunStoredProc();

Console.Read();

}

}

}

This displays the exception Cannot find the stored procedure dbo.test. Do I need to provide the path? [If yes](https://www.mindstick.com/forum/159589/can-we-crash-jvm-if-yes-then-how), in which location should the [stored procedures](https://www.mindstick.com/forum/540/using-stored-procedures-with-entity-framework-in-an-asp-dot-net-application) be stored?

## Replies

### Reply by Pravesh Singh

Hi Takeshi,

SqlConnection conn = null;

SqlDataReader rdr = null;

conn = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");

conn.Open();

// 1. create a command object identifying the stored [procedure](https://www.mindstick.com/forum/32/stored-procedure-return-datatype)

SqlCommand cmd = new SqlCommand("CustOrderHist", conn);

// 2. set the command object so it knows to execute a stored procedure

cmd.CommandType = CommandType.StoredProcedure;

// 3. add parameter to command, which will be passed to the stored procedure

cmd.Parameters.Add(new SqlParameter("@CustomerID", custId));

// execute the command

rdr = cmd.ExecuteReader();

// iterate through results, printing each to console

while (rdr.Read())

{

Console.WriteLine("Product: {0,-35} Total: {1,2}",rdr["ProductName"],rdr["Total"]);

}


---

Original Source: https://www.mindstick.com/forum/1905/how-to-execute-a-stored-procedure-within-c-sharp-program

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
