Add new connection in connectionString - c#

I have the next connectionstring file:
<?xml version="1.0"?>
<connectionStrings>
<add name="DefaultConnection"
connectionString="Application Name=CalculatorTest;Server=PC01;Initial catalog=MyDB;Integrated Security=true" />
</connectionStrings>
I want to add a new connection, for example "MyDBTest", staying:
<?xml version="1.0"?>
<connectionStrings>
<add name="DefaultConnection"
connectionString="Application Name=CalculatorTest;Server=PC01;Initial catalog=MyDB;Integrated Security=true" />
<add name="NewDefaultConnection"
connectionString="Application Name=CalculatorTest;Server=PC01;Initial catalog=MyDBTest;Integrated Security=true" />
</connectionStrings>
How do I do it from C# Mvc 5?

Right click on your project and add an Entity Data Object, then enter the name you want it to be called; chose code first from database, then enter the server address and you can also select which tables from the db you want.
This will create the connection string for you. I would however recommend doing this in a separate class library so it’s easier to maintain. Note: if you do use it in a separate class library, you will need to look in the Web.config and copy it over to your main application web.config. Also you will want to add the reference of it in your project references.

Related

ASP.NET and Visual Studio 2019: how to set up Web.Debug and Web.Release if you have configSource in Web.config?

In my ASP.NET web form, this is what my connection string looks like in my Web.config file:
<connectionStrings configSource="MySecrets.config"/>
I know that I can use Web.Debug and Web.Release to change the connection strings so that they are not exposed when the web app is released.
However, the examples provided by Visual Studio mention:
In the example below, the "SetAttributes" transform will change the value of
"connectionString" to use "ReleaseSQLServer" only when the "Match" locator
finds an attribute "name" that has a value of "MyDB".
<connectionStrings>
<add name="MyDB"
connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
However, this references a <connectionStrings> section in the Web.config file that I don't have in the first place, because in my project I have:
<connectionStrings configSource="MySecrets.config"
How can I set up the Web.Release to replace the MySecrets.config file so that it's not visible once published?
use the preprocessor to switch between connectionstring, for this you should have both connectionstrings.
web.config
<connectionStrings>
<add name="Project.Properties.Settings.ConnString_A" connectionString="" providerName="System.Data.SqlClient" />
<add name="Project.Properties.Settings.ConnString_B" connectionString="" providerName="System.Data.SqlClient" />
</connectionStrings>
code behind
public SqlConnection conn { get; set; }
public DbContext()
{
#if DEBUG
conn = new SqlConnection(Properties.Settings.Default.ConnString_A);
#else
conn = new SqlConnection(Properties.Settings.Default.ConnString_B);
#endif
}
reference: #if (C# Reference)

App.config transformation is not working based on environment

I have a console application and I need to separate app.config for diff. env in order to use different connection strings.
I right-clicked on App.config -> Add config transforms and got two files:
App.Debug.config
App.Release.config
Those files are following:
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-
Transform">
<connectionStrings>
<add name="SqlCpaConnectionString"
connectionString="blahblah"
providerName="System.Data.SqlClient"/>
<add name="AzureStorageConnectionString"
connectionString="blahblah"
providerName="Microsoft.WindowsAzure.Storage"/>
When getting the settings like:
ConfigurationManager.ConnectionStrings["AzureStorageConnectionString"].ConnectionString
I get null ref ex on both of envs...
See following answer. This will also work with console apps: How do I use Web.Config transform on my connection strings?
Set the connection string transform:
<connectionStrings>
<add name="local" connectionString="Data Source=IPAddress,Port;Initial Catalog=SomeOtherDB;User ID=TopSecretUsername;Password=SecurePassword"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>

How do I modify my web.config file to support SQL Server 2016?

I'm totally new to C#/.Net development. I've read extensive documentation about the web.config file on msdn and still am unsure what to change.
If answered would you please provide an example of what I should do? I can provide more info if necessary.
You don't have to provide information about SQL Server, You just need to give correct connection string, which points to your database.
Something like:
<connectionStrings>
<add name="myConnectionString"
connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
This is one way if you are just using some local database.
Otherwise you can set it up like this:
<connectionStrings >
<add
name="myConnectionString"
connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;"
providerName="System.Data.SqlClient"/>
And then in your code you should write:
using System.Web.Configuration;
And then at last you can set up your connection variable something like this:
SqlConnection con = new SqlConnection( WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);
Are you using the entity framework? Because then it can be generated automatically.

Why aren't new connection strings in Web.config file retrieved?

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.

How to set connection string for whole application in c# windows Application?

datadirectory is not working for connection string in c# windows application in my project.
I already tried this but its not work.
you can add it in app.config:
<connectionStrings>
<add name="Dbconnection"
connectionString="Server=localhost; Database=OnlineShopping ; Integrated Security=True" ;
providerName="System.Data.SqlClient" />
</connectionStrings>
in code Add reference to add System.Configuration and read like this:-
System.Configuration.ConfigurationManager.
ConnectionStrings["connectionStringName"].ConnectionString;
or in web.config, you can add it appSettings:
<appSettings>
<add key="ApplicationTitle" value="Sample Console Application" />
<add key="ConnectionString"
value="Server=localhost;Database=Northwind;Integrated
Security=false;User Id=sa;Password=;" />
</appSettings>
and read in code like this:
ConfigurationSettings.AppSettings["ConnectionString"];

Categories