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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1121917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:43:00  来源:igfitidea点击:

Local database, I need some examples

c#database

提问by Aviatrix

I'm making an app that will be installed and run on multiple computers, my target is to make an empty local database file that is installed with the app and when user uses the app his database to be filled with the data from the app . can you provide me with the following examples :

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

  1. what do I need to do so my app can connect to its local database
  2. how to execute a query with variables from the app for example how would you add to the database the following thing

    String abc = "ABC";
    String BBB = "Something longer than abc";
    
  1. 我需要做什么才能让我的应用程序连接到它的本地数据库
  2. 如何使用应用程序中的变量执行查询,例如您将如何将以下内容添加到数据库中

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

and etc Edit :: I am using a "local database" created from " add > new item > Local database" so how would i connect to that ? Sorry for the dumb question .. i have never used databases in .net

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

采纳答案by sindre j

Depending on the needs you could also consider Sql CE. I'm sure that if you specified the database you're thinking of using, or your requirements if you're usure you would get proper and real examples of connection strings etc.

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

Edit: Here's code for SqlCe / Sql Compact

编辑:这是 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();
    }

Remember to add a reference to System.Data.SqlServerCe

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

回答by Adrian Godong

For 1)

对于 1)

The easiest way to provide this functionality is through SQL Server Express User Instances. SQL Server Express is free, so your user does not have to pay additional license for SQL Server, and the User Instance is file-based, which suits your requirement.

提供此功能的最简单方法是通过SQL Server Express 用户实例。SQL Server Express 是免费的,因此您的用户无需为 SQL Server 支付额外的许可,并且用户实例是基于文件的,这符合您的要求。

For 2)

对于 2)

This is a big topic. You may want to go through some of the tutorials from Microsoftto get the feeling of how to connect to DB, etc.

这是一个很大的话题。您可能想通过Microsoft 的一些教程来了解如何连接到 DB 等。

回答by J. Polfer

Seems like you're:

好像你是:

  • -Making a C# app that will be installed and run on multiple computers
  • -That needs a local database (I'm assuming an RDBMS)
  • -You need to generate a blank database at installation
  • -You then need to be able to connect to the database and populate it when the app runs.
  • - 制作将在多台计算机上安装和运行的 C# 应用程序
  • - 这需要一个本地数据库(我假设一个 RDBMS)
  • - 安装时需要生成一个空白数据库
  • - 然后您需要能够连接到数据库并在应用程序运行时填充它。

In general, it seems that you need to read up on using a small database engine for applications. I'd start by checking out SQLite, especially if you need multi-OS capability (eg., your C# program will run on Microsoft's .NET Framework and Novell's Mono). There are C# wrappers for accessing the SQLite database.

一般而言,您似乎需要深入了解为应用程序使用小型数据库引擎。我首先检查SQLite,特别是如果您需要多操作系统功能(例如,您的 C# 程序将在 Microsoft 的 .NET Framework 和 Novell 的 Mono 上运行)。有用于访问 SQLite 数据库的 C# 包装器。

回答by Brad Bruce

What are you considering as a database? From what little you've provided in your question, I'd suggest SQLite.

你在考虑什么作为数据库?从你在问题中提供的很少,我建议使用 SQLite。

You can get sample code from their site Sqlite.NET

您可以从他们的站点Sqlite.NET获取示例代码

回答by flesh

Not sure I fully understand what you're asking but Sqlite is a good option for lightweight, locally deployed database persistence. Have a look here:

不确定我是否完全理解您的要求,但 Sqlite 是轻量级、本地部署的数据库持久性的不错选择。看看这里:

http://www.sqlite.org/

http://www.sqlite.org/

and here for an ADO.NET provider..

在这里为 ADO.NET 提供程序..

http://sqlite.phxsoftware.com/

http://sqlite.phxsoftware.com/

回答by STW

I'm not seeing anybody suggesting SQL Compact; it's similar to SQLite in that it doesn't require installation and tailors to the low-end database. It grew out of SQL Mobile and as such has a small footprint and limited feature-set, but if you're familiar with Microsoft's SQL offerings it should have some familiarity.

我没有看到有人建议使用 SQL Compact;它与 SQLite 的相似之处在于它不需要安装并且针对低端数据库量身定制。它源自 SQL Mobile,因此占用空间小且功能集有限,但如果您熟悉 Microsoft 的 SQL 产品,则应该对它有所了解。

SQL Express is another option, but be aware that it requires a standalone installation and is a bit beefier than you might need for an applciation's local cache. That said it's also quite a bit more powerful than SQL Compact or SQLite.

SQL Express 是另一种选择,但请注意,它需要独立安装,并且比应用程序的本地缓存可能需要的功能更强大。也就是说,它也比 SQL Compact 或 SQLite 强大得多。

回答by John Saunders

I believe this question is about the "Local Database" item template in Visual Studio:

我相信这个问题是关于 Visual Studio 中的“本地数据库”项模板:

alt text

替代文字