我们的系统通常会调用存储过程,这些过程会输出多个表的结果。以前我们使用XML输出来获取每个表并使用XSLT正确关联它们。如果我使用ASP.NET MVC和LINQ调用存储过程,我如何获取每个表,然后根据需要输出数据?
答案 0 :(得分:3)
这里有一篇关于LINQ to SQL和存储过程的文章,特别是“从SPROC处理多个结果形状”一节: LINQ to SQL - Retrieving Data Using Stored Procedures
这对你的情况有用吗?
否则,不使用LINQ to SQL,可以使用SqlDataReader
的{{1}}来查看结果,例如:
NextResult
编辑:如何处理不容易适合域模型对象的自定义数据组合的示例;在这种情况下,为订单检索订单和客户:
IList<Employee> employees = new List<Employee>();
IList<Customer> customers = new List<Customer>();
using (SqlConnection connection = new SqlConnection
(Properties.Settings.Default.NorthwindConnectionString))
using (SqlCommand command = new SqlCommand
("GetEmployeesAndCustomers", connection))
{
command.CommandType = CommandType.StoredProcedure;
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
Employee e = new Employee{EmployeeID = (int)reader["EmployeeID"]};
employees.Add(e);
}
reader.NextResult();
while (reader.Read())
{
Customer c = new Customer{CustomerID = (string)reader["CustomerID"]};
customers.Add(c);
}
}
}
对此的启示:在chapter约NerdDinner中,Scott Guthrie谈到创建自定义“ViewModel”对象以保存数据,例如不容易适合域模型对象的连接。 / p>
答案 1 :(得分:3)
this article here解释了一切。这与我在previous SO question中链接的文章相同。