Saturday, December 8, 2012

Sample code to execute stored procedure from C# (ADO.NET)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleApplication2
    {
    class Program
        {
        static void Main(string[] args)
            {
            SqlDataReader rdr = null;
            var connectionString = @"server=myServer;database=myDatabase;uid=sa;pwd=mypassword";
            using (var conn = new SqlConnection(connectionString))
            using (var command = new SqlCommand("RetrieveSupplier", conn)
            {
                CommandType = CommandType.StoredProcedure

            })
      
                {
                command.Parameters.Add(new SqlParameter("@Supid",  4));
                conn.Open();
                rdr= command.ExecuteReader();
               
                while (rdr.Read())
                    {
                    Console.WriteLine(
                        "Name is {0}",
                        rdr["SupplierName"]
                );
                    }
                conn.Close();
                }
          
            Console.ReadKey();
            }
        }
    }

No comments:

Post a Comment