存储过程中的参数 [英] Parameters in Stored Procedure

查看:79
本文介绍了存储过程中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Code项目,

我正在使用3层架构在SP中仅传递2个参数(即user_name和password),并且我想为该用户使用表字段数据(即City Name).

Hello Code Project,

I''m using 3 tier architecture & passing only 2 parameter (i.e user_name and password) in SP and I want to use table field data (i.e City Name) for that user.. I want that City Name at Presentation Side, How do I achieve this ?

ALTER PROCEDURE [dbo].[LogInProcedure]
    @username nvarchar (50),
    @password nvarchar (50)
AS
    SET NOCOUNT ON;
SELECT  * FROM users
WHERE   user_username=@username AND user_password=@password



我的问题是,如何从表格到演示方获取城市名称?

在此先感谢:)



My question is, how can get the City Name from table to the Presentation Side ??

Thanks in advance :)

推荐答案

基于上面的评论和您的澄清,我将以此为答案.我不能给您这样做的代码"的答案,因为我们没有足够的信息来这样做.但是,我可以概括地回答您.

术语三层体系结构"是通用的.实现三层体系结构有多种方法.例如,您使用的是MVC,MVP,MVVM等,还是做了其他不同的事情?通常,中间层向数据访问层询问信息.然后,中间层对其进行转换并将其提供给表示层.在这种情况下,将城市信息传递到表示层的方法是让中间层传递信息.

我建议阅读有关完成此任务的不同方法.那里有一些很棒的文章,包括这篇文章,将对您有所帮助:

C#.NET中的三层体系结构 [
Based upon the comments above and your clarifications, I''m going to post this as the answer. I cannot give you a "here is the code to do it" answer because we do not have nearly enough information to do so. However, I can answer you in general terms.

The term "3-tier architecture" is generic. There are multiple ways to implement a 3-tier architechture. For example, are you using MVC, MVP, MVVM, etc. or have you done something different? In general, the middle layer asks the data access layer for information. The middle layer then transforms it and gives it to the presentation layer. In that scenario, the way you would get the city information to the presentation layer would be to have the middle layer pass it along.

I would recommend reading up on different ways to accomplish this task. There are some great articles out there, including this one, that will help:

Three Layer Architecture in C# .NET[^]


因此,代码将如下所示...您可以使用DataSet将所需的数据返回到Presentation Layer.
So, the code will go something like below...You can use DataSet to return the data you want to the Presentation Layer.
public DataSet ViewUserProfile(string userName, string password)
        {
            SqlConnection con = new SqlConnection(conStr);
            DataSet ds = new DataSet();
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Clear();
            cmd.CommandText = "proc_GetUserDetails";
            cmd.Parameters.Add(new SqlParameter("@UserName", UserName));
            cmd.Parameters.Add(new SqlParameter("@Password", Password));

            con.Open();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            con.Close();
            return ds;
        }


现在,它将以表格格式返回数据,其中包含您在存储过程中提到的所有列.
因此,您可以在前端使用下面的代码显示值...


Now, this will return data in a tabular format with all the colums you mentioned in the stored procedure.
So, in the front end you can show the values using the code below...

BAlLayer objUser = new BAlLayer();
DataSet ds = objUser.ViewUserProfile(userName, password);

txtCityName.Value = ds.Tables[0].Rows[0]["CityName"].ToString();


这篇关于存储过程中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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