Replace Properties.Settings DB connection with connectionStrings name - c#

I'm working on a project that takes the connection to the database via the `Properties.Settings.Default;
so I have to set the connection string there, but it gets annoying because I already have a connection string set in the We.config file.
How to I set the Properties.Settings to use DefaultConnection connection string in the database?
var request = HttpContext.Current.Request;
var settings = Properties.Settings.Default; <--- use 'DefaultConnection' connectionString
using (var db = new Database(settings.DbType, settings.DbConnection))
{...}
my connection string
<connectionStrings>
<add name="DefaultConnection" connectionString="server=localhost;Database=DB;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
THANK YOU!

Related

Call globally used sql connection in app.config for insert query?

I have set the path for sql server in app.config and I want to insert data in the table?
how will I call that connection string?
I want to set this connection as the global connection for my project how will I call it on my each page in the project?
using (SqlConnection Conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["YourConfigFileAssignName"].ConnectionString))
{
};
YourConfigFileAssignName is the assigned name of sour connection string in the app.config file.
<connectionStrings>
<add name="YourConfigFileAssignName"
connectionString="Data Source=YourDataSource;Initial Catalog=YourDBname;User ID=SQLid;Password=SQLpass"
providerName="System.Data.SqlClient"/>
</connectionStrings>

Connection string has quotation mark, how to pass it to SqlConnection

I have created a LocalDB database in my project and its connection string is :
Data Source=(LocalDB)\v11.0;AttachDbFilename="E:\Projects\visual studio 2013\Projects\sqlce\mydb.mdf";Integrated Security=True;Connect Timeout=30
How should I pass it to SqlConnection()?
Note that it has an address within quotation marks. Have I done anything wrong?
I guess even if I program it correctly it won't work in another computer which doesn't have that .mdf file in that exact place. Isn't it so?
How can I have a program with a portable database so I can easily publish my pp?
Add the mdf file to your solution and and change the property "Copy to Output Directory" to Copy Always. Don't hardcode the mdf file path in connection string. Add the connection string in app.config or web.config file like below:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\myDB.mdf;Initial Catalog=MyDatabaseName;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Then you can access the connection string in your C# code as below:
string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ToString()
If you face any error regarding accessing the mdf file, the you can set the DataDirectory in your C# code using AppDomain.CurrentDomain.SetData() method.
<connectionStrings>
<add
name="NorthwindConnectionString"
connectionString="Data Source=serverName;Initial
Catalog=Northwind;Persist Security Info=True;User
ID=userName;Password=password"
providerName="System.Data.SqlClient"
/>
</connectionStrings>
you can access by
connString =rootWebConfig.ConnectionStrings.ConnectionStrings["NorthwindConnectionString"];
More about connection string
SqlConnection con = new SqlConnection(connstring);
or you can go in this way like
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);

Dynamics Connection SQL Server C#

I would like some explanations on how to create and set up a dynamic connection to SQL Server DB engine in a C # project
if you want connection string in config and read it than you need to do like this , put possible connectionstring in config
<connectionStrings>
<add name="CharityManagement"
connectionString="Data Source=.;Initial Catalog=CharityManagement;Integrated Security=True"/>
<add name="CharityManagement_two"
connectionString="Data Source=.;Initial Catalog=CharityManagement_two;Integrated Security=True"/>
</connectionStrings>
and than read it base on condition using configurationmanager class
//to read first connection string
var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement"].ConnectionString;
//to read second connection string
var connectionString=ConfigurationManager.ConnectionStrings["CharityManagement_two"].ConnectionString;
This is what you're after
In the config file
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
Then to read the connection string in your code you will do
string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
Don't forget to use using System.Configuration;
Further reading here

Modify Connection String to add host name

How to add host's name into connection string?
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Mapping.WksModels.csdl|res://*/Mapping.WksModels.ssdl|res://*/Mapping.WksModels.msl;provider=Oracle.ManagedDataAccess.Client;provider connection string="data source=MyDb;password=test;persist security info=True;user id=test"" providerName="System.Data.EntityClient" />
</connectionStrings>
MyDb in Data source=MyDb should be the host name (not the db name)
Here's what worked for me:
string conn = "DATA SOURCE=YOURhostname.com:1521/DBNAME;USER ID=YOURUSERNAME;PASSWORD=YOURPASSWORD";
Do not add any additional spaces or quotes. Good luck.

Putting a connection string in the web config

So I'm using C# and I've got the following SQL connection string:
private static string _conn = Properties.Settings.Default.dBizConnectionString;
And I'd like to know if and how I can put it in the web config and app config? Thanks for the help!
Here's a blog post from Scott Forsyth explaining everything:
Using Connection Strings from web.config
The short answer is yes, like so:
<connectionStrings>
<add name="ConnectionStringName" providerName="System.Data.SqlClient" connectionString="Server=ServerName; Initial Catalog=InitialCatalog; User ID=User; Password=Password; MultipleActiveResultSets=True;" />
</connectionStrings>
There's a lot of information out there about connection strings. There are more options you can specify, they can be encrypted, etc. etc., but this will get you started.
Add it to the web config like this:
<connectionStrings>
<add name="myConnectionStringName" connectionString="Data Source=mySqlServerInstance;Initial Catalog=myCatalog;User ID=myUserId;Password=myPassword"/>
</connectionStrings>
Add code to retrieve it:
private static string GetConnectionString()
{
var config = WebConfigurationManager.OpenWebConfiguration("/myProject");
var connections = config.ConnectionStrings;
var settings = connections.ConnectionStrings["myConnectionStringname"];
string connectionString = settings.ConnectionString;
return connectionString;
}

Categories