【发布时间】:2020-04-02 11:10:01
【问题描述】:
我想创建一个 Web API 控制器,它返回带有 id 的产品详细信息。使用来自数据库类的存储过程调用,而不是数据库上下文。发送带有 ID 的请求并获得带有 Jason 值的响应。 我看到了很多示例,但它们直接使用数据库上下文中的存储过程。但是我想引入一个连接字符串并在业务类中调用该方法,而不是在业务类中调用带有响应返回的控制器。
请帮忙举个小例子
【问题讨论】:
标签: asp.net-mvc webapi
我想创建一个 Web API 控制器,它返回带有 id 的产品详细信息。使用来自数据库类的存储过程调用,而不是数据库上下文。发送带有 ID 的请求并获得带有 Jason 值的响应。 我看到了很多示例,但它们直接使用数据库上下文中的存储过程。但是我想引入一个连接字符串并在业务类中调用该方法,而不是在业务类中调用带有响应返回的控制器。
请帮忙举个小例子
【问题讨论】:
标签: asp.net-mvc webapi
这是一个例子。您可以选择使用 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));
}
【讨论】: