0

I have made a sample c# application (First project ive done) Its quite a simple application which uses a database to store data and edit data.

The problem i am having is the program works perfectly fine on my computer but if i publish the application and put it on another computer it cannot use the database as it is not part of the project.

I used connection string

private SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename = \"C:\\Users\\Ryan\\Documents\\Visual Studio 2015\\Projects\\youtubeLoginTut\\youtubeLoginTut\\data.mdf\"; Integrated Security = True; Connect Timeout = 30");

Which is obviously the path to the database on my computer and will not be the same on the next computer. Is there anyway i could include the database in the package so its referenced from where ever the application sits.

Ive tried shortening the path to for example ..\data.mdf but to no avail and i cant find anything on google so im all out of ideas.

Go easy im very new to c#

Cheers Ryan

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Hadley8899
  • 136
  • 1
  • 12

4 Answers4

0

There is a way to get the location of your project in every computer : (inside the bin/debug/)

string path = AppDomain.CurrentDomain.BaseDirectory //example : C:/Users/Ryan/Documents/Visual Studio 2015/Projects/Youtubetut/bin/debug

you just need add the location of the database inside of the project's folder to this path. path will replace your "C:\Users\Ryan\Documents\Visual Studio 2015\Projects\youtubeLoginTut" and make sure to move your database inside the debug folder.

Eliran Ziv
  • 320
  • 1
  • 2
  • 13
  • Right i got this working perfect, Now what i need is for the database to actaully be included when i publish, It works perfectly fine in the microsoft visual studio but when i publish and run the app the database isnt there, I have moved the database over to where the exe is and it works but is there anyway i can get it so it does this automatically ? Cheers – Hadley8899 May 03 '16 at 18:06
  • I think you can find what you are looking here - http://stackoverflow.com/questions/28321508/publish-a-project-with-local-database – Eliran Ziv May 03 '16 at 18:14
  • Thankyou so much, Worked perfectly – Hadley8899 May 03 '16 at 18:36
0

Afeter publiching your database with your project you get the Installtion Path From Deployment byuse :

string sourcePath =System.Reflection.Assembly.GetExecutingAssembly().Location
sourcePath =sourcePath +"\data.mdf";
Beldi Anouar
  • 2,170
  • 1
  • 12
  • 17
0

If it's a simple application you can try to use embeded DB like SQLite. I use it in my application and it works fine.

Avirtum
  • 438
  • 6
  • 14
0

If you want to use SQLite, let's go step by step.

  1. download the dynamic library System.Data.SQLite.dll for your version of the .NET Framework
  2. link System.Data.SQLite.dll to your project
  3. write a code something like this

Create a table

SQLiteConnection con = new SQLiteConnection(String.Format(@"Data Source={0};Version=3;New=True;", "./db/mydatabase.sdb"));
con.Open();
SQLiteCommand cmd = con.CreateCommand();
cmd.CommandText = @"CREATE TABLE Books (BookId int, BookName varchar(255), PRIMARY KEY (BookId));";
cmd.ExecuteNonQuery();
con.Close();

Read the data

using(SQLiteConnection con = new SQLiteConnection(String.Format(@"Data Source={0};Version=3;New=False;", "./db/mydatabase.sdb")) {
   con.Open();
   using (SQLiteCommand cmd = con.CreateCommand())
   {
      cmd.CommandText = @"SELECT BookName FROM Books WHERE BookId=1 LIMIT 1;";
      using (SQLiteDataReader reader = cmd.ExecuteReader()) {
         if (reader.HasRows && reader.Read()) {
            oResult = Convert.ToString(reader["BookName"]);
         }
         reader.Close();
      }
   }
   con.Close();
}
Avirtum
  • 438
  • 6
  • 14