简体   繁体   English

使用Visual Studio 2012的本地数据库

[英]Local Database with Visual Studio 2012

I am creating a application and I want to use a local database stored on the clients local machines. 我正在创建一个应用程序,我想使用存储在客户端本地计算机上的本地数据库。 I am debating over if I should use SQLITE or is there something in Visual Studio to help me. 我正在讨论是否应该使用SQLITE或者Visual Studio中有什么东西可以帮助我。 The other thing is that I want to create the database programmatically in the users directory when the application is launched. 另一件事是我想在启动应用程序时以编程方式在users目录中创建数据库。

I am see a few things online but the articles were all about SQL Server stuff and that is not want I want to do with this application. 我在网上看到了一些东西,但文章都是关于SQL Server的东西,而且我不想要这个应用程序。 All data will need to be stored on the local machine. 所有数据都需要存储在本地计算机上。

You can use SQL Server Compact , which has tooling in Visual Studio. 您可以使用SQL Server Compact ,它在Visual Studio中具有工具。 It's syntax-compatible with SQL Server, but stores its data in a local file, which you can create on the fly (at app startup, for example). 它与SQL Server的语法兼容,但将其数据存储在本地文件中,您可以动态创建(例如,在app启动时)。

You can create the SQLite database on the fly with the libraries provided from their website. 您可以使用其网站提供的库动态创建SQLite数据库。 I have used it in many projects for my personal code, as well as it being used in some of the internal architecture of Data Explorer (IBM Product). 我在许多项目中使用它来获取我的个人代码,以及它在Data Explorer(IBM产品)的某些内部体系结构中使用。 Some sample C# to create a database file: 一些示例C#创建数据库文件:

        if (!Directory.Exists(Application.StartupPath + "\\data"))
        {
            Directory.CreateDirectory(Application.StartupPath + "\\data");
        }
        SQLiteConnection conGlobal;
        if (!File.Exists(dbGlobal))
        {
            conGlobal = new SQLiteConnection("Data Source=" + dbGlobal + ";New=True;Compress=True;PRAGMA synchronous = 1;PRAGMA journal_mode=WAL");
            conGlobal.SetExtendedResultCodes(true);
            firstRun = true;
        }
        else
        {
            conGlobal = new SQLiteConnection("Data Source=" + dbGlobal + ";Compress=True;PRAGMA synchronous = 1;PRAGMA journal_mode=WAL");
            conGlobal.SetExtendedResultCodes(true);
        }
        try
        {
            conGlobal.Open();
        }
        catch (Exception)
        {
            //do stuff
        }

Simply initiating a connection to the file will create it if the new=true is passed as the connection string. 如果将new = true作为连接字符串传递,则只需启动与文件的连接即可创建它。 Then you can query it and get results just like you would any database. 然后,您可以查询它并获得与任何数据库一样的结果。

You also have the ability to password protect the database files to prevent access to them from just opening them with an SQLite-Shell or a different SQLite DB viewer. 您还可以使用SQLite-Shell或其他SQLite DB查看器密码保护数据库文件,以防止访问它们。

For more info on the pragma statements that are being passed in the connection string, see the following: http://www.sqlite.org/pragma.html 有关在连接字符串中传递的pragma语句的更多信息,请参阅以下内容: http//www.sqlite.org/pragma.html

I'm not sure about programmatically (that's probably what you meant, right?) creating the database, but SQL Server Compact Edition has served me well in the past for simple apps. 我不确定以编程方式(这可能就是你的意思,对吧?)创建数据库,但SQL Server Compact Edition过去对我来说很适合简单的应用程序。 It's embedded and even runs in medium trust. 它是嵌入式的,甚至以中等信任的方式运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2012数据库图表? - Visual Studio 2012 Database Diagram? 如何在 Microsoft Visual Studio 2012 中使用本地数据库文件添加/编辑/检索数据 - How to add/edit/retrieve data using Local Database file in Microsoft Visual Studio 2012 如何使用InstallShield LE为带有SQL Server 2012本地数据库的Visual Studio 2012 Windows窗体应用程序创建setup.exe文件 - How to create a setup.exe file using InstallShield LE for a Visual Studio 2012 Windows Forms Application with SQL Server 2012 Local Database 在Visual Studio 2012中设置数据库 - Setting up database in Visual Studio 2012 无法在Visual Studio 2012 Professional中导入数据库 - Not able to import database in Visual Studio 2012 Professional 将SQL Server 2012数据库连接到Visual Studio 2012 - Connecting SQL Server 2012 database to Visual Studio 2012 使用C#连接到SQL Server 2012数据库(Visual Studio 2012) - Connect to SQL Server 2012 Database with C# (Visual Studio 2012) Visual Studio本地数据库到在线数据库 - Visual Studio local database to online database Visual Studio 2012将本地.sdf文件移动到服务器计算机 - Visual Studio 2012 move local .sdf file to server computer Visual Studio 2013中的本地数据库连接错误 - Connection of local database error in Visual Studio 2013
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM