Not able to read connection string from app config - c#

This is my connection string in App.config
<connectionStrings>
<add name="CostAllocationEntities2" connectionString="Data Source=PC210090\SQLEXPRESS; Initial Catalog=master;Persist Security Info=True;
User ID=sa;password=password-1;multipleactiveresultsets=True; " providerName="System.Data.SqlClient"/>
</connectionStrings>
ServerConnection connection = new ServerConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString);
Server sqlServer = new Server(connection);
Database db = sqlServer.Databases[strDBName];
Exception : Failed to connect to server Data Source=PC210090\SQLEXPRESS; Initial Catalog=master;Persist Security Info=True;
User ID=sa;password=password-1;multipleactiveresultsets=True; .

What are you trying to do, not sure but change your connection string
connectionString="Data Source=PC210090\\SQLEXPRESS;
Initial Catalog=master;; don't connect to master DB rather provide a user DB name like Sample.
<connectionStrings>
<add name="CostAllocationEntities2" connectionString="Data Source=PC210090\\SQLEXPRESS; Initial Catalog=Sample;Persist Security Info=True; User ID=test;password=test;multipleactiveresultsets=True; " providerName="System.Data.SqlClient"/>
</connectionStrings>
In your code behind
Once you have the connection string create a instance of SqlConnection like
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString);
Open the connection
connection.Open();

Related

Failed to read database schema

im newbie in c# and i want fetch data from mssql local engine with peta poco orm but i get this error
image of error im getting
and here is my connection string of my class library:
<connectionStrings>
<add name="School" connectionString="Data Source= MSSQLSERVER;Initial Catalog=School;user id=sa; Password=1qaz!QAZ;" providerName="System.Data.SqlClient" />
</connectionStrings>
my database name is School, i tried server too, and my server name which i use to connect to ssms is DESKTOP-KVEJKDS
thanks for your help!
Just replace your Data Source name with your Server name:
<connectionStrings>
<add name="School" connectionString="Data Source= DESKTOP-KVEJKDS\SQLEXPRESS;Initial
Catalog=School;user id=sa; Password=1qaz!QAZ;" providerName="System.Data.SqlClient" />
</connectionStrings>

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>

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.

How to solve database initialization error?

I have been testing out different things and have concluded that the following error is the reason my database will not initialize and throws an exception. The exception occurs when I reference the database and initialize it. My Web.config connection string is fine as you can see. I am using Entity Framework 6, MSSQL, and C#. Any help would be appreciated.
Exception:
Cannot open database \"xxx\" requested by the login. The login failed.\r\nLogin failed for user 'xxxx'.
Connection String:
<add name="WebDBContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=WebDB;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
Code:
private SchoolContext db = new SchoolContext();
public ViewMethod Method()
{
var students = from s in db.Students select s; //EXCEPTION IS THROWN HERE
}
You should mention userid and password in connection string
<add name="WebDBContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=WebDB;Integrated Security=SSPI;User ID=xyz;pwd=top$secret;" providerName="System.Data.SqlClient"/>
Your connection string is not in the format required by EF, it should actually look similiar to this:
<add name="XYZ" connectionString="metadata=res://*/ModEntity.csdl|res://*/ModEntity.ssdl|res://*/ModEntity.msl;provider=System.Data.SqlClient;provider connection string="Data Source=SomeServer;Initial Catalog=SomeCatalog;Persist Security Info=True;User ID=Entity;Password=SomePassword;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
Also use linq query.
var students = db.Students;
<connectionStrings>
<add name="WebDBContext" connectionString="metadata=res://*/Models.ModelName.csdl|res://*/Models.ModelName.ssdl|res://*/Models.ModelName.msl;provider=System.Data.SqlClient;provider connection string="data source=servername;initial catalog=databasename;user id=username;password=password;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
replace the ModelName to your Model and change connections on your db.

Categories