简体   繁体   English

Visual Studio 2015中的本地数据库

[英]Local DB in Visual Studio 2015

After a few years, I have returned to writing in C# and I am really struggling here - I would like to have my app to have a local SQL database. 几年后,我回到了用C#编写的过程中,在这里我真的很挣扎-我想让我的应用程序具有本地SQL数据库。 I have added Service-based database and "Database1.mdf" was added to my project. 我已经添加了基于服务的数据库,并且“ Database1.mdf”已添加到我的项目中。 I created a table and added some data just to see if it is working but I cannot connect to it. 我创建了一个表并添加了一些数据,只是为了查看它是否正常工作,但是我无法连接到它。 I tried numerous connection strings with no success (Server not accessible). 我尝试了许多连接字符串,但均未成功(服务器无法访问)。

Do I need to run something else in the background? 我是否需要在后台运行其他内容? I thought that I might have a local database and with .NET client I can access it, and I hoped it would work whenever I bring my application (also not requiring any SQL server running). 我以为我可能有一个本地数据库,并且可以使用.NET客户端访问它,并且希望它在我携带我的应用程序时也可以运行(也不需要运行任何SQL Server)。 Is that wrong? 错了吗

If you don't require any SQL server, take a look at SQLite. 如果您不需要任何SQL Server,请查看SQLite。 This is lite SQL database engine. 这是lite SQL数据库引擎。 Database is just one file. 数据库只是一个文件。 C# has a great library to SQLite on NuGet: https://www.nuget.org/profiles/mistachkin C#在NuGet上有一个很棒的SQLite库: https : //www.nuget.org/profiles/mistachkin

SQLite is widely used, event in Android (as a native db engine). SQLite在Android中作为事件被广泛使用(作为本地db引擎)。

here is what i use to connect. 这是我用来连接的东西。 it appears as a Data Connection in Server Explorer. 它在服务器资源管理器中显示为Data Connection

string con2 = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=" + Application.StartupPath + "\\jobfile_2017.mdf;Integrated Security=True;Connect Timeout=30";

when i first started working with these, i used this source 1 当我第一次使用这些工具时,我使用了此来源 1

it works on PC's that i have nothing installed (not even office) but as i said i'd be interested to know of any shortcomings of this method 它可以在PC上安装任何东西(甚至不安装办公室),但正如我所说,我很想知道这种方法的任何缺点

I experiencing same problem and decided to move mdf file to static c:\\db directory. 我遇到同样的问题,决定将mdf文件移动到静态c:\\ db目录。 Connection string was changed to incorporate new file location (AttachDbFile). 连接字符串已更改为合并新文件位置(AttachDbFile)。

But AttachDbFile has some issues also (by some reason one table in db is inaccesible and error is access denied). 但是AttachDbFile也有一些问题(由于某种原因,db中的一个表无法访问并且错误被拒绝访问)。

So I decided to move to Sqlite. 因此,我决定迁移到Sqlite。 LocalDb has many issues to work with. LocalDb有很多问题要处理。 I read good note to resolve problem: in command line stop/remove/start name of instance. 我读了很好的说明来解决问题:在命令行中,实例的停止/删除/启动名称。 But it nuissance. 但这很讨厌。

Wish you good luck to work with these db files. 祝您好运,可以使用这些数据库文件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM