When setting up a new ASP.NET MVC Web Application, the default connection string inside Web.Config is something like this:
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
I'm just wanting to play around with logging in and registering, etc but when I run the app it obviously can't find a SQL database. What database with what tables do I need to setup to do this?
I have SQL Server 2005 Standard installed on my system, is that enough?
Thanks.
SQL Server 2005 is more than enough. The default connection string is looking for a SQL Server Express file in the App_Data folder. Replace it with the following:
Data Source=<server name>;Initial Catalog=<database name>;User Id=<user name>;Password=<password>;
If you want to generate the ASP.NET membership tables then run aspnet_regsql.exe, which is found in the .NET 2.0 framework folder (e.g. C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727). A wizard will guide you through the process.
Related
I'm a beginner in MVC application development. I'm just trying to make a MVC application getting help from internet. That was a simple example and working properly. The article is said the database is in app_data folder. But actually there is no any database in that folder. Then I just tried to find the physical database location using the connection string.
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnet-ePhoneBook-20140322204146;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>
as the above connection string (I just not write down this and it is auto generated code in web.config file), the database name is "aspnet-ePhoneBook-20140322204146". Then I checked weather this DB available in physical location of the sql databases. My all other databases (My old projects' databases) are in that folder. But couldn’t find this database. then I tried with folder searching option in windows 7. There is no any database with that name. The wonder is my application run properly. Data saved properly. SQL management studio also not showing the database.
My question is, How to find the physical location of the database and why is this database not showing in SQL management studio.
The issue is you're pointing to SQL server, and ,definitely, not to separate attached database file.
your connection string should look like this:
<connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-ePhoneBook-20140322204146;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-ePhoneBook-20140322204146.mdf" />
where |DataDirectory| - is actually App_Data folder in terms of ASP.NET.
I create a new ASP.NET MVC project with internet template.
I build the solution.
I open ASP.NET Web Site Administration Tool (WSAT).
I click the security tab
I get this error:
"Unable to connect to SQL Server database."
I am using Visual Studio 2012 and SQLServer Express 2012.
I don't have IIS installed (other then what ships with Visual Studio).
The Connection String from the new project:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-MvcApplication2-20130804051506;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-MvcApplication2-20130804051506.mdf" providerName="System.Data.SqlClient" />
It is worth mentioning that I did not change anything in the web.config after its initial creation and that MVC project seems to be working properly e.g. I am able to register and login and then see that data in the (LocalDb)\v11.0.
I read on other posts that the connection string from machine.config is sometimes used instead of the one from web.config.
I have a two machine.config files, one under C:\Windows\Microsoft.NET\Framework\config\v4.0.30319 and the other under C:\Windows\Microsoft.NET\Framework\CONFIG\v2.0.50727
When is a machine.config's connection string is used instead of my web.config's connection string?
I want to use the (LocalDb)\v11.0 data source, why does WSAT unable to connect to it?
From http://forums.asp.net/t/1483981.aspx/1 :
by default, the membership / role provider uses the "localSQLServer" connection string which is set to point to a local SQL Express database from the root web.config. In most cases, the server do not have SQL express installed and you will get this error.
By clearing the connection strings should reveal those errors.
I fired up a new Internet application type MVC 3 project. It has an Account Controller that works using the default ASP.NET membership provider.
The web.config of the project has the following connection string:
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
I do have SQLEXPRESS on my machine, but when I look up the databases in the Management Studio, I do not see any database created for membership. I only see the ones that I created.
Where does ASP.NET MVC create this membership database?
You are using a User Instance (contained the fileaspnetdb.mdf in your SQL installations' data directory) not an embedded database within SQLExpress, which is why you don't see it in SSMS.
Here's an explanation of what this is and how to interrogate the data within it, should you need to.
I have an existing database in SQL Server. I am trying to create an asp.net mvc project around this db.
For this purpose I need to add the db to app_data folder of asp.net MVC project
How do I achieve this?
Note:
The SQL Server is in another system and I do not have rights to install SQL Server Express on my machine as well :(
You don't need to add app_data directory or install SQL Express on your machine. All you need is a connection string allowing you to connect to the remote database.
Data Source=NameOfDbServer;Database=DatabaseName;User ID=User;Password=PWD;Trusted_Connection=False
Once you have the connection string you can start querying the database. The way you do this will depend primary on the technology you would like to use: NHibernate, LINQ to SQL, LINQ To Entities, plain old ADO.NET, ...
I've encountered this problem (while trying to add SQL Server Database (.mdf) file to my asp.net mvc project):
Connections to SQL Server files (.mdf) require SQL Server Express 2005 to function properly. Please verify the installation of the component or download from the URL: http://go.microsoft.com/fwlink/?LinkId=49251*
I have SQL Server 2008. Does anyone know how to add that file to a project?
If you are using SQL Developer, Standard, or any other version aside from Express you need to attach the database to the SQL server before you can use it. In SQL management studio connect to your server, right click on the Databases folder and choose attach, browse to your database and select it (note that you may need to move it to a directory SQL can see - by default SQL runs as Network Service and cannot peer inside C:\Users).
Once you've done that you need to tell ASP.NET that is the database you wish to use. There are a few ways to do this but by fair the easiest is to override the SQL Express database connection by adding the following to your web.config
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Don't forget to give Network Service, or whatever identity you run your application pool as, access to the database in SQL server.
(I believe this will work.)
Move the MDF file to the SQL Server box and use Management studio to attach it, thus creating a database in SQL Server. (You will need to create a transaction log file to go with it.)
Then connect to that database as data source.
Full SQL Server cannot use a connection to a database file specified in the connection string.