I am adding my database in both bin->Debug and bin->Release folder.
When I create a setup file, I am getting the primary output from configuration manager->release.
Is there a way I could have the DB set up at only one place (not both Debug and Release folder)
This is the connection string:
<add name="myConnString" connectionString="Data Source=Test.sqlite; InitialCatalog = Test.sqlite;Compress=True;Version=3"
providerName="System.Data.Sqlite" />
Thank u
Why don't you create a 'data' folder at the same level than the bin folder? This way the database location never changes, and you can use InitialCatalog =../../data/Test.sqlite
Place it in the main Project path:
<add name="myConnString" connectionString="Data Source=Test.sqlite; InitialCatalog = ..\..\Test.sqlite;Compress=True;Version=3"
providerName="System.Data.Sqlite" />
Related
<connectionStrings>
<add name="localconnection" connectionString="Driver={Microsoft Access Driver (*.mdb)}; DBQ=D:/abc/pqr/xyz/abc.mdb; UID=;PWD=12345;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Question: How can i pass relative path for (.mdb) file in web.config file? I tried using (../) and (~/) but its not working. Can any one please help me out.
In web.config
<connectionStrings>
<add name="localconnection" connectionString="Driver={Microsoft Access Driver (*.mdb)} ;pwd=12345; DBQ="/>
</connectionStrings>
In .aspx file
string Connection = ConfigurationManager.ConnectionStrings["localconnection"].ConnectionString + Server.MapPath("//abc//pqr//xyz//abc.mdb");
OdbcConnection SGemsConn = new OdbcConnection(Connection);
In my ASP.NET project I added a new connection string in the Web.config file having the name "Proba":
<connectionStrings>
<add name="Users" connectionString="Data Source=.\SQLExpress;Initial Catalog=Registratura;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="Test" connectionString="Data Source=.\SQLExpress;Initial Catalog=REGDATABASE;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="Proba" connectionString="Data Source=.\SQLExpress;Initial Catalog=AnotherReg;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
This piece of code should list all the 3 connection strings:
List<String> conns = new List<string>();
foreach (ConnectionStringSettings conn in System.Configuration.ConfigurationManager.ConnectionStrings)
{
if(conn.Name != "LocalSqlServer")
conns.Add(conn.Name);
}
But it only detects the former 2 strings. I have built and rebuilt, closed Visual Studio and then reopened it, but nothing changed.
I have also tried to update the database in the Package Manager Console, but once again the connection is not found and the following red error occurs:
No connection string named 'Proba' could be found in the application
config file.
Why could it happen?
After different attempts, I came to realize that the Web.config file onto which I had imprinted the new connection strings was kind of a phantom of the real Web.config that needed to be changed. It had been opened before some configuration settings I made, so it was no more available. That is why the code written in it was not considered.
I decided to learn how to make a simple ASP.Net project, with a reference to a database project through the Repository Pattern.
I have my Controller calling for a List<Weight> to handle:
public IActionResult MyWeight()
{
var repo = new Database.Repositories.WeightRepository();
var data = repo.GetWeight().Result;
return View(data);
}
When repo.GetWeight() is called, I get an AggregateException error, with an inner exception saying:
"No connection string named 'MyDatabaseConnection' could be found in the application config file."
So for clarity, let me outline the solution's structure:
aspProj
Controllers
Views
Service
App.config (1)
Web.config
...
Database
Entities
Repositories
App.config (2)
...
Database.Test
Test.cs
App.config (3)
...
I've added the following connectionString to all App.configs and the Web.config:
<connectionStrings>
<add
name="MyDatabaseConnection"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Integrated Security=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
I have tested the database both from Visual Studio's Server Explorer, and through the Test.cs file from the test-project. I can insert data and retrieve without a problem.
But when the ASP.Net-part wants to access it, there is no love.
I thought it might be the ISS which did not know the path from where it is...
Any thoughts?
__
Edit:
My Web.config:
My AppSetting.json:
Well the problem is clear - ASP.NET is trying to access the database using a connection string with the name MyDatabaseConnection:
<connectionStrings>
<add name="MyDatabaseConnection" connectionString="put the connection to the db here..." />
</connectionStrings>
And in your Web.config you only have a connection string with the name WeightDatabaseConnection:
<connectionStrings>
<add name="WeightDatabaseConnection" connectionString="put the connection to the db here..." />
</connectionStrings>
Just add a new element for MyDatabaseConnection under <connectionStrings> in the Web.config file and it should work
We have several c# selenium test suites that are run on several servers by several testers.
I want to simplify the settings if possible. Currently we have multiple c# selenium projects each with their own app.config. When I want to change the server, I need to change each and every app.config. My app.config currently looks something like this:
<connectionStrings>
<add name="Company"
connectionString="Data Source=func3_db1;Initial Catalog=CompanyProduction;Integrated Security=SSPI;"/>
<add name="CompanyProductionEntities"
connectionString="metadata=res://*/DataAccess.CompanyEntities.csdl|res://*/DataAccess.CompanyEntities.ssdl|res://*/DataAccess.CompanyEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=func3_db1;initial catalog=CompanyProduction;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
We also have some settings in Windows Environment variables. It's kind of an obscure way of doing it but it works pretty well. To access these settings, we just do something like this:
var value = Environment.GetEnvironmentVariable(varName, EnvironmentVariableTarget.User);
So, we have a variable called "CompanyTestBrowser" which can be set to "Firefox" or "Chrome" or "IE".
I like environment variables because the powershell scripts that run all of our selenium tests can easily alter the variables whenever they need to.
However, I can't seem to pull those DB strings out of this app.config. How can I move them into something that can be a bit more global & dynamic. Ideally I only have to set in 1 single place? Ideally, I could move them into environment variables like the other or a config file that sits outside the c# project.
Thanks!
My advise would be, have the connection strings maintained in a xml file in a networklocation ex. \machine-name\DBConnectionString.config as below format
<connectionStrings>
<add name="Company"
connectionString="Data Source=func3_db1;Initial Catalog=CompanyProduction;Integrated Security=SSPI;"/>
<add name="CompanyProductionEntities"
connectionString="metadata=res://*/DataAccess.CompanyEntities.csdl|res://*/DataAccess.CompanyEntities.ssdl|res://*/DataAccess.CompanyEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=func3_db1;initial catalog=CompanyProduction;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
At your application startup routine, read \machine-name\DBConnectionString.config file and update application config file using the below code snippet,
// Get the application configuration file.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
// Create a connection string element and
// save it to the configuration file.
//Read the \\machine-name\DBConnectionString.config file
// Create a connection string element.
ConnectionStringSettings csSettings =
new ConnectionStringSettings("My Connection",
"LocalSqlServer: data source=127.0.0.1;Integrated Security=SSPI;" +
"Initial Catalog=aspnetdb", "System.Data.SqlClient");
// Get the connection strings section.
ConnectionStringsSection csSection =
config.ConnectionStrings;
// Add the new element.
csSection.ConnectionStrings.Add(csSettings);
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
Hope this helps.
You could add a key to app.config which has a path to an XML file which has all the common settings in it and then write a new ConfigurationManager class to first try to pull out a value from app.config and if not found, open the XML file and look for it in there
Something like this:
<appSettings>
<add key="ConfigFileSettings" value="\\MyServer\CommonSetting\settings.xml"/>
I used the accepted solution above. This is my exact code (which is only slightly different than above).
This solutions effectively keeps my current app.config file. All other settings in that app.config file are kept, except I "override" the "connectionString" part with my custom one.
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
System.Console.WriteLine("Starting to write app.config stuff");
//Change the Admin's app.config where name=companyProductionEntities
config.ConnectionStrings.ConnectionStrings["companyProductionEntities"].ConnectionString =
string.Format(#"metadata=res://*/DataAccess.companyEntities.csdl|res://*/DataAccess.companyEntities.ssdl|res://*/DataAccess.companyEntities.msl;provider=System.Data.SqlClient;provider connection string=';data source={0};initial catalog=companyProduction;integrated security=True;multipleactiveresultsets=True;App=EntityFramework';", SeleniumConfiguration.SimpleDatabaseConnectionString);
config.Save(ConfigurationSaveMode.Modified, true);
ConfigurationManager.RefreshSection("connectionStrings");
This is for a Windows Application.
In a class I want to referee to my connectionstring called freighthelper which is located in the settings.settings file in my project. How do I do this?
I have tried with this without success.
_connection.ConnectionString = FreightHelper.Properties.Settings.Default.freighthelper;
Why do you need to store your connection string in a settings file? It is usually put in the config file and retrieved like this: http://msdn.microsoft.com/en-us/library/ms254494%28VS.80%29.aspx
try this: _connection.ConnectionString=Settings.Default. freighthelper; to read form a setting files.
NOte: connection string better to place in App.confing
E.g:
<connectionStrings>
<add name="Name" connectionString="Data Source=Instance Name;Integrated Security=True;MultipleActiveResultSets=True;"
providerName="System.Data.EntityClient" />