数据库中的数据集为空 [英] Dataset from database is empty
问题描述
这是我要做的:
IQueryable< NorthwindDataSet.OrdersRow> LookupOrdersForYear(int year)
{
using(var context = new NorthwindDataSet())
{
var orders =
from context.Orders
其中order.OrderDate!= null&&& order.OrderDate.Year> =年
选择订单;
return orders.ToList()。AsQueryable();
}
}
我发现
是空的,所以我添加了
Console.WriteLine(context.Orders.Count);
这给我的输出为0.出了什么问题?
我发现我需要用数据填充数据集,否则会保持为空。
SqlDataAdapter做了一个诀窍:
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(SELECT * FROM Orders);
string connString = @Data Source = localhost\SQLEXPRESS; Initial Catalog = Northwind; Integrated Security = True;
SqlConnection conn = new SqlConnection(connString);
adapter.SelectCommand = command;
adapter.SelectCommand.Connection = conn;
adapter.Fill(context.Orders);
SqlDataAdapter.Fill有几种重载方法,其中一个采用数据集,另一个采用数据。当我第一次使用SqlDataAdapter时,我使用了一个数据集
adapter.Fill(context);
其中还给了我一个空的context.Orders。正确的一个需要一个数据表:
adapter.Fill(context.Orders);
这个工作并按预期返回日期。
I'm accessing a table from my database. I have added the tables as a dataset in Visual Studio 2013, but when I try to use it, it comes out empty.
This is what I'm trying to do:
IQueryable<NorthwindDataSet.OrdersRow> LookupOrdersForYear(int year)
{
using (var context = new NorthwindDataSet())
{
var orders =
from order in context.Orders
where order.OrderDate != null && order.OrderDate.Year >= year
select order;
return orders.ToList().AsQueryable();
}
}
I found out that orders
was empty, so I added
Console.WriteLine(context.Orders.Count);
which gave me an output of 0. What went wrong?
I found out that I needed to fill my dataset with data, or it would stay empty.
A SqlDataAdapter did the trick:
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand("SELECT * FROM Orders");
string connString = @"Data Source=localhost\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True";
SqlConnection conn = new SqlConnection(connString);
adapter.SelectCommand = command;
adapter.SelectCommand.Connection = conn;
adapter.Fill(context.Orders);
There are several overloaded methods for SqlDataAdapter.Fill, of which one takes a dataset and another takes a datatable. When I first used the SqlDataAdapter, I used the one that takes a dataset
adapter.Fill(context);
which still gave me an empty context.Orders. The correct one takes a datatable:
adapter.Fill(context.Orders);
This one worked and returned the dates as expected.
这篇关于数据库中的数据集为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!