I'm going through the ASP.NET "Contoso University" Tutorial found here.
The problem starts when i try to view the database. Following directly from the tutorial, when i go to open the database (SchoolContext), i get this error mesage.
Cannot open database "ContosoUniversity1" requested by the login. The login failed.Login failed for user 'machine\user'.
So on the DB i went to "Modify Connection" and hit OK without changing anything. Then I was able to view the database, tables, views, etc. But there was no info available, so continuing from the tutorial I should have seen the Student entity, but it was not there. And when i run the program and try go to website/students, i get a 404 saying the resource cannot be found.
The connection string i'm using from the tutorial
<connectionStrings>
<add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity1;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>
I'm running VS 2013 Community Express. I do not have SQL Server Management Studio installed or any related programs.
I'm on the admin user for the computer.
Is this a problem with my machine or user rights? Do i need to install another program?
I think you are using the sqlserver express edition which comes with Visual Studio installation. Try adding AttachDBFilename='path of .mdf file of database' also in your connection string.
<connectionStrings>
<add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=ContosoUniversity1;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\ContosoUniversity1.mdf" providerName="System.Data.SqlClient"/>
</connectionStrings>
Go to that connection in Server Explorer. Go to Properties and use the connection string from there.
Related
I have developed an application that uses SQL database. My challange is to be able to ship the database with the app itself. When i package my mdf file with the setup, it gets deployed. However it is not able to connect to it.
I have made following as prerequisites
When installing, it downloads and installs all the prerequisites.
I am also using following connection string to connect to it.
<connectionStrings><add name="MyDBContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=" Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDB.mdf; MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
This setup works fine when installed on my machine. Any idea how this will work on simple machines?
Finally I got it working after changing many things. First off, the connection string itself. It was
<connectionStrings><add name="MyDBContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=" Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\MyDB.mdf; MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
But since only SQL Express 2012 is being installed on the destination machine (See the picture in question above) during setup, this was not going to work. So changed it such that it uses SQLServer Express's default instance name
<connectionStrings><add name="MyDBContext" connectionString="metadata=res://*/MyModel.csdl|res://*/MyModel.ssdl|res://*/MyModel.msl;provider=System.Data.SqlClient;provider connection string=" Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|MyFolder\MyDB.mdf; User Instance=True; Integrated Security=SSPI; MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
Please note that i have added a folder after |DataDirectory|. Idea is to create a folder within AppData so it would end up creating a folder in C:\users\me\AppData\MyFolder\MyDB.mdf
I also set the User Instance as true because otherwise it wasn't working from AppData folder.
Next I added the code to update the DataDirectory to my desired location, which is in AppData folder as
AppDomain.CurrentDomain.SetData("DataDirectory",Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
I did this change in the main of my program file
I have created a setup of my vb.net win application and installed it successfully on another pc. The database file is also restored in other pc SQL server but the problem is that when i run my app it doesn't connect to the SQL server and throws the error
a network related or instance specific error occurred while establishing a connection to sql server
I tried alot but its still not getting fixed. All SQL Server instances are running and i have checked and restarted many time but i wonder why this problem is occuring because in my main pc this app is running perfectly fine.
Any solutions for this?
Regards
Update//
The Port is enabled and its already 1433. This is the app.config code:
<configuration>
<configSections>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="dbx" connectionString="Data Source=SERVER;Initial Catalog=DemoDb;User Id=sa;password=123456#"
providerName="System.Data.SqlClient" />
<add name="db.My.MySettings.MasterDemoConnectionString"
connectionString="Data Source=SERVER;Initial Catalog=Demo;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
Did you enable TCP/IP in the configuration manager .Also, you need to configure a TCP port (1433) on which the SQL Server service is listening. If you accessing database via network, then you need to open specified port in the firewall. Can you provide us your connection string?
Try Data Source = 127.0.0.1
I assume you have checked your account name and password for the SQL server.
Is your SQL Server running? If not type services.msc in Run (ctrl + r). And look for the SQL Server (MSSQLSERVER), right-click it and then click start.
Usually the format needed to specify the database server is machinename\instancename.
Please try below.. hope this will help you.
<add name="dbx" connectionString="Data Source=machinename\instancename;Initial Catalog=DemoDb;User Id=sa;password=123456#"
providerName="System.Data.SqlClient" />
In the sql configuration manager check if the protocols have TCP/IP enabled. Also check if the sqlserver browser service is running on the machine where sql server is installed.
I also noticed that after adding a port to windows firewall you sometimes have to stop and start it again before it comes into effect.
To quickly test a connectionstring do the following.
Create an text file anywhere and give it extension .udl
Now doubleclick on it and the microsoft datalink property window will open, here you can enter your values and use the "test connection" button to see if it works.
Finally, if you get it working, you can open this file in notepad and there you will find a complete connectionstring.
If you will checkout few things then I think that your problem will be solve.
*Remove your Double Connection string from app.config *
step 1 : Check you Connection string (Data Source is very important because u have shifted your database to another pc thats why)
<add name="dbConnectionString" connectionString="Data Source=.; Initial Catalog=dbFactory;User ID=sa; password=user#123"
providerName="System.Data.SqlClient" />
step 2 : clean your project solution
Right Click on solution and click on clean then your old bin file remove and build again your project then your new bin file create in your for your setup.
step 3 : Is your SQL Server running?
If you want to check then press window + r and type services.msc in Run. And find your SQL Server (MSSQLSERVER) and view status of this service and if status will come stop/pause then press right-click on this service and choose start then status will change in start.
step 4 : TCP/IP
If you are using LAN for accessing database then check you have enable TCP/IP in the configuration manager
I am using Visual Studio 2015, Entity Framework v6, and SQL Server 2016 Express. In the past I created a database using a SqlConnection and SqlCommand and stuff the SQL into a string.
Now, I am teaching myself EF6 on Entity Framework Tutorial. On the simple code-first example (very simple), I literally copy and paste my code but still do not see the database created in SSMS. Neither does my code throw me any error.
Instead of pasting the code, I did a screenshot. I hope someone can point out what I am or the tutorial is missing.
[EDIT]
Following Sampath's suggestion, I end up getting the following error:
[EDIT - Solved, sort of]
I apply the same code to another machine of same setup and the code works. So I suspect there are some corruption in the SQL Server or perhaps some registry is incorrect. I uninstall EVERY SQL Server version and related tools, delete all folders and files manually, then freshly reinstall SQL Server Express 2016 and tools. Then my code works.
I don't see this as a solution, but if someone can suggest what may have cause this problem I will try to recreate it or post a real solution to it.
You have to give the connection string name on the web.config file as shown below.
Context :
public SchoolContext(): base("MySchoolDB")
{
}
App.config file
<add name="MySchoolDB" connectionString="Server=localhost;
Database=YourDBName;Trusted_Connection=True;" providerName="System.Data.SqlClient" />
You can get more details here : Database Initialization
You have to add entity to your database after configuring connection string, then DBContext will create database. Here is a connection string example:
<add name="Name" connectionString="Data Source=.; Initial Catalog=yourdbName; Integrated Security=True; MultipleActiveResultSets=True;"
providerName="System.Data.SqlClient" />
I have started a new project which i will be displaying the data in the grid view, where i have to consume the .mdf file. Here the problem starts. I am able to open the .mdf file in sql data base, but not by selecting as servertype: "database engine" but by selecting "SQL server compact edition", only then i am able to open the .mdf file in sql studio and create the tables and everything.
I have creted tables and everthing, and now comes the part where i have to give the connection string in the web.config file. I have defined as below.
connstring:
connectionString="Data source=.\SQLEXPRESS\v11.0;Integrated Security=SSPI;AttachDBFilename=C:\Users\usr\Downloads\InterviewSolution\Backup\myproject\App_Data\myproject.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
and i am not able to connect. I have tried to test the connection while configuring the database schema to sqldatasource, but it is failing with below error.
error: "sql network interfaces error 52"
I thought it was permissions issue, and gave full permissions to the file by adding NFS account to it. but still the same, not able to connect through project.
Can anyone pls help me on this, why is the file able to connect to sql studio, but was not able to connect through project.
Is my connection string wrong ??
I always use this template for this case of connection string:
<connectionStrings>
<add name="MyConn"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\myproject.mdf;
Integrated Security=True;
User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
What providerName should be used in Web.config file for MS SQL Compact Edition in MVC 4 in Visual Studio 2013?
Introduction
When I use:
<connectionStrings>
<add name="MusicStoreEntities"
connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>
my website database works and Genre objects are retrieved from database and displayed
after I change of providerName to providerName="System.Data.SqlClient like this:
<connectionStrings>
<add name="MusicStoreEntities"
connectionString="Data Source=|DataDirectory|MvcMusicStore.sdf"
providerName=providerName="System.Data.SqlClient"/>
</connectionStrings>
I get:
So WHY I want to change anything?
Actual problem:
If I leave providerName="System.Data.SqlServerCe.4.0" when I want to add new controller by this menu:
I get:
but if I set providerName="System.Data.SqlClient the StoreManagerController is generated but the database does not work at all.
Question: What should I do to generate StoreManagerController? I follow this tutorial: http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-5 and link to the working project just before that step it: http://www.speedyshare.com/RGdqH/MvcMusicStore.zip
So bare in mind the "Mvc Music Store" was MVC 101 since the beginning of MVC. Given there have been a lot of changes, the chances of the documentation/walk-throughts being a little off are unfavorable. With that said:
The T4 template for the data-driven controller is obviously failing due to your connection string. This is most likely because SQL Express (in it of itself) has gone through a lot of cycles and is now following LocalDB. Given you're using MVC4, it's probably a good time to upgrade, and this means you can have all the benefits of SQL Express (including the System.Data.SqlClient provider) without the possible headaches from previous revisions.
With that said, change your connection string to use the new format:
<connectionStrings>
<add name="MusicStoreEntities"
connectionString="Data Source=(localdb)\v11.0;Integrated Security=true;AttachDbFileName=|DataDirectory|MvcMusicStore.mdf"
providerName="System.Data.SqlClient" />
</connectionStrings>
And (probably) re-run your DatabaseInitializer (if I recall correctly, this should happen automatically since it's using EF. If not, you can go to your Package Manager console and run Update-Database).
This now gives you the provider the Controller template wants while still keeping it as a local (dev) database.
Delete the database your local server(make sure u do a back up off data / your seed method has relevant data needed)
Delete all migration class files besides the configuration file from your Migrations Folder. (migrations need to be enabled in order for this to work "Enable migrations" )
In the Package Manage Console type the following command: “ add-migration update ”. Once that is successful
Type in “Update-Database” in the Package Manager Console.
Your Database will be recreated without ANY data.
I have the same problem for MVC5, so i change the web.comfig connection string for:
<add name="MusicStoreEntities" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|MvcMusicStore.mdf Catalog=MvcMusicStore" providerName="System.Data.SqlClient"/>
it works for me!