John V
John V

Reputation: 5047

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. I have added Service-based database and "Database1.mdf" was added to my project. 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). Is that wrong?

Upvotes: 0

Views: 1255

Answers (3)

stoarch
stoarch

Reputation: 44

I experiencing same problem and decided to move mdf file to static c:\db directory. Connection string was changed to incorporate new file location (AttachDbFile).

But AttachDbFile has some issues also (by some reason one table in db is inaccesible and error is access denied).

So I decided to move to Sqlite. LocalDb has many issues to work with. 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.

Upvotes: 0

ferday
ferday

Reputation: 197

here is what i use to connect. it appears as a Data Connection in Server Explorer.

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

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

Upvotes: 0

Adam Jachocki
Adam Jachocki

Reputation: 2125

If you don't require any SQL server, take a look at SQLite. This is lite SQL database engine. Database is just one file. C# has a great library to SQLite on NuGet: https://www.nuget.org/profiles/mistachkin

SQLite is widely used, event in Android (as a native db engine).

Upvotes: 2

Related Questions