在C#中执行SQL Server存储过程

fjnneemd  于 2022-11-21  发布在  C#
关注(0)|答案(2)|浏览(191)

我正在帮助用C#制作聊天机器人,但我以前从未使用过C#,请原谅我的无知。我将使用以下链接作为参考:https://jd-bots.com/2020/10/15/connect-bot-framework-to-db-part-3-read-data-from-azure-sql-database/
我想将选择查询切换到一个存储过程,它可以完成同样的事情(我们需要在SQL Server中执行一些更有意义的连接)。
下面的代码基于链接中的示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using EchoAzureDBBot.Models;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
 
namespace EchoAzureDBBot.Bots
{
    public class EchoBot : ActivityHandler
    {
        EmployeeDBContext context;
        public EmployeeDBContext Context { get { return context; } }

        public EchoBot()
        {
            context = new EmployeeDBContext();
        }

        public Employee FetchEmployeeName(string no)
        {
            Employee employee;

            try
            {
                employee = (from e in Context.Employee
                            where e.Empid == no
                            select e).FirstOrDefault();//Query for employee details with id
            }
            catch (Exception)
            {
                throw;
            }

            return employee;
        }

        protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            var empNumber = turnContext.Activity.Text;
            Employee employee = FetchEmployeeName(empNumber);
            var replyText = employee.Empid + ": " + employee.Empname;
            await turnContext.SendActivityAsync(MessageFactory.Text(replyText, replyText), cancellationToken);
        }

        protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            var welcomeText = "Hello and welcome! I have connected this bot to Azure DB."; //welcome message

            foreach (var member in membersAdded)
            {
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    await turnContext.SendActivityAsync(MessageFactory.Text(welcomeText, welcomeText), cancellationToken);
                }
            }
        }
    }
}

我们要做的是让一个存储过程执行查询。所以基本上不是:

employee = (from e in Context.Employee
            where e.Empid == emp_id //the id we pass in as a variable earlier 
            select e).FirstOrDefault();//Query for employee details with id

我们希望执行相同操作的存储过程只从查询返回EmployeeName

SqlCommand cmd  = new SqlCommand("GetEmployee", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@EmployeeID", emp_id))

using (SqlDataReader rdr = cmd.ExecuteReader()) 
{
    employee = [EmployeeName] 
}

这就是实现它的方法吗?是否有一种简单的方法,只需传入@EmployeeID参数,然后执行以下操作就可以使它工作?如果是,那么这段代码在原始代码块中的位置,即选择查询现在所在的位置?

EXEC GetEmployee @EmployeeID = emp_id;

谢谢你的帮助!

jv4diomz

jv4diomz1#

在using语句中,需要从SqlDataReader中获取值,如下所示:

employee = rdr["EmployeeName"];
zfycwa2u

zfycwa2u2#

您可以使用此存储过程,通过为Employee提供连接的员工ID,仅返回Employee名称。

CREATE PROCEDURE [GetEmployee]
        (@EmployeeID int)
        AS
        BEGIN
         
        select EMPNAME from EMPLOYEE where EMPID=@EmployeeID
         
        END

示例:

EXEC GetEmployee @EmployeeID = 1;

相关问题