如何与网站(ASP.NET)中的存储过程集成

安德里安·亚历山大(Andrian Alexander)

首先,我创建了与数据库集成的类(SQL Server),然后我想与我在SQL Server中创建的存储过程进行连接(创建存储过程的目的是使我们的网站更加高效,而不是比使用select语句)

public class TicketTypeDB
{
    public static string conStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

    public static List<TicketType> getTicketByCountry(string country)
    {
        SqlConnection con = new SqlConnection(conStr);

        try
        {
            SqlCommand command = new SqlCommand("usp_getTicket");
            con.Open();

            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.CommandText = "usp_getTicket";
            command.Connection = new SqlConnection(conStr);

            command.Parameters.Add(new SqlParameter("@country", System.Data.SqlDbType.VarChar, 50, "country"));
            command.Connection = con;

            int i = command.ExecuteNonQuery();
        }
        finally
        {
            con.Close();
        }
    }
}

这是我的存储过程代码,在SSMS中可以正常工作

create procedure usp_getTicket
    (@country varchar(50))
as
begin
    select 
        TicketType.type, TicketType.description, TicketType.price, 
        Attraction.country 
    from 
        TicketType 
    inner join 
        Attraction on TicketType.orgEmail = Attraction.orgEmail
    where 
        country = @country
end

exec usp_getTicket 'singapore';

这是TicketType类

public class TicketType
{
    public string TicketID { get; set; }
    public string Type { get; set; }
    public string Description { get; set; }
    public double Price { get; set; }
    public Attraction Attraction { get; set; }
    public List<Attraction> attraction { get; set; }
    public TicketType()
    {
        attraction = new List<Attraction>();
    }
    public override string ToString()
    {
        return TicketID + " " + Type + " " + Description + " " + Price;
    }
}
乔尔·科洪

我创建存储过程的目的是使我们的网站效率更高,而不是使用select语句

这是一个谬论。SELECT语句的效率不比存储过程低。以前很久以前就是这样,但是今天使用参数化查询,无论哪种方式,您都可以获得相同的性能。

无论如何,问题在于方法要求返回a List<TicketType>,但不符合承诺。根本没有return声明。

我建议这种模式:

public class TicketTypeDB
{
    //Make this PRIVATE, which will ensure ALL db access goes through this class
    private static string conStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

    public static IEnumerable<TicketType> getTicketByCountry(string country)
    {
        using (var con = new SqlConnection(conStr))
        using (var command = new SqlCommand("usp_getTicket", con))
        {
            command.CommandType = System.Data.CommandType.StoredProcedure;
            command.Parameters.Add("@country", System.Data.SqlDbType.VarChar, 50).Value = country;
            using (var reader = command.ExecuteDataReader())
            {
                while (reader.Read())
                {
                    yield return new TicketType() {
                         //The `TicketType` class in the question isn't shared, so I have to guess here.
                         //Assign fields to that class based on reader columns. Example:

                         //Type = reader["type"],
                         //Description = reader["description"],
                         //Price = reader["Price"],
                         //Country = reader["country"]
                    };
                }
                reader.Close();
            }
        }
    }
}

如果您确实需要一个列表(提示:通常不需要),则可以将其追加.ToList()到调用此方法的任何位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在ASP .NET Visual Studio中使用SAS存储过程。SAS集成技术?

如何将存储过程存储在asp.net System.Web.Caching中的缓存中

如何从asp网站的类中获取页面

在 ASP.NET Core 3.1/5.0 中调用存储过程

在ASP .NET中通过C#获取存储过程脚本

在ASP.NET MVC中显示存储过程选择结果

使用ASP.NET中的存储过程进行更新

SSMS 和 ASP.NET 中存储过程的不同输出

如何从oracle存储过程值中的数据读取到asp.net中的dropdownlist中

如何在ASP.NET Core 3.0中的存储过程中添加参数

存储过程:如何在EF和ASP.NET MVC中返回小数

如何在ASP.Net MVC(C#)中调用和执行存储过程

如何在C#(ASP.Net)中调用参数化存储过程?

使用存储过程时如何在asp.net中执行回滚

如何使用实体框架在 ASP.NET MVC 中调用存储过程

如何从 ASP.NET 中的 Mysql 存储过程调用输出参数

如何将整个 http URL 传递给 asp.net API 中的存储过程?

如何在我的ASP.NET网站中显示其他网站RSS feed?

如何在asp.net MVC网站中集成Bing Cognitive Web Search API

如何将 mikrotik 路由器与 asp.net 网站集成?

如何在asp.net网站上加载文件

如何取消部署asp.net 2.0网站

如何使用asp.net.core从spa网站读取html

如何在asp.net网站上添加“服务参考”?

ASP.NET如何调用此存储过程

如何从ASP.NET Web API执行(调用)存储过程?

ASP.NET/MySQL SQLDataSource存储过程

如何知道何时在ASP.NET中完成将多个文件上传到Azure存储Blob的过程

如何在Linux(cntose)服务器上将我的asp.net mvc网站托管到该网站或将此网站代码转换为asp.net核心