Click here to Skip to main content
15,944,397 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I work in visual studio 2015 c# and i need to call stored procedure from sql 2012
and make function by c# to call stored procedure and retrieve one value string
in case name = @name

ALTER PROCEDURE [dbo].[ATC_Code_ByGeneric_H]  
    @Lvl_Name nvarchar(500)  
    SELECT     Tbl_Lvl1.Code + Tbl_Lvl2.Code + Tbl_Lvl3.Code + Tbl_Lvl4.Code + Tbl_Lvl5.Code AS ATC_Code  
    FROM         Tbl_Lvl1  
                 INNER JOIN  
                 ON Tbl_Lvl1.S_ID = Tbl_Lvl2.UpLvl_ID  
                 INNER JOIN  
                 ON Tbl_Lvl2.S_ID = Tbl_Lvl3.UpLvl_ID  
                 INNER JOIN  
                 ON Tbl_Lvl3.S_ID = Tbl_Lvl4.UpLvl_ID  
                 INNER JOIN  
                 ON Tbl_Lvl4.S_ID = Tbl_Lvl5.UpLvl_ID  
    WHERE     (Tbl_Lvl5.Lvl_Name = @Lvl_Name) and Tbl_Lvl5.human=1  

Actually i need to retrieve ATC_Code in case of Lv1_Name = @Lv1_Name
How to create function return string by c# to call stored procedure ?

What I have tried:

How to make function by c# to call stored proc from sql and return string
Updated 27-Apr-17 1:51am

The "What I have tried:" section is where you should have put the C# code that you had tried.

Here is some research material from Google[^]

Do come back if you get stuck, with a specific question. Remember to include the code that you are stuck with.

Unfortunately we provide neither a code writing nor a research service in this forum.
Share this answer
string s;
using (SqlConnection con = new SqlConnection(strConnect))
    using (SqlCommand cmd = new SqlCommand("ATC_Code_ByGeneric_H", con))
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Lvl_Name", levelName);
        s = (string) com.ExecuteScalar();
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900