Why no connection error if wrong connection - sql

I am using EF and localDB to develop an app.
At a point I get no data from my queries, although my program worked without crashing.
Then I notice the localDb connection string was wrong and then I got again data from my repositories.
I am using a .mdf file as my "test-DB".
My question is why I get no connection error although there is no file or database with the given name?
The connection string in app.config is
<add name="T4Context"
connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=C:\TestDb.MDF;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
providerName="System.Data.SqlClient"/>

Related

How do I connect to a Local database using asp.net Mvc4 and visual studio express 2012?

I am following the Mvc4 OdeToFood tutorial on Pluralsight, which uses asp-net Mvc4 and visual studio express for web 2012.
I am trying to set up a small local database to hold some information so that I can proceed with the tutorial, but I am having trouble getting the database versions and connection string right, I have never done anything like this before.
I have been going fine until a few days ago when I tried to connect to a Local database.
Unlike in the tutorial, In my web.config file I had no connection string tags, The v11.0 database instance was specified in the tutorial, which from google seems to be a local database used for development purposes. so I looked up what I needed and entered into the Web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=
(LocalDb)\v11.0;Initial
Catalog=OdeToFoodDb;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFoodDb.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
This would not work for me, so upon more googling I tried:
<connectionStrings>
<add name="OdeToFood" connectionString="Data Source=
(LocalDb)\MSSQLLocalDB;Initial
Catalog=aspnet-OdeToFood;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFood.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
This initially did work, but when I tried to modify the tables, there was a database version error and I could not use the database crud functionality with the program. I searched as to why this wouldn't work and looked through the error files and I found that once you change a .mdf file to v13.0 (from googling I believe MSSQLLocalDB is v13.0) it cannot be reverted to a v11.0 .mdf version. So I guess vs express 2012 can only use v11.0?
If anyone can help me or point me to somewhere where I can learn how to connect to this database I would be really grateful.

ArgumentException: Keyword not supported: 'server'

I have an Asp.net MVC5 application and have published it to Microsoft Azure. I first migrated my .mdf files toSql Azure Databases. The database connection string provided in the Azure Portal is not working.
[ArgumentException: Keyword not supported: 'server'.]
My connection string is as follows web.config
connectionString="
Server=tcp:dbprojectserver.database.windows.net,1433;
Initial Catalog=db_project;
Persist Security Info=False;
User ID=username#servername;
Password=kenth&&123;
Encrypt=True;
TrustServerCertificate=False;
Connection Timeout=30;
"
I believe so there is something wrong with this connection string. Any help regarding that is highly appreciated.
EDIT
Reading from here SQL Server Connection Strings and following EF Db first or Model first connection string example
<add name="ConnectionStringName"
providerName="System.Data.EntityClient"
connectionString="metadata=res://*/ ContextClass.csdl|res://*/ ContextClass.ssdl|res://*/ ContextClass.msl;provider=System.Data.SqlClient;provider connection string="Data Source=ServerName;Integrated Security=False;User Id=userid;Password=password;MultipleActiveResultSets=True"" />
This is what I am using according to above example
<add name="ProjectEntities" connectionString="metadata=res://*/ ProjectWeb.Models.User.csdl|res://*/ ProjectWeb.Models.User.ssdl|res://*/ ProjectWeb.Models.User.msl;provider=System.Data.SqlClient;provider connection string="Data Source=tcp:dbprojectserver.database.windows.net,1433;Integrated Security=False;User Id=username#servername;Password=kenth$$123;MultipleActiveResultSets=True"
" providerName="System.Data.EntityClient"/>
It says
Keyword not supported 'data source'
I had the same problem when specified EF connection string on the Azure Portal in Application Service settings (Application settings -> Connection strings).
To fix it:
Replace " with "
Specify connection string type as Custom but not as SQL Database.
Also, as I can see you do not have Initial Catalog in your latest example. You need to add it and specify your database in this parameter.
Finally your connection string for application settings in Azure portal should look like this:
metadata=res://*/ ProjectWeb.Models.User.csdl|res://*/ ProjectWeb.Models.User.ssdl|res://*/ ProjectWeb.Models.User.msl;
provider=System.Data.SqlClient;
provider connection string="Data Source=tcp:dbprojectserver.database.windows.net,1433;Initial Catalog=<your database>;Integrated Security=False;User Id=username#servername;Password=kenth$$123;MultipleActiveResultSets=True";

Adding EntityClient into Connection String SqlClient

I am trying to add a Entity Framework project with object relational mapping (ORM) into my standard SQL Server Project (MVC ASP.Net).
I think the problem is in the Connection String in the Web.config file:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Project2-20160209033141.mdf;Initial Catalog=aspnet-Project2-20160209033141;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="BlogEntities" connectionString="metadata=res://*/Entities.BlogContext.csdl|res://*/Entities.BlogContext.ssdl|res://*/Entities.BlogContext.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\aspnet-Project2-20160209033141.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
DefaultConnection is the main project im working with and BlogEntities is what i'm trying to add.
When i run the project the BlogEntities it seems to work fine, but im having trouble running the Default Database, as It wont let me log into user accounts and other tasks that have worked before.
Error Example->
Is there an easier way to add this into my project without disrupting the current database?
Thanks in advance.
Entity Framework User system uses the default connection, you cant use another connection to change anything related to users.

Strange MVC Error Using GearHost

When I run my program on my local machine it works just fine. However, when I try to interact with my database via GearHost, I get this nugget of an error:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ComponentModel.Win32Exception: The system cannot find the file specified.
Everywhere I have gone, the forums say it is a connection string error.
If that is the case here is my connection string:
<add name="GameStoreEntities" connectionString="metadata=res://*/Models.GamesModel.csdl|res://*/Models.GamesModel.ssdl|res://*/Models.GamesModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\GameStore.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
If anyone has any idea about what is wrong with the connection string, I would appreciate your feedback.
GearHost does not seem to have support for LocalDB.
So you would have to create a database on their admin panel and use the credentials of the default user that is created to write your connection string. You could also create a new user for the database and use those credentials instead.
Either way your new connection string should be something like this
<add name="GameStoreEntities"
providerName="System.Data.SqlClient"
connectionString="Data Source=DATABASEHOST;Initial Catalog=DATABASENAME;Integrated Security=False;User Id=USERNAME;Password=PASSWORD;MultipleActiveResultSets=True" />
where you just replace the DATABASEHOST with the hostname for the database. You would see this at the bottom of the page after creating the database.
You get the PASSWORD by clicking the uncover(eye) button after creating the database
If you have data in the localDB that you want to sync to the database you have created on GearHost you can use the Data Comparison and Schema Comparison tools in Visual Studio to update the target database.

login failed connecting to SQL Server with AttachDbFilename and User Instance

I am developing ASP.NET 2.0 application using SQL Express 2005. I have attached my database with the application.
The Connection string:
<add name="WCMIRConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename="\App_Data\WCMIR.mdf";Integrated Security=True;Trusted_Connection=no;User Instance=True" />
When trying to connect the following error appears :
Cannot open database "dp-name" requested by the login. The login failed.
Login failed for user 'Machine\useID'.
How can this error be resolved?
First I think the connection string should be with |App_Data| not \App_Data\
Second Make sure that this user has permission on this folder and on this DB
You shouldn't need the AttachDbFileName if your database is already attached, but you should include InitialCatalog to indicate which database you would like to connect to.
Problem Solved
Connection string must be like :
Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|DB.MDF;" +
"Integrated Security=True;User Instance=True";
Just try to use "|" instead of backslash in your code.