"how to use stored procedure in c#" Code Answer's

You're definitely familiar with the best coding language C# that developers use to develop their projects and they get all their queries like "how to use stored procedure in c#" answered properly. Developers are finding an appropriate answer about how to use stored procedure in c# related to the C# coding language. By visiting this online portal developers get answers concerning C# codes question like how to use stored procedure in c#. Enter your desired code related query in the search bar and get every piece of information about C# code related question on how to use stored procedure in c#. 

call stored proc c#

By IlluzioIlluzio on Apr 14, 2020
using (SqlConnection conn = new SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI")) {
    conn.Open();

    // 1.  create a command object identifying the stored procedure
    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
    using (SqlDataReader 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"]);
        }
    }
}

Source: stackoverflow.com

Add Comment

0

how to use stored procedure in c#

By Outstanding OstrichOutstanding Ostrich on Feb 10, 2021
using System;
using System.Data;
using System.Data.SqlClient;

class StoredProcDemo
{
	static void Main()
	{
		StoredProcDemo spd = new StoredProcDemo();

		// run a simple stored procedure
		spd.RunStoredProc();

		// run a stored procedure that takes a parameter
		spd.RunStoredProcParams();
	}

	// run a simple stored procedure
	public void RunStoredProc()
	{
		SqlConnection conn = null;
		SqlDataReader rdr  = null;

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

		try
		{
			// create and open a connection object
			conn = new 
				SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
			conn.Open();

			// 1. create a command object identifying
			// the stored procedure
			SqlCommand cmd  = new SqlCommand(
				"Ten Most Expensive Products", conn);

			// 2. set the command object so it knows
			// to execute a stored procedure
			cmd.CommandType = CommandType.StoredProcedure;

			// execute the command
			rdr = cmd.ExecuteReader();

			// iterate through results, printing each to console
			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();
			}
		}
	}

	// run a stored procedure that takes a parameter
	public void RunStoredProcParams()
	{
		SqlConnection conn = null;
		SqlDataReader rdr  = null;

		// typically obtained from user
		// input, but we take a short cut
		string custId = "FURIB";

		Console.WriteLine("\nCustomer Order History:\n");

		try
		{
			// create and open a connection object
			conn = new 
				SqlConnection("Server=(local);DataBase=Northwind;Integrated Security=SSPI");
			conn.Open();

			// 1. create a command object identifying
			// the stored procedure
			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"]);
			}
		}
		finally
		{
			if (conn != null)
			{
				conn.Close();
			}
			if (rdr != null)
			{
				rdr.Close();
			}
		}	
	}
}

Source: csharp-station.com

Add Comment

0

All those coders who are working on the C# based application and are stuck on how to use stored procedure in c# can get a collection of related answers to their query. Programmers need to enter their query on how to use stored procedure in c# related to C# code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about how to use stored procedure in c# for the programmers working on C# code while coding their module. Coders are also allowed to rectify already present answers of how to use stored procedure in c# while working on the C# language code. Developers can add up suggestions if they deem fit any other answer relating to "how to use stored procedure in c#". Visit this developer's friendly online web community, CodeProZone, and get your queries like how to use stored procedure in c# resolved professionally and stay updated to the latest C# updates. 

C# answers related to "how to use stored procedure in c#"

View All C# queries

C# queries related to "how to use stored procedure in c#"

how to use stored procedure in c# pass datatable to stored procedure c# dapper c# dapper execute stored procedure with parameters playerprefs stored at unity decimals not stored in azure tables how to use distinct in linq query in c# Bad array declarator: To declare a managed array the rank specifier precedes the variable's identifier. To declare a fixed size buffer field, use the fixed keyword before the field type. how to use monitor from system.threading in c# c# use cefcharp and selenium can? how to use open hardware monitor in c# create expression func c# for use in where clause how to use buildcontext in initstate flutter how to use javascriptexecutor for loop in selenium c# use of this in programming in c# c# how to use or operator on integers in while loop how to use a function from a scrpt of a gameobject into another class how to use json in unity More than one migrations configuration type was found in the assembly 'EFCodeFirst'. Specify the name of the one to use wpf use enum description c# xamarin forms use AssetManager to get text file how to use external resource.resx file in c# how to use animtion trigger in unity use matrix3d c# use network share file folder can you use unity for ftee should i use unity hdrp how to use hangfire in controller in .net core How to use C# to open windows explorer in “select/open file mode use different database with entitymanagerfactory how to use K2 games Games parallax background How to use multiple Commands for one ViewModel how to use input field unity

Browse Other Code Languages

CodeProZone