Failed to read database schema - c#

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>

Related

Creating a Connection String and Working with SQL Server LocalDB

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

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

How to set a proper connection string for EF DB First?

I have an ArgumentException : Keyword not supported: 'metadata'.
I tried many things, read many posts. I dont know what to do...
This is my CS :
<add name="CDPContext"
connectionString="metadata=res://*/CDP_Model.csdl|
res://*/CDP_Model.ssdl|
res://*/CDP_Model.msl;
provider=System.Data.SqlClient
provider connection string='Data Source=(LocalDb)\v11.0;
AttachDbFilename=\CDP.mdf;
Initial Catalog=CDP;
Integrated Security=True;
MultipleActiveResultSets=True;
App=EntityFramework'"
providerName="System.Data.SqlClient" />
Thanks.
the connection string you provided above is for model first.
to use connection string for code first you could write your connection string as below
<add name="CDPContext" connectionString="Data Source=(LocalDb)\v11.0;
AttachDbFilename=\CDP.mdf;
Initial Catalog=CDP;integrated security=True;MultipleActiveResultSets=True;" providerName="System.Data.SqlClient" />
and also go to you context, if there is a throw exception in it, just remove it.
providerName="System.Data.SqlClient" is the wrong provider for DB First
instead use providerName="System.Data.EntityClient" to access the db using the EDM...

Invalid value for key attachdbfilename&500 - Internal server error

this is my connection string on godaddy hosting. By the way I have my database and membership database I makes security for some pages on my website.
All tables in one database Waves database. I used it to make login page for admin panel for my website but when I try to login and write pass & username it makes error that I write it above and this my connection string in web.config .also i have another 2 connection strings but it works OK, only this connection string is not working.
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=180.000.004.53\SQLEXPRESS;Integrated Security=false;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
User ID=id;Password=Password;Encrypt=False;
providerName="System.Data.SqlClient" />
<add name="WavesEntities"
connectionString="metadata=res://*/App_Code.WaveModel.csdl|res://*/App_Code.WaveModel.ssdl|res://*/App_Code.WaveModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=004.00.004.03;User ID=id;Password=password;Encrypt=False;Connect Timeout=15;Initial Catalog=Waves;Integrated Security=false;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
<add name="WavesConnectionString"
connectionString="Data Source=000.100.10.0;User ID=id;Password=password;Encrypt=False;Connect Timeout=15;Initial Catalog=Waves;Integrated Security=false"
providerName="System.Data.SqlClient" />
</connectionStrings>
After a minor reformatting of your code, it seems pretty obvious that the first connection string is invalid, and may be causing your problem:
This part should presumably be placed within the quotes, and be part of the connection string:
User ID=id;Password=Password;Encrypt=False;

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