Is it possible to include a app.config by the host in C# .net? So i can load a different connection string and other options?
like
if (host == "stackoverflow"){
//inlcude stackoverflow.config
}else{
//include othersite.config
}
It is possible to load a configuration file. You should have a look at ConfigurationManager documentation
But if you want to use different connection strings you could use it by its name like this. See Retrieving Connection Strings at Run Time
Related
I just created a desktop Winforms application with localhost database.
The connect string I am using is this:
SqlConnection connect = new SqlConnection(#"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Administrator\Desktop\learningsystem\LearningSystem\LearningSystem\LearningSystem.mdf;Integrated Security=True");
If I want to run my application on other computers, how should I make it work?
EDIT:SOLUTION
Thank for all the help! I tried the following steps. I think it is working now. But please correct me if I did something tricky.
1. add a new setting item in project property setting. App.config will automatically update:
<connectionStrings>
<add name="LearningSystem.Properties.Settings.LearningConn" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\LearningSystem.mdf;Integrated Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient" />
</connectionStrings>
2. In my program, just add the following statement to connect to the sql server
SqlConnection connect = new SqlConnection(#"Data Source = (LocalDB)\MSSQLLocalDB; AttachDbFilename=|DataDirectory|\LearningSystem.mdf;Integrated Security = True; Connect Timeout = 30");
Further question
If others will run this application on their computer(not in the same network), they just go into the project setting and change the value by selecting the database file I provide to them,the connectionString will automatically change, right?
Thanks!
It's generally a bad idea to hard code such stuff in your application. Normally, application settings and connection strings are placed in the application's configuration file (in the ConnectionStrings section).
Just like with all strings, you could build your connectionstring from dynamic parts (variables, settings, etc.) and then pass that generated connectionstring to the SqlConnection constructor. Again, to make those separate parts configurable without hard coding them in your application, you might want to add them to your application's configuration file (in the AppSettings section). But IMHO this is an overly complex solution in most scenarios. Putting the entire connectionstring in the ConnectionStrings section is more straightforward (and more flexible).
Anyway, again, to make your application configurable, you might use your application's configuration file (App.config or Web.config), you need to add a reference to System.Configuration in your project's .NET Framework dependencies and use the AppSettings and ConnectionStrings properties of the System.Configuration.ConfigurationManager class.
(Of course, there are more ways to make your application configurable. But using the application configuration file is one of the most straightforward solutions.)
Edit:
When deploying your app to another computer, you need to copy its database over too. If you want to use the application on multiple machines and let them connect to the same database, you might want to leave LocalDB and migrate the data to a SQL Server (Express) instance and make it accessible over the (local) network.
Edit 2 (regarding the recent edits in your post):
I see in step 1 that you are using an application setting (called LearningConn) in your solution now. That's fine. However, it is important that you also use that setting in step 2, like this:
SqlConnection connect = new SqlConnection(Properties.Settings.Default.LearningConn);
If you change the setting in Visual Studio, it will update the connection string. Since the setting will probably have application scope, it will not be possible to update the setting/connection string within your application in runtime (by the user).
I'm not sure if your connection string using |DataDirectory| will always work as expected in all scenarios. I have only been using it in ASP.NET webapplications. If it does work in WinForms applications, you might read this document to learn how to set it up. But personally I am somewhat sceptical about this approach.
I personally would opt for a solution where you use a placeholder in your connection string, which you replace with the full path to the .mdf file before you pass it to your SqlConnection constructor.
When you use "{DBFILE}" as the placeholder, for example, the value of your LearningConn setting would look like this:
Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename={DBFILE};Integrated
Security=True;Connect Timeout=30
(Note that this value should be a single line without any line breaks!)
You might create a separate setting in your application called DbFile (of type string) to store the actual value that should be put in place of {DBFILE} in your connection string. When you use scope "user" for that setting, the value might be changed from within the application by the user. When saved, it might not be saved directly in the application's configuration file, however, but in an additional configuration file hidden somewhere in the user's Windows user profile. You might read this document to learn more about application settings.
Your code in step 2 might eventually look something like this:
string connectString = Properties.Settings.Default.LearningConn;
string dbFile = Properties.Settings.Default.LearningSystemDb;
connectString = connectString.Replace("{DBFILE}", dbFile);
SqlConnection connect = new SqlConnection(connectString);
To let your application's users select and store the database .mdf file to use, you might include (a variation of) the following code in your application somewhere:
using (var dlg = new System.Windows.Forms.OpenFileDialog())
{
dlg.Title = "Select database file to use";
dlg.Filter = "Database Files (*.mdf)|*.mdf";
dlg.CheckFileExists = true;
if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
Properties.Settings.Default.DbFile = dlg.FileName;
Properties.Settings.Default.Save();
}
}
Your question is not clear!
you need work with one Database on 2 or more PC?!
OR
you need work with 2 separate programs?
if you need 2 separate programs :
you must copy .mdf file to other PC at same address or keep mdf address in app.config and read it before connect to SQL.
How to Read From app.config
if you need work with one Db you must connect to dataBase Server such as SQL Server and keep connection string in app.config in connectionStrings tag.
Get connection string from App.config
If you want to work on other PCs, rather than building it dynamically make the connection string more generic:
Server=(localdb)\\mssqllocaldb;Database=LearningSystem;Trusted_Connection=True;MultipleActiveResultSets=true
This should create the mdf file under 'mssqllocaldb' in %appdata% for each user. You might need LocalDb installed (which you tick during SQL Server installation)
I am currently on a solution in ASP.net containing two project. One MVC project and the other is a class library serving as an API.
Currently I have a connection string like this in the web config of my project MVC.
I read it with the following code in my API:
public ConnectionProvider()
{
this.connectionString = ConfigurationManager.ConnectionStrings[Connection.Name].ConnectionString.ToString();
factory = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings[Connection.Name].ProviderName.ToString());
}
The problem is that I would like to move the connection string in the app.config of my API and by default but every time it starts, it will read in the web.config.
Using a resource file is a bad idea.
Just copy the the connection string from the api and have it in the web.config. By using the resource file there is no way to update that connection string without having to recompile.
The web application will use web.config and the connection string will be accessible from there. If the same api is to be reused in another project, say a desktop application, again just copy the connection string to the config file of the entry point.
As I suggested via comment, I would use a resource file in the class library.
While #Nkosi is not wrong that the connectionstring will be static, it's the easiest way to carry the connectionstring across w/o having an actual API or database call.
However, it's not the entire application that needs to be republished.
If the functionality in the code of the class library does not change, you can simply build the class library and overwrite the .dll of it.
Another valuable solution would be to save the connectionstring in a separate text file which all solutions will use.
Then you can replace that piece of code in all your applications while having 1 centralized point of access which can be replaced w/o building and replacing anything :).
(Posted solution on behalf of the OP).
Thanks to Dieter B!
With a resource file:
And for read it in the API:
public ConnectionProvider()
{
ResourceManager rm = new ResourceManager("Bank.Project.API.resources", GetAssemblyByName("Bank.Project.API"));
this.connectionString = rm.GetString(Connection.Name);
this.factory = DbProviderFactories.GetFactory(rm.GetString(Connection.Factory));
}
Assembly GetAssemblyByName(string name)
{
var Myassembly = AppDomain.CurrentDomain.GetAssemblies().
SingleOrDefault(assembly => assembly.GetName().Name == name);
return Myassembly;
}
guys i'm still new on developing system and I've encountered this
This is the string i use to connect. It is inside of a class.
public string connections1 = "user id=sa;" +
"password=;server=SEAN\\SQLEXPRESS;" +
"Trusted_Connection= false;" +
"database= METROEXPRESS; ";
How do i change this to put the connection information in an .ini file?
You'd not using an .ini file; you'd use a .config file, which is an XML file containing your configuration. If you're doing a rich client app (console, winforms, wpf) add an Application Configuration to your project and go from there (look up the section). If you're doing an asp.net application, you should already have a web.config, which you can also add a section to.
Here's an example: http://social.msdn.microsoft.com/Forums/en-US/adodotnetdataproviders/thread/9a8c9f5a-092e-4c4a-87bb-9f35d8f55da1/
.NET and C# don't have built-in mechanism to edit *.ini files. The standard format for configuration files in .NET is XML.
Check the next question if you still want to use *.ini files.
Reading/writing an INI file
simple question how do i change the connection string of the nhibernate at runtime ?
<property name="connection.connection_string" >value</property>
nevermind i got it.
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var root = XElement.Load(configuration.FilePath);
root.Elements().Where(m => m.Name.LocalName == "hibernate-configuration").First().Elements().First().Elements().Where(m => m.FirstAttribute.Value == "connection.connection_string").First().Value = cs;
root.Save(configuration.FilePath);
Tinkering with your app.config on the fly is a bit horrible.
I'd suggest not storing your connection string amongst the other nhibernate settings. Keep your various connection strings elsewhere (e.g. in appSettings), then set the appropriate connection string directly against your NH Configuration:
var configuration = new Configuration();
configuration.SetProperty("connection.connection_string", "...connection string...");
configuration.Configure();
Just make sure that the connection.connection_string setting is not specified in your config file, as configuration.SetProperty will only work correctly if the setting is not there already.
When you need to switch connections based on some conditions (such as having one website with live and demo mode, each with different connection string) then it´s probably best to implement a class inherited from DriverConnectionProvider and set this class as value of the connection.provider configuration property in your config file.
See http://jasondentler.com/blog/2009/11/authentication-impersonation-and-dynamic-nhibernate-connection-strings/
I would personally go with Enterprise Library and its Data Access Application Block to provide the NHibernate sessions APIs with proper named connectiong strings when instantiating a session API.
The DAAB has the feature to instantiate a DbConnection based on the configuration file. So you could possibly use several connection strings definition, and tell DAAB what connection to use, then pass it to your NHibernate session so that you may work with NHibernate against multiple datastores at once.
Using this approach will avoid you messing with the configuration file on runtime, and even allows you to create your own connections instance without having them defined in the configuration file at once.
I deployed my asp.net web app project that has a reference to my DAL class library. How can I change the connection string in my DAL library once deployed? The DAL code was ignoring my web.config connection string and trying to use the app.config value.
I thought I would be able to edit a config file that is associated with the class library, but I can't find one. Temporarily I edited the connection string and re-compiled and re-deployed the library.
Is there an option or way to setup the project files where it changes the values of the connection string based on being compiled in debug mode versus doing a release compile.
What is the recommended way of dealing with connection strings in web apps which reference class libraries?
Clarification:
the DAL library connection strings are also utilized by some datasets and L2S classes (.dbml) and I am not sure how to change those to reference a web.config file that sits outside the library in the web app project.
Currently Using this code to get around my L2S class problem:
public partial class MyDataContext
{
partial void OnCreated()
{
ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"];
if (cs != null)
{
this.Connection.ConnectionString = cs.ConnectionString;
}
}
}
Generally, I let the top-level project define this, via either web.config or app.config; either by specifying that the application should include a connection-string named "FOO", or (much better) allowing the calling application to pass (one of) the connection key, the connection string, or the connection to the dll.
Then you mainly just edit web.config like normal...
I usually place the string in the web.config file and let a class (possibly a global class used to reference settings in the web.config) reference it, with the following line:
class Globals
{
static string ConnectionString =
ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
}
and use it elsewhere like:
IApartmentRepository apartmentRepository = new
ApartmentRepository(Globals.ConnectionString);
This was also Saif Khan's suggestion in his answer.
As for changing the connection strings based on compile mode, Visual Studio 2010 has support for this, see Web Deployment: Web.Config Transformation from the Visual Web Developer team blog. I do not think Visual Studio 2008 can do this out of the box, however maybe it can be done with some kind of build script.
I don't believe there is a recommended way, but a prefered way. Some store connection strings in the web.config file, while some store in the registry or machine.config, while some go to the extreme and store it remotely...yes I've seen that.
The most common storage I see and use myself is storing in the web.config. In my DAL objects I make a call to the web.config file for the connectionstring
string connStr = ConfigurationManager.ConnectionStrings["myString"].ConnectionString
as for auto changing of the connectionstring based on the app compiled mode, I've never seen that. You might have to put that check in the DAL itself to check if debug mode is turned "on" or "off". That would require 2 connectionstring entries in the web.config file.