Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

baskar
More...

Resources » Code Snippets » ADO.NET

Use of SQL stored procedure in ADO.NET code


Posted Date:     Category: ADO.NET    
Author: Member Level: Gold    Points: 10



 


Here i am share an code snippet for using SQL Stored Procedure in ADO.NET code.
I write an used this code in my shopping cart application.


public static Product GetAProduct(int productid)
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("GetAProduct", connection))
{
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(new SqlParameter("@ProductID", productid));
connection.Open();
Product temp;
using (SqlDataReader reader = command.ExecuteReader())
{
reader.Read();
temp = new Product(
(int)reader["ProductID"],
(int)reader["CategoryID"],
(int)reader["ManufacturerID"],
(string)reader["Name"],
(int)reader["Weight"],
(int)reader["Price"],
(string)reader["Description"]);
}
return temp;
}
}
}


All the code is same is we use in simple SQL query only difference is that we set sql command type to StoredProcedure in statement below

command.CommandType = CommandType.StoredProcedure;

one mere thing to be noted is that Connection string is retrieved from .config file of the project in very first line of the code.

As I have user Stored Procedure named GetAProducts, we should write code for this stored procedurt in SQL database Stored Procedure. which is as follows.


ALTER PROCEDURE GetAProduct
@ProductID int
AS
SELECT * FROM [Products]
WHERE [Products].[ProductID] = @ProductID
RETURN


In the code snippet above first CREATE keyword is used in place of ALTER to create the stored procedure, as soon as we save this stored procedure it is converted to ALTER.

If we want to make any change in this Stored Procedure then this ALTER keyword is used as it is.

Than you.
Happy Programming.





Did you like this resource? Share it with your friends and show your love!


Responses to "Use of SQL stored procedure in ADO.NET code"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Use ExecuteScalar for get single data
    Previous Resource: Access any Stored Procedure with or without parameter
    Return to Resources
    Post New Resource
    Category: ADO.NET


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    ADO.Net  .  SQL  .  Stored Procedure  .  
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.