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?