Connection string using local SQL Server - asp.net

How to write the connection string to local SQL Server database using web.config?
Here is my connection string in web.config. But when I open local/demo, it said:
login failed for user ''.
Actually I'm using Windows authentication, so I thought it doesn't need user and password.
<connectionStrings>
<add name="HRISContext"
connectionString="data source=.;initial catalog=BPSDEMO;Pooling=false;persist security info=True;user id=;password=;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient" />
</connectionStrings>

If you want to use Windows authentication, then yes, you must not include any user id and pwd in your connection string - just remove those completely. But you must include the Integrated Security=SSPI setting instead.
Try this:
<connectionStrings>
<add name="HRISContext"
connectionString="data source=.;initial catalog=BPSDEMO;Integrated Security=SSPI;MultipleActiveResultSets=True;Pooling=false;persist security info=True;App=EntityFramework"
providerName="System.Data.SqlClient" />
</connectionStrings>

Related

in asp.net mvc project using localhost instead .\sqlexpress in connection string

I installed Sql Server Express but I need to use localhost as Datasource in connection string.
Here is the connection string now:
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS.../>
I want to use like this:
<add name="DefaultConnection" connectionString="Data Source=localhost
Is there a way to use localhost alias instead of ".\sqlexpress" in connection string. I work in a team project and the other people use connection string like that (they have installed sql server - not express)
You have to mention only localhost and database name like below
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=DatabaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
</connectionStrings>

Connection failed to SQL Server Express using user with SQL Server authentication

I have IIS and SQL Server Express on the same machine. After deployment of web site to this environment I get this error:
Cannot open database "MyDB" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\USER'.
Here are my connection strings where I set SQL Server user
<connectionStrings>
<add name="myCS1"
connectionString="data source=172.20.3.20\SQLEXPRESS;initial catalog=MyDB;user id=SomeUser;password=SomeUser###;integrated security=True;MultipleActiveResultSets=True;"
providerName="System.Data.SqlClient" />
<add name="myCS2"
connectionString="metadata=res://*/CLModel.csdl|res://*/CLModel.ssdl|res://*/CLModel.msl;provider=System.Data.SqlClient;provider connection string="data source=172.20.3.20\SQLEXPRESS;initial catalog=myDB;persist security info=True;user id=SomeUser;password=SomeUser###;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>
I want to use SQL Server user for connection to the database but not AppPool user.
I specified login and password but it seems that this info is not taken in account and in error message I still see IIS user but not sql.
What I missed?
Well, you specify both - explicit user id and password, and Integrated Security=True - in your myCS1 connection string:
data source=172.20.3.20\SQLEXPRESS;initial catalog=MyDB;
user id=SomeUser;password=SomeUser###;integrated security=True;
In this case, the integrated security wins over your user - you need to specify only the user id and password and get rid of the integrated security - so use this:
<connectionStrings>
<add name="myCS1"
connectionString="data source=172.20.3.20\SQLEXPRESS;initial catalog=MyDB;user id=SomeUser;password=SomeUser###;MultipleActiveResultSets=True;"
providerName="System.Data.SqlClient" />
<add name="myCS2"
connectionString="metadata=res://*/CLModel.csdl|res://*/CLModel.ssdl|res://*/CLModel.msl;provider=System.Data.SqlClient;provider connection string="data source=172.20.3.20\SQLEXPRESS;initial catalog=myDB;persist security info=True;user id=SomeUser;password=SomeUser###;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
</connectionStrings>

database is not connecting in iis

I have two database.
<add name="OdonovanTestConnectionString" connectionString="Data Source=ODO-SRV004\;Initial Catalog=OdonovanTest;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="rackleyConnectionString1" connectionString="Data Source=ODO-SRV004\;Initial Catalog=rackleyTest;Integrated Security=true" providerName="System.Data.SqlClient" />
When I host my asp .net application in iis. The first database is connecting which is odonovantest.
But the database rackletTest is not connecting. Both database lies in same sql server.
Can any one give me solution?
you can call your connection string explicitly as code bellow:
string connectionString = SampleApplication.Properties.Settings.Default.rackleyConnectionString1;
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
// put code here
}
Make Sure you are using Windows Authentication or Sql Server Authentication.
In Web.config:
For Sql Server Authentication:
For Windows Authentication:
And then your have to define in codebehind also.

Sql server express and connection string in web.config

i have connection string stored in web.config file but it is giving me the error.
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DbProduct.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
where is the bug in the connection string. i have stored my db DbProduct.mdf in app_data folder. i have never use SQLEXPRESS. so i need guide line. thanks
this AttachDbFilename=|DataDirectory|\DbProduct.mdf
should be AttachDbFilename=|DataDirectory|DbProduct.mdf
You don't need to add \
Have a look at this Using connection strings from web.config in ASP.NET v2.0

ASP.NET System.Data.EntityClient connection string help

I'm running ASP.NET MVC on a shared server and I'm having problems connecting to SQL via System.Data.EntityClient. Below is the connection string that my hosing provider gave me to connect to SQL and the one that VS configured for my local machine during development, what should my connection string look like when I deploy to the server?
From my hosting provider:
<add name="WeddingsDBEntities"
connectionString="data Source=<server name>; Initial Catalog=<db name>; User ID=<user ID>; Password=<password>;"
providerName="System.Data.EntityClient"/>
From VS (during development):
connectionString="metadata=res://*/Models.WeddingsModel.csdl|res://*/Models.WeddingsModel.ssdl|res://*/Models.WeddingsModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\WeddingsDB.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"
Thanks!
You have to wrap the connection string instide an entity connection string which is in the format of
<add name="Name"
connectionString="metadata=<Conceptual Model>|<Store Model>|<Mapping Model>;
provider=<Underlying Connection Provider>;
provider connection string="<Underlying ConnectionString>""
providerName="System.Data.EntityClient"/>
Instead of:
<add name="WeddingsDBEntities"
connectionString="data Source=<server name>; Initial Catalog=<db name>; User ID=<user ID>; Password=<password>;"
providerName="System.Data.EntityClient"/>
Use this:
<add name="WeddingsDBEntities"
connectionString="metadata=res://*/Models.WeddingsModel.csdl|res://*/Models.WeddingsModel.ssdl|res://*/Models.WeddingsModel.msl;provider=System.Data.SqlClient;provider connection string="data Source=<server name>; Initial Catalog=<db name>; User ID=<user ID>; Password=<password>;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"/>
Change the provider from entityclient to sqlclient (assume code first EF).
providerName="System.Data.EntityClient" />
to
providerName="System.Data.SqlClient" />
<add name="Name" connectionString="metadata=res://*; provider=System.Data.SqlClient; provider connection string='; data source=YOURIP;Initial Catalog=YOURDB;Persist Security Info=True;User ID=YOURUSER;Password=YOURPASSWORD; Connect Timeout=15;Encrypt=False;Packet Size=4096;MultipleActiveResultSets=True'"
providerName="System.Data.EntityClient"/>
Hope this Will help you!!

Resources