.NET 窗口窗体本地数据库窗体、窗口、数据库、NET

2023-09-06 04:51:53 作者:似水流年╮乱了浮尘

我刚开始编写 Windows 窗体应用程序,我想将数据存储在数据库中,而不是实际的数据库服务器中.类似于 SQLite(本地,独立于服务器).

I just started writing a Windows Forms Application and I'd like to store data in a Database but not in an actual DB Server.. something like SQLite (local, server independent).

.NET 有这样的开箱即用的东西吗?我查看了 DataSets,但我很难填充它.

Does .NET have anything like this out of the box? I looked into DataSets but I'm have a hard time populating it.

不胜感激,谢谢!

推荐答案

考虑一下 Sql Server Compact 版本,你可以找到如何将它添加到你的项目中的说明 这里.下面的示例代码从包含数据库文件 MyDb.sdf 中的列 Id 的表 MyTable 中检索数据,您还需要添加 System.Data.SqlServerCe 程序集引用和命名空间 System.Data.SqlServerCe

Consider Sql Server Compact edition, you can find instruction how to add it to your project here. Below sample code to retrieve data from the table MyTable containing column Id in database file MyDb.sdf, you also need to add System.Data.SqlServerCe assembly reference and namespace System.Data.SqlServerCe

using (SqlCeConnection ceConn = new SqlCeConnection(@"Data Source=|DataDirectory|MyDb.sdf"))
using (SqlCeCommand ceCmd = new SqlCeCommand("select Id from MyTable", ceConn))
{
    ceConn.Open();
    SqlCeDataReader dataR = ceCmd.ExecuteReader();
    while (dataR.Read())
    {
        Console.WriteLine(dataR["Id"].ToString());
    }
}