.NET Window Forms local database

后端 未结 3 635
粉色の甜心
粉色の甜心 2021-01-26 00:03

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).

3条回答
  •  花落未央
    2021-01-26 00:19

    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());
        }
    }
    

提交回复
热议问题