gpt4 book ai didi

c# - 本地数据库,我需要一些例子

转载 作者:IT王子 更新时间:2023-10-29 04:37:12 25 4
gpt4 key购买 nike

我正在制作一个将在多台计算机上安装和运行的应用程序,我的目标是制作一个与该应用程序一起安装的空本地数据库文件,当用户使用该应用程序时,他的数据库将填充来自应用程序。你能给我提供以下例子吗:

  1. 我需要做什么才能让我的应用程序连接到其本地数据库
  2. 如何使用应用程序中的变量执行查询,例如如何将以下内容添加到数据库

    String abc = "ABC";
    String BBB = "Something longer than abc";

等等编辑::我正在使用从“添加 > 新项目 > 本地数据库”创建的“本地数据库”,那么我将如何连接到它?抱歉这个愚蠢的问题..我从来没有在 .net 中使用过数据库

最佳答案

根据需要你也可以考虑使用Sql CE。我敢肯定,如果您指定了您正在考虑使用的数据库,或者您的要求(如果您确定),您将获得正确且真实的连接字符串示例等。

编辑:这是 SqlCe/Sql Compact 的代码

    public void ConnectListAndSaveSQLCompactExample()
{
// Create a connection to the file datafile.sdf in the program folder
string dbfile = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location).DirectoryName + "\\datafile.sdf";
SqlCeConnection connection = new SqlCeConnection("datasource=" + dbfile);

// Read all rows from the table test_table into a dataset (note, the adapter automatically opens the connection)
SqlCeDataAdapter adapter = new SqlCeDataAdapter("select * from test_table", connection);
DataSet data = new DataSet();
adapter.Fill(data);

// Add a row to the test_table (assume that table consists of a text column)
data.Tables[0].Rows.Add(new object[] { "New row added by code" });

// Save data back to the databasefile
adapter.Update(data);

// Close
connection.Close();
}

记得添加对System.Data.SqlServerCe的引用

关于c# - 本地数据库,我需要一些例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1121917/

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