Connecting to database in Visual Studio Express 2013 for Web - c#

I'm using Visual Studio Express 2013 for web and I'm following this tutorial : http://www.codeproject.com/Articles/791740/Using-AngularJs-ASP-NET-MVC-Web-API-and-EntityFram which specifies this connection to connect ot the db:
<add name="MainDb" connectionString="Server=localhost; Database=SimpleTaskSystemDb; Trusted_Connection=True;" providerName="System.Data.SqlClient" />
but when I run Update-Database I get this error:
A network-related or instance-specific error occurred while
establishing a connection to SQL Server. The server was not found or
was not accessible. Verify that the instance name is correct and that
SQL Server is configured to allow remote connections. (provider: SQL
Network Interfaces, error: 26 - Error Locating Server/Instance
Specified)
I've also finished the Music Store app which is working fine and the connection there is different:
<add name="MusicStoreEntities" connectionString="Data Source=|DataDirectory|MusicStore.sdf" providerName="System.Data.SqlServerCe.4.0"/>
I tried a similar connection string but that didn't work either.
No Entity Framework provider found for the ADO.NET provider with
invariant name 'System.Data.SqlServerCe.4.0'. Make sure the provider
is registered in the 'entityFramework' section of the application
config file.
However the Music Store app.config file does not reference System.Data.SqlServerCe.4.0 but I added it to this app anyway and then I get back to my original error.
Any help would be greatly appreciated.

Here are some of the sample connection strings I have described every connection string with full details
For Local Sql server (BuildIn Sql Server Express into Visual Studio).
For Sql Server instance which comes default with visual studio you can use following connection string
<connectionStrings>
<add name="ConnectionStringName" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=YourDatabaseName;Integrated Security=True"
providerName="System.Data.SqlClient" />
For Sql Server Which Has been installed seperately such as Evaluation Edition etc
<connectionStrings>
<add name="ConnectionStringName" connectionString="Data Source=YourPCName-PC\ServerInstanceName;Initial Catalog=YourDatabaseName;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
For CompactEdition of Sql server you can use provider name as
providerName="System.Data.SqlServerCe.4.0"

Related

I want to create a connection string with .mdf file in window forms application

This is my connection string:
<add name="EntityContext"
connectionString="data source=.\SQLEXPRESS;
AttachDbFileName=|DataDirectory|\PasLockProduction.mdf;
Database=PasLockProduction;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient" />
I get this error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
Most likely your DataDirectory is not what you think it is. In the Interactive window or even in your code where you can set a breakpoint do this to see where that directory is:
var ddir = AppDomain.CurrentDomain.GetData("DataDirectory");
To set the directory to where your database file is :
AppDomain.CurrentDomain.SetData("DataDirectory", "<path to file...");
Try someting like this.
<connectionStrings>
<add name="EntityContext" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=c:\folder\PasLockProduction.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";/>
</connectionStrings>

Entity Framework code first approach connection string configuration

I've implemented Identity & OWIN authentication to my existing asp.net web form application. I referred to the site:
https://learn.microsoft.com/en-us/aspnet/identity/overview/getting-started/adding-aspnet-identity-to-an-empty-or-existing-web-forms-project
I am having problem with connection string for entity framework code first approach. I referred to the site: http://odetocode.com/Blogs/scott/archive/2012/08/14/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx for help. But none of them is working.
I use VS 2015, SQL Server 2014 & Windows 10 OS.
For SQL Server authentication (Windows authentication) my server name is : DESKTOP-07C2G55.
I changed my connection string as told in the sites but none of them worked. My first connection string is::
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=DESKTOP-07C2G55;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient"
type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
On using this connection string when I try to register any user I am getting error as:
Directory lookup for the file "c:\users\myPC\documents\visual studio 2015\Projects\RENTAL\RENTAL\App_Data\WebFormsIdentity.mdf" failed with the operating system error 5(Access is denied.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
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.Data.SqlClient.SqlException: Directory lookup for the file "c:\users\myPC\documents\visual studio 2015\Projects\RENTAL\RENTAL\App_Data\WebFormsIdentity.mdf" failed with the operating system error 5(Access is denied.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
Then I run the following commands in my VS console. I get:
PM> Get-Service | Where-Object {$_.Name -like '*SQL*'}
Status Name DisplayName
------ ---- -----------
Running MSSQLFDLauncher SQL Full-text Filter Daemon Launche...
Running MSSQLSERVER SQL Server (MSSQLSERVER)
Running MSSQLServerOLAP... SQL Server Analysis Services (MSSQL...
Stopped SQLBrowser SQL Server Browser
Stopped SQLSERVERAGENT SQL Server Agent (MSSQLSERVER)
Running SQLWriter SQL Server VSS Writer
PM> SqlLocalDb info
MSSQLLocalDB
Then I changed my connection string, chaned the datasource to Data Source=MSSQLLocalDB:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=MSSQLLocalDB;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I get the error
Invalid value for key 'attachdbfilename'.
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.ArgumentException: Invalid value for key 'attachdbfilename'.
Again I changed my connection string to Data Source= .\SQLEXPRESS
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=WebFormsIdentity;AttachDbFilename=|DataDirectory|\WebFormsIdentity.mdf;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
I got the error:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
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.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
I can not figure out what is the problem whether the cause of the problem is connection string or any where else in the code. In other solutions, it is mentioned to edit DBContext Class, but in the tutorial there is no such class created. I am not using MVC, simple web form application trying to implement Identity and OWIN module. Please help me.
The output from your PowerShell script shows that you have a default instance (with the "instance" name of MSSQLSERVER) of SQL Server running:
Running MSSQLSERVER SQL Server (MSSQLSERVER)
This means, you can use Data Source=. (or Data Source=(local)) for your server name (you must not define an instance name to connect to the default instance), define the database name in Initial Catalog, and drop that AttachDbFileName crap once and for all (and let SQL Server handle all the file-related ins and outs - don't mess around with .mdf database files yourself!):
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=.;Initial Catalog=WebFormsIdentity;Trusted_Connection=Yes;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>

ASP.NET MVC : change connection from SQL Server 2012 to SQL Server 2014

I have an existing ASP.NET MVC project which previously was connecting to SQL Server 2012. But now I have only SQL Server 2014 installed and it can not connect.
connectionString="Data
Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspn‌​et-Vidly-20160330105‌​730.mdf;Initial
Catalog=aspnet-Vidly-20160330105730;Integrated Security=True"
My Sql Server works ok, I have version problem, the project is taken from another pc Which had installed SQL-Server 2012 , Now I am trying to run with SQL-Server 2014 installed !!!
It is trying to connect to SQL Server 2012. How can I fix this?
This question may already have an answer here: - I could not find anything helpful.
Looks like there is a problem with your connection string in the application.
This is the standard connection string to connect local DB:
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFileName=|DataDirectory|\DatabaseFileName.mdf;InitialCatalog=DatabaseName;Integrated Security=True;MultipleActiveResultSets=True" />
An example with credentials:
<add name="ConnectionStringName"
providerName="System.Data.SqlClient"
connectionString="Data Source=ServerName;Initial Catalog=DatabaseName;Integrated Security=False;User Id=userid;Password=password;MultipleActiveResultSets=True" />
For more information visit: https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx

host a database website with entity framework in the production server

I developed an asp.net website (using webforms) and for database connection I used Entity Framework. On my local computer everything is working fine. Now I try to host it I am having a problem. I have copied my files to the production server and attached my SQL Server database and set my connection string to:
<add name="ConnectionString"
connectionString="Data Source=SQL5003.Smarterasp.net;Initial Catalog=DB_9C4561_WFormBlog;User Id=DB_9C4561_WFormBlog_admin;Password=mydbpwd"
providerName="System.Data.EntityClient" />
but when I test my website am getting error:
Keyword not supported: 'data source'
When I removed "data source", it started complaining of "Initial catalog" keyword not supported.
Please help me out. You can refer me step by step tutorial or material that can put me through. Thanks in advance!
Try the below format, I have my database on smarterasp.net and it works well
<connectionStrings>
<clear />
<add name="ConnectionString" connectionString="Persist Security Info=False;database=DB_9C4561_WFormBlog;server=SQL5003.Smarterasp.net;user id=DB_9C4561_WFormBlog_admin;password=mydbpwd;Current Language=English;Connection Timeout=60;" />
</connectionStrings>

Not able to create database using entity framework

I am trying to use code-first approach of Entity Framework in my asp.net mvc application. Below is my connection string which was autogenerated...
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
Whenever I press F5, the following error occurs:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)
What am I doing wrong??
I have SQL Server 2008 Enterprise edition installed. I am trying to connect to SQL Server using Windows authentication. I am also following a video tutorial so there may not be anything wrong with code
IF you have SQL Server Enterprise edition, you most likely won't have it installed as SQLEXPRESS instance (that's the default for the SQL Server Express installations that come with Visual Studio).
More likely than not, you have the unnamed default instance - so try to use this connection string:
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.;Database=YourDatabaseName;Integrated Security=SSPI;
providerName="System.Data.SqlClient" />
</connectionStrings>
Not sure if your application really uses the ApplicationServices connection string - if not, adapt the one your app is using! Also, you'll need to replace the YourDatabaseName with the actual database name that you're using in your application.
If you are trying to connect to an instance of SQL server that's installed on your machine, you'll want to use a connectionString formatted to use that instead. Your current connectionString is trying to create a database file from which to work, rather than building a database in your SQL server instance.
Try something like this;
connectionString="data source=localhost;user id=<user with dbo access>;password=<user password" providerName="System.Data.SqlClient" />

Categories