如何通过C#使函数从SQL调用存储过程并返回字符串? [英] How to make function by C# to call stored proc from SQL and return string ?

查看:62
本文介绍了如何通过C#使函数从SQL调用存储过程并返回字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在visual studio 2015 c#工作,我需要从sql 2012调用存储过程

并通过c#调用函数来调用存储过程并检索一个值字符串

如果名称= @name



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)  
AS  
    SELECT     Tbl_Lvl1.Code + Tbl_Lvl2.Code + Tbl_Lvl3.Code + Tbl_Lvl4.Code + Tbl_Lvl5.Code AS ATC_Code  
    FROM         Tbl_Lvl1  
                 INNER JOIN  
                 Tbl_Lvl2  
                 ON Tbl_Lvl1.S_ID = Tbl_Lvl2.UpLvl_ID  
                 INNER JOIN  
                 Tbl_Lvl3  
                 ON Tbl_Lvl2.S_ID = Tbl_Lvl3.UpLvl_ID  
                 INNER JOIN  
                 Tbl_Lvl4  
                 ON Tbl_Lvl3.S_ID = Tbl_Lvl4.UpLvl_ID  
                 INNER JOIN  
                 Tbl_Lvl5  
                 ON Tbl_Lvl4.S_ID = Tbl_Lvl5.UpLvl_ID  
    WHERE     (Tbl_Lvl5.Lvl_Name = @Lvl_Name) and Tbl_Lvl5.human=1  
    RETURN



实际上我需要检索ATC_Co de在Lv1_Name = @ Lv1_Name的情况下

如何通过c#创建函数返回字符串来调用存储过程?



我尝试了什么:




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

推荐答案

我试过的内容:部分是您应该放置您尝试过的C#代码的地方。



以下是 Google [ ^ ]



如果您遇到问题,请回来。请记住包含您坚持使用的代码。



很遗憾,我们在此论坛中既未提供代码编写也未提供研究服务。
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.


尝试:
string s;
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("ATC_Code_ByGeneric_H", con))
        {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Lvl_Name", levelName);
        s = (string) com.ExecuteScalar();
        }
    }


这篇关于如何通过C#使函数从SQL调用存储过程并返回字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆