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.
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
Related
I have developed an application that uses SQL database. My challange is to be able to ship the database with the app itself. When i package my mdf file with the setup, it gets deployed. However it is not able to connect to it.
I have made following as prerequisites
When installing, it downloads and installs all the prerequisites.
I am also using following connection string to connect to it.
<connectionStrings><add name="MyDBContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=" Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDB.mdf; MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
This setup works fine when installed on my machine. Any idea how this will work on simple machines?
Finally I got it working after changing many things. First off, the connection string itself. It was
<connectionStrings><add name="MyDBContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=" Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDB.mdf; MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
But since only SQL Express 2012 is being installed on the destination machine (See the picture in question above) during setup, this was not going to work. So changed it such that it uses SQLServer Express's default instance name
<connectionStrings><add name="MyDBContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=" Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MyFolder\MyDB.mdf; User Instance=True; Integrated Security=SSPI; MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
Please note that i have added a folder after |DataDirectory|. Idea is to create a folder within AppData so it would end up creating a folder in C:\users\me\AppData\MyFolder\MyDB.mdf
I also set the User Instance as true because otherwise it wasn't working from AppData folder.
Next I added the code to update the DataDirectory to my desired location, which is in AppData folder as
AppDomain.CurrentDomain.SetData("DataDirectory",Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
I did this change in the main of my program file
I've created an application that works perfectly on my PC using C# and SQL Server 2014.
My connection string is:
Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Youssef\Desktop\gone\gone\gone\gestion_incidents.mdf;Initial Catalog=BG_ONE;Integrated Security=True
I know that the connection string is the problem but how can I create a connection string that works on all computers?
Replace your database file path to a generic path that automatically get the location of the project data folder and it will not cause problem when you move project to another location. Follow below steps:
Step 1: First Add App_Data folder in your project and add database file there.
Step 2: Change your connection string replace path before the database file with |DataDirectory|
Example:
<connectionStrings>
<add name="ConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\nwind.mdb" providerName="System.Data.OleDb" />
</connectionStrings>
If you follow the above steps then your connection replace C:\Users\Youssef\Desktop\gone\gone\gone with |DataDirectory|. See modified connection string as below:
Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Youssef\Desktop\gone\gone\gone\gestion_incidents.mdf;Initial Catalog=BG_ONE;Integrated Security=True
My project Web.config has connection strings defined in a separate file using the following construct:
<connectionStrings configSource="ConnectionStrings.config">
</connectionStrings>
This is handy when collaborating on a project or when deploying the project. However, I was unable to get the VSO Build working as it shows me the following error:
C:\Program Files
(x86)\MSBuild\14.0\bin\Microsoft.Common.CurrentVersion.targets (4105,
5) Could not copy the file
"C:\a\1\s\MyProject\ConnectionStrings.config" because it was not
found.
The connection string must be already defined in the web.config and the connection string name must be the same as the connection string name you set in Azure. Then the connection string can be updated by Azure. Refer to the description from Azure:
Connection strings work in a similar fashion, with a small additional
requirement. Remember from earlier that there is a connection string
called “example-config_db” that has been associated with the website.
If the website’s web.config file references the same connection string
in the configuration section, then Windows Azure
Web Sites will automatically update the connection string at runtime
using the value shown in the portal.
However, if Windows Azure Web Sites cannot find a connection string
with a matching name from the web.config, then the connection string
entered in the portal will only be available as an environment
variable (as shown earlier).
And
Remember though that for Windows Azure Web Sites to override a
connection string and materialize it in the .NET Framework’s
connection string configuration collection, the connection string must
already be defined in the web.config. For this example website, the
web.config has been updated as shown below:
For more information, please see this link: https://azure.microsoft.com/en-us/blog/windows-azure-web-sites-how-application-strings-and-connection-strings-work/
I know you already mark your question answered but I think I should post my solution here for anyone's having same issue.
I create a transformation for the connectionStrings section in other environment with a dummy connectionstring value like this:
<connectionStrings xdt:Transform="Replace">
<add name="Your_ConnectionString_Name" connectionString="dummy_value"
providerName="System.Data.SqlClient" />
</connectionStrings>
so that I can keep the configSource attribute in my local machine and when I deploy to other environments, I have some dummy connectionstring for Azure to replace.
I want to get connection string using app.config file
this is my app.config file :
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="MSSConStr"
connectionString="Data Source=Sithi-PC;Initial Catalog=mssdb2;User ID=XXXXX;Password=YYYYYYY"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
string x = ConfigurationManager.ConnectionStrings["MSSConStr"].ConnectionString;
this statement gives a exception "Object reference not set to an instance of an object."
Please can u help me to correct this error!!! this is working properly in .NET framework 3.5 project. But this project is .NET framework 4.0. I added the reference "System.Configuration" to my project and use it in my Database Access class.
Thank you!!!
EDIT: Addional Code:
public static SqlConnection getNewConnection()
{
string x = ConfigurationManager.ConnectionStrings["MSSConStr"].ConnectionString.ToString();
con2 = new SqlConnection(x);
return con2;
}
There are two possible issues:
1) You have some settings elsewhere in the app (settings, code, etc, that is unintentionally clearing the connection strings (unlikely, but possible).
2) The .config that is being used at run time does not have the connection string propagated to it. If you are running in debug mode, open windows explorer, navigate to the bin/debug directory and open the .exe.config or .vshost.exe.config file and see if the connection string is set correctly there. If it isn't, stop the application, clear the directory, and try again. If the problem persists, see item #1.
I have a c# assembly that uses the app.config to store its database connection string. When debugging the application I noticed that the connection to the database kept failing because the ConfigurationManager kept returning the machine.config connection string:
data source=.\SQLEXPRESS; Integrated Security;....
I added <clear/> before my connection string in the app.config and it fixed the issue on my dev machine. The problem returned when I deployed it to production. Can someone tell me how I can stop the machine.config connection string from being used?
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[0].ConnectionString);
<connectionStrings>
<clear/>
<add name="VersionConnectionString"
connectionString=" Data Source=localhost;Initial Catalog=VersionInfo;User ID=user;Password=password"
providerName="System.Data.SqlClient" />
</connectionStrings>
UPDATE
The following still gives me the machine.config connection string?!
Configuration appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
string dllConfigData =
appConfig.ConnectionStrings.ConnectionStrings[0].ConnectionString;
When using connection strings in a DLL, you need to add them to your exe's app.config as well, using the exact same name for the setting. Then, you can change the connection string in the exe's .config file and the DLL will pick it up automatically when loaded.
This is probably the only way you can have working custom connection strings in the app.config file when your DB persistence layer is implemented in a separate DLL. Don't even ask me how much time it took me to research and debug this.
I know this is an older question, but I was having the same problem today. The problem was that the app.config that I added to my project wasn't being copied to the output directory. To fix this, right click on the app.config and select Properties. Then change Build Action to Content.
Hope this helps!
Try getting an instance of your app.config file as a Configuration object:
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var myConnString = config.ConnectionStrings["VersionConnectionString"].ConnectionString;
This bypasses the machine config file completely.
You should be getting your connection string by NAME instead of INDEX - that will ensure you're getting what you're asking for.
Try
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["VersionConnectionString"].ConnectionString);