I want to create an exe of my c# windows application project. I created the exe. But my problem is that I don't know how to include database with this exe. Because now am taking backup of my database and restore this backup to the system in which I want to install my exe. Database is created in sql server2012. In my c# code connection string set to my system server name. so if I want to install it in another system, I need to change this connection string as server name of the system in which I want to install my exe. But it is not possible in all the time. so is there any method to done all these without changing in the code? I Created the exe using install shield. Thanks.
-
Use the `App.config` or some configuration file to store the connection string and change it during installation or afterwards. Don't hardcode it. E.g. http://stackoverflow.com/questions/6536715/get-connection-string-from-app-config – Jens May 15 '15 at 05:42
-
It is only possible if the computer where .exe is going to install can ping the system where sql server is installed and database is there. If there is not connectivity it is better to use localdb connectivity thats save the database along with your project and call it from there like "SqlConnection con = new SqlConnection(@"Data Source(LocalDB)\v11.0; AttachDbFilename="+Application.StartupPath + "\........mdf; Integrated Security=True;Connect Timeout=30");" – Sachu May 15 '15 at 05:43
-
@Sachu:what u mean by dbfilename?mdf is creating during resore time na? – user3575807 May 15 '15 at 07:06
-
Can anyone clear my doubt? – user3575807 May 20 '15 at 05:57
1 Answers
Normally database settings should be configurable i.e. the user sets the settings through the application UI which are then written into a configuration file. If you give the settings through a configuration file with hardcoding, the exe need not be built everytime.
For getting the existing database, your application should be coded to create a blank database if the database in the server doesn't exist. The existing data can be imported through Administrator mode od your application or manually done in the SQL Server.
The following code shows how you can store connection strings in App.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MyDBConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=localhost;Initial Catalog=MySQLServerDB; Integrated Security=true" />
</connectionStrings>
</configuration>
Once you have saved your connection string in App.config file you can use System.Configuration.ConfigurationManager class to read this connection string in code.
ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["MyDBConnectionString"];
ConnectionStringsSettings class provides properties to read connection string settings in your program as following code shows.
string name = conSettings.Name;
string providerName = conSettings.ProviderName;
string connectionString = conSettings.ConnectionString;
The above code has been taken from this link
For a detailed example check this article on CodeProject

- 2,925
- 4
- 34
- 59