سلام بر تمامی برنامه نویسان عزیز 😉 در این پست کد فراخوانی Stored Procedure و پاس دادن یک پارامتر (sql server) در سی شارپ را آماده کردم. انشاالله که این کد سی شارپ براتون مفید باشد.
کد فراخوانی Stored Procedure و پاس دادن یک پارامتر (sql server) در سی شارپ :
using System;
using System.Data;
using System.Data.SqlClient;
class MainClass
{
public static void Main()
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = @"Data Source = .\sqlexpress;Database = Northwind; Integrated Security=SSPI";
con.Open();
string category = "Seafood";
string year = "1999";
// Create and configure a new command.
using (SqlCommand com = con.CreateCommand())
{
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "SalesByCategory";
// Create a SqlParameter object for the category parameter.
com.Parameters.Add("@CategoryName", SqlDbType.NVarChar).Value =
category;
// Create a SqlParameter object for the year parameter.
com.Parameters.Add("@OrdYear", SqlDbType.NVarChar).Value = year;
// Execute the command and process the results.
using (IDataReader reader = com.ExecuteReader())
{
Console.WriteLine("Sales By Category ({0}).", year);
using System.Data;
using System.Data.SqlClient;
class MainClass
{
public static void Main()
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString = @"Data Source = .\sqlexpress;Database = Northwind; Integrated Security=SSPI";
con.Open();
string category = "Seafood";
string year = "1999";
// Create and configure a new command.
using (SqlCommand com = con.CreateCommand())
{
com.CommandType = CommandType.StoredProcedure;
com.CommandText = "SalesByCategory";
// Create a SqlParameter object for the category parameter.
com.Parameters.Add("@CategoryName", SqlDbType.NVarChar).Value =
category;
// Create a SqlParameter object for the year parameter.
com.Parameters.Add("@OrdYear", SqlDbType.NVarChar).Value = year;
// Execute the command and process the results.
using (IDataReader reader = com.ExecuteReader())
{
Console.WriteLine("Sales By Category ({0}).", year);