gpt4 book ai didi

c# - 如何在 .NET Core 中使用 .sdf 文件

转载 作者:太空宇宙 更新时间:2023-11-03 11:58:10 25 4
gpt4 key购买 nike

在.NET 框架中,我们可以使用System.Data.Linq.DataContext 加载.sdf 文件。但这在 .NET Core

中不可用
public partial class Northwind : System.Data.Linq.DataContext
{
private static System.Data.Linq.Mapping.MappingSource mappingSource = new AttributeMappingSource();
....
public Northwind(string connection) :
base(connection, mappingSource)
{
OnCreated();
}

.....
public System.Data.Linq.Table<Categories> Categories
{
get
{
return this.GetTable<Categories>();
}
}

public System.Data.Linq.Table<Customers> Customers
{
get
{
return this.GetTable<Customers>();
}
}

public System.Data.Linq.Table<Employees> Employees
{
get
{
return this.GetTable<Employees>();
}
}

public System.Data.Linq.Table<OrderDetails> OrderDetails
{
get
{
return this.GetTable<OrderDetails>();
}
}

public System.Data.Linq.Table<Orders> Orders
{
get
{
return this.GetTable<Orders>();
}
}

public System.Data.Linq.Table<Products> Products
{
get
{
return this.GetTable<Products>();
}
}
}


// Getting Data
private void PopulateData()
{
Random r = new Random();

Northwind north = new Northwind(string.Format(@"Data Source= {0}",
FindFile("Northwind.sdf")));

foreach (OrderDetails orderDet in north.OrderDetails.Take(50))
{
OrderInfo orderInfo = new OrderInfo();
orderInfo.OrderID = orderDet.OrderID;
orderInfo.CustomerID = orderDet.Orders.CustomerID;
orderInfo.ProductName = orderDet.Products.ProductName;
orderInfo.UnitPrice = (double)orderDet.UnitPrice;
orderInfo.OrderDate = (DateTime)orderDet.Orders.OrderDate;
orderInfo.DeliveryDelay = (DateTime)orderDet.Orders.ShippedDate -
orderInfo.OrderDate;
orderInfo.Quantity = orderDet.Quantity;

orderInfo.ContactNumber = r.Next(999111234, 999111239).ToString();

orderInfo.ShipAddress = orderDet.Orders.ShipAddress;

_orderList.Add(orderInfo);
}
}

应用程序的数据将使用 .NET Framework 应用程序中的 DataContext 检索,现在在 .NET Core 中,我们无法加载 .sdf 文件。

我们如何在 .NET Core 中使用 .sdf 文件?

最佳答案

您可以直接使用 ADO.NET 提供程序,但仅限于 Windows。

这里有一些示例代码和提示:https://github.com/dotnet/corefx/issues/33897#issuecomment-536269132

关于c# - 如何在 .NET Core 中使用 .sdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58553065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com