【问题标题】:Web API in ASP.net C#ASP.net C# 中的 Web API
【发布时间】:2020-04-02 11:10:01
【问题描述】:

我想创建一个 Web API 控制器,它返回带有 id 的产品详细信息。使用来自数据库类的存储过程调用,而不是数据库上下文。发送带有 ID 的请求并获得带有 Jason 值的响应。 我看到了很多示例,但它们直接使用数据库上下文中的存储过程。但是我想引入一个连接字符串并在业务类中调用该方法,而不是在业务类中调用带有响应返回的控制器。

请帮忙举个小例子

【问题讨论】:

    标签: asp.net-mvc webapi


    【解决方案1】:

    这是一个例子。您可以选择使用 SQL 命令或 SQL 适配器,尝试搜索更多有关它的信息。

    编程不仅是编写代码,也是在谷歌上搜索解决方案。

    // Setup connection string to access local SQL Server 2000
    
        string connectionString = "server=localhost;" +
        "database=Northwind;uid=sa;pwd=manager";
    
        // Instantiate the connection, passing the
        // connection string into the constructor
        SqlConnection con = new SqlConnection(connectionString);
    
        // Open the connection
        con.Open();
    
        // Create and execute the query
        SqlCommand cmd = new SqlCommand("SELECT * FROM Customers",con);
        SqlDataReader reader = cmd.ExecuteReader();
    
        // Iterate through the DataReader and display row
        while(reader.Read()) {
            Console.WriteLine("{0} - {1}",
            reader.GetString(0), reader.GetString(1));
        }
    

    Source

    【讨论】:

    • 我正在寻找一个数据层类,我可以在其中定义我的存储过程,然后我将在业务类中调用该过程,然后可以使用请求类在控制器中调用业务类并返回 Jason回复。请通过这些步骤帮助构建整个 API
    • @Zeldalink 嗨,欢迎来到 SO,您可能想查看帮助页面,特别是:stackoverflow.com/help/on-topic - 您的“问题”(实际上是要求某人为您工作的一般要求)是太宽泛了。请限制您遇到困难的特定请求。任何数量的关于如何创建多层应用程序/数据层的在线教程都可能涵盖您的“构建整个 API”要求。
    猜你喜欢
    • 2019-11-30
    • 2013-02-21
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 2021-12-17
    • 2012-03-27
    • 1970-01-01
    相关资源
    最近更新 更多