I decided to transfer my project from work to home and I am having some trouble with the connection to the database. This one works on work:
web.config:
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=XXXXXX\SQLSERVER2008;Persist Security Info=true;Initial Catalog=esResearch;User ID=XXXXXX; Password=XXXXXX"
providerName="System.Data.SqlClient" />
<add name="esResearchConnectionString" connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;Persist Security Info=True;User ID=XXXXXX;Password=XXXXXX"
providerName="System.Data.SqlClient" />
</connectionStrings>
app.config:
<connectionStrings>
<add name="esResearchModels.Properties.Settings.esResearchConnectionString"
connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;Persist Security Info=True;User ID=XXXXXX"
providerName="System.Data.SqlClient" />
<add name="esResearchModels.Properties.Settings.esResearchConnectionString1"
connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;Persist Security Info=True;User ID=XXXXXX;Password=XXXXXX"
providerName="System.Data.SqlClient" />
<add name="esResearchModels.Properties.Settings.esResearchConnectionString2"
connectionString="Data Source=XXXXXX\SQLSERVER2008;Initial Catalog=esResearch;User ID=XXXXXX;Password=XXXXXX"
providerName="System.Data.SqlClient" />
</connectionStrings>
I guess I do not need all those strings but it works atleast. And this line is used in designer.cs
base(global::esResearchModels.Properties.Settings.Default.esResearchConnectionString2, mappingSource)
I have done the movie sample project on asp.net/mvc and used this connectionstring and this one works on my computer at home.
Web.config:
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
<add name="MovieDBContext"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;database=Movies;User ID=sa;password="
providerName="System.Data.SqlClient"/>
</connectionStrings>
Any ideas?
There are so many different connection strings are available:
General(Windows Authentication):
SqlConnection sql=new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=database;Integrated Security="True");
(SqlServer Authentication):
SqlConnection sql=new SqlConnection("Data Source=.\\SQLEXPRESS;Uid=sa;password=sqlserver;database=databasename");
If you want to know more about connection string, go to:
http://www.connectionstrings.com
Related
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDataSource1"].ToString());
<connectionStrings>
<add name="SqlDataSource1"
connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=SqlDataSource3;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\SqlDataSource3.mdf"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Should that work now?
Is at ConnectionStrings["XXX"] has to be placed Name or Initial Catalog of connectionStrings?
As shown on this page, the index value is the name of the connection string. Therefore your code is correct.
Now wondering why you have posted the question. Does your code not work?
Try this:
<add name="SqlDataSource1" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=SqlDataSource3;Integrated Security=True"/>
and then:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SqlDataSource1"].ConnectionString);
Hope this helps.
I have problem when i try to connect to localdb from visual studio 15.
I have installed SQL Server Express 2016
I folowing this tutorial: http://www.asp.net/mvc/overview/getting-started/introduction/creating-a-connection-string
I create model and context(MovieDBContext) class and try to setup connection:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"
/>
When i try to access to page where i show all movies :
public ActionResult Index()
{
return View(db.Movies.ToList());
}
I get this exception:
I try to edit connection strings like this and also not working:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-MvcMovie-20130603030321.mdf;Initial Catalog=aspnet-MvcMovie-20130603030321;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"
/> </connectionStrings>
I get this excpetion when i use (local)MSSQLLocalDB
What i do wrong ?
The initial Catalog is missing in MovieDBContext connection string.
It needs to be as follows:
<add name="MovieDBContext" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Movies.mdf;Initial Catalog=Movies;Integrated Security=True" providerName="System.Data.SqlClient"/>
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.
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"];
I am working on asp.net c# project, for connection I used:
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True");
but I want to get this connection string to get configure and be like this, so can any one help how to create this kind of connection.
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["itmall"].ConnectionString);
Demo :
<connectionStrings>
<add name="myConnectionString" connectionString="server=localhost;database=myDb;uid=myUser;password=myPass;" />
</connectionStrings>
Based on your question:
<connectionStrings>
<add name="itmall" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True" />
</connectionStrings>
Refer links:
http://www.connectionstrings.com/store-connection-string-in-webconfig/
Retrive connection string from web.config file:
write the below code in your file where you want;
string connstring=ConfigurationManager.ConnectionStrings["itmall"].ConnectionString;
SqlConnection con = new SqlConnection(connstring);
or you can go in your way like
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["itmall"].ConnectionString);
Note:
The "name" which you gave in web.config file and name which you used in connection string must be same(like "itmall" in this solution.)
string connectionstring="DataSource=severname;InitialCatlog=databasename;Uid=; password=;"
SqlConnection con=new SqlConnection(connectionstring)
add this in web.config file
<configuration>
<appSettings>
<add key="ConnectionString" value="Your connection string which contains database id and password"/>
</appSettings>
</configuration>
.cs file
public ConnectionObjects()
{
string connectionstring= ConfigurationManager.AppSettings["ConnectionString"].ToString();
}
Hope this helps.
Add this in your web.config file
<connectionStrings>
<add name="itmall"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-
02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True" />
</connectionStrings>
Add this connection string tag in web.config file:
<connectionStrings>
<add name="itmall"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=D:\19-02\ABCC\App_Data\abcc.mdf;Integrated Security=True;User Instance=True"/>
</connectionStrings>
And use it like you mentioned. :)
It occurs when IIS is not being connected to SQL SERVER. For a solution, see this screenshot: