存储过程EF和NET CORE [英] Stored procedures EF and NET CORE
问题描述
我正在构建一个WEB API以在.net core中生成JSON对象
I am building a WEB API to generate JSON objects in .net core
问题是数据集是在SQL存储过程中生成的(使用动态SQL),我不知道返回的对象类型,因此我可以将其映射到具体模型,因为输出列根据参数而变化
The thing is the data sets are generated in SQL stored procedures (using dynamic SQL) and i dont know the type of objects that are returned so i can map it to a concrete model, since the output columns change depending on the parameters.
在使用或不使用EF的情况下,是否有人知道如何从net core 1.0中的BD检索数据集?
Does any one know ho to retrive the data set from the BD in net core 1.0 with or without using EF?
浏览量很大,只能找到使用模型的答案
Browsed a lot and can only find ansers that use models
预先感谢
推荐答案
您可以在project.json文件中为您的项目添加以下依赖项:
You can add the following dependencies for your project in project.json file:
- System.Data.Common
- System.Data.SqlClient
如您在下一张图片中所见:
As you can see in the next image:
重建项目,您可以编写如下代码:
Rebuild your project and you can code something like this:
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Dynamic;
namespace ConsoleApp1
{
public class Program
{
public static IEnumerable<dynamic> GetData(String cmdText)
{
using (var connection = new SqlConnection("server=(local);database=Northwind;integrated security=yes;"))
{
connection.Open();
using (var command = new SqlCommand(cmdText, connection))
{
using (var dataReader = command.ExecuteReader())
{
var fields = new List<String>();
for (var i = 0; i < dataReader.FieldCount; i++)
{
fields.Add(dataReader.GetName(i));
}
while (dataReader.Read())
{
var item = new ExpandoObject() as IDictionary<String, Object>;
for (var i = 0; i < fields.Count; i++)
{
item.Add(fields[i], dataReader[fields[i]]);
}
yield return item;
}
}
}
}
}
public static void Main(String[] args)
{
foreach (dynamic row in GetData("select * from Shippers"))
{
Console.WriteLine("Company name: {0}", row.CompanyName);
Console.WriteLine();
}
Console.ReadKey();
}
}
}
请告诉我这是否有用.
这篇关于存储过程EF和NET CORE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!