Creating a Connection String and Working with SQL Server LocalDB - c#

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"/>

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>

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"];

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.

Connection String in web.config fails to connect

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

Categories