0

I have been trying to use local db of c# and I have managed to work with local db. My db contains a connection string which is used by this command

SqlConnection _connection = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Devjeet Projects\Visual Studio Projects\Pujo Project\FinalPujoSetup\FinalPujoSetup\counter_db.mdf;Integrated Security=True");

I can see the path of the connection string contains the path somewhere in my pc. I am able to access the data with these queries.

For updating the database

_connection.Open();
SqlCommand cmd = _connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update [Table] set Time = '" + _new_time + "'";
cmd.ExecuteNonQuery();
_connection.Close();

and for fetching the data.

_connection.Open();

String sqlSelectQuery = "SELECT * FROM [Table] WHERE Id = " + int.Parse("1");
SqlCommand cmd = new SqlCommand(sqlSelectQuery, _connection);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
    lblTotalTime.Text = (dr["Time"].ToString());
}
_connection.Close();

In this line lblTotalTime.Text = (dr["Time"].ToString()); the Time is the only table data.

Now I want to transfer this project to my friends pc and run it there. Obviously, it will not work because the path will be different.

is there any way I can change the connection string such that it can automatically fetch the path of the database in any computer and then can be executed there?

or is there any other way that I could use local db such that when I transfer the application in different pc, my application will work?

FCin
  • 3,804
  • 4
  • 20
  • 49
Devjeet Mandal
  • 345
  • 1
  • 4
  • 23
  • https://stackoverflow.com/questions/28321508/publish-a-project-with-local-database – Ehsan Kiani Sep 29 '19 at 06:14
  • @EhsanKiani I get an error `An error occurred while signing: SignTool.exe was not found at path C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\signtool.exe.` – Devjeet Mandal Sep 29 '19 at 06:24
  • https://blogs.msdn.microsoft.com/vsnetsetup/2013/11/18/an-error-occurred-while-signing-signtool-exe-not-found/ – Ehsan Kiani Sep 29 '19 at 06:42
  • @EhsanKiani Everything done but when i set the connection string as given in the link i get this error. `A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 - Local Database Runtime error occurred. Cannot create an automatic instance` – Devjeet Mandal Sep 29 '19 at 07:16
  • Did you change the connection string? what is it now? – Ehsan Kiani Sep 29 '19 at 07:28
  • @EhsanKiani in app config it is `connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\counter_db.mdf;Integrated Security=True"` and in my code it is `SqlConnection _connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\counter_db.mdf;Integrated Security=True");` – Devjeet Mandal Sep 29 '19 at 08:09
  • if you use the same connection that's in app.config what would happen? – Ehsan Kiani Sep 29 '19 at 08:13
  • @EhsanKiani Ok now it builds without any errors but data is not saved in the database. The database path is `F:\Devjeet Projects\Visual Studio Projects\Pujo Project\FinalPujoSetup\FinalPujoSetup\counter_db.mdf` but my connection string is `connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\counter_db.mdf;Integrated Security=True"`. is path and connection string path are different?? – Devjeet Mandal Sep 29 '19 at 08:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200113/discussion-between-ehsan-kiani-and-devjeet-mandal). – Ehsan Kiani Sep 29 '19 at 08:47

0 Answers0