Connect MVC 3 application to sql server 2008 - asp.net-mvc-3

I am newbie and confused how to do it.
I have built MVC 3 application in Visual Studio 2010 and published to the localhost and it's working fine.
Here, What I actually want is to use the Sql Server 2008 installed in my computer rather than database under the folder APP_DATA that I have created using visual studio.
I am new to the MVC and don't know about the database connection detailly. Following is the connection string of the web.config file:
<connectionStrings>
<add name="IVRControlPanelEntities" connectionString="metadata=res://*/Models.IVRControlPanelModel.csdl|res://*/Models.IVRControlPanelModel.ssdl|res://*/Models.IVRControlPanelModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;attachdbfilename=|DataDirectory|\IVRControlPanelDB.mdf;integrated security=True;user instance=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
This is the connectionstring automatically added by the visual studio while adding the data entity model.
After published to localhost the database used by mvc 3 is under the APP_DATA folder of wwwroot folder.
What I need to change to connect to the Sql Server 2008 installed in my computer rather than database created on the Visual Studio.
One more thing, I could not import the database created by visual studio to the sql server 2008 which is in the form IVRControlPanelDB.mdf

You should use Database.SetInitializer<> method in Application_Start method in Global.asax
read this and this ,this article may help too.

You need to modify your connectionstring to add the address to your sql server. Example:
<connectionStrings>
<add name="ApplicationServices"
connectionString="Server=localhost;Database=myDatabase;User Id=myUser;Password=myPassword;"
providerName="System.Data.SqlClient" />
</connectionStrings>

Related

SQL Server database connection

I have an ASP.net web application and a database created on another computer. Now I want to run this on my computer.
I have installed SQL Server Express edition and SQL Server Management Studio on my computer and I copied database file to my SQL directory and attached it successfully through SQL Server Management Studio. Applications current connection string looks like this.
<add name="ASPNETDB"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
What do I have to change in this to make it work? FYI I have already copied database to SQL Server Management Studio default installation directory and attached it through SQL Server Management Studio express. Also I connect to SQL Server Management Studio using this server name.
localhost\SQLExpress
If you've attached the database to your SQL Server Express instance, then you should be able to use this connection string from now on:
<add name="ASPNETDB"
connectionString="Server=.\SQLEXPRESS;Database=ASPNETDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
With this, you basically tell your application
what server (instance) to connect to (.\SQLEXPRESS)
what database to use on the server (ASPNETDB - or whatever name you gave it)
to use integrated security (e.g. use your Windows credentials) to log on to SQL Server
That's all you need - SQL Server will handle all the details of dealing with data and transaction log files and all those nitty gritty jobs for you.

Web Site Administration Tool:Unable to connect to SQL Server database

I am developing an ASP.NET MVC3 application using,
MS SQL Server Pro 2008
MS VS 2010 pro
Entity FrameWork as model class.
When i start ASP.NET Web Site Administration Tool by clicking ASP.NET Configuration from solution explorer, in Security tab it shows an error:
Unable to connect to SQL Server database.
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config has a connection string as,
<connectionStrings>
<add name="LocalSqlServer" connectionString="data source=.\MSSQLSERVER;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>
I opened the
C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regsql.exe
and configure the database default named 'aspnetdb'. But situation is not being improved. So, would you please help me to solve it.
Thank you.
Have you tried the database server that Visual Studio attempts to use?
It should match the one specified in your web.config file.
To check to go: Tools -> Options -> Database Tools -> Design-time Validation Database.
You can then change the SQL Server instance which is used.

Get MVC3 to use Oracle for user roles and memberships

I'm having problems getting my MVC3 front end to use an Oracle 11g database to hold the user memberships.
I've been following this simple tutorial but can't get it to work.
Some things I've done so far:
Downloaded and installed the Oracle Data Provider for .NET (ODAC 11.2
Release 4 (11.2.0.3.0) )
Run the InstallAllOracleASPNETProviders.sql against the database on
the development server.
Tested the connection via SQL developer (using the TNSNames file)
with no issues.
Unable to create a connection to the database under the Server
Explorer pane is Visual Studio 2010 which seems related.
Having checked the database I can see that the "ORA_ASPNET_" tables have been created OK.
I'd appreciate a steer on where I'm going wrong as this SHOULD be easy.
The problem appeared to be related to the connection string.
I had been using the TNS names file for the connection details but this wasn't being picked up.
I modified the connection string to include the server, port and service name as follows:
<connectionStrings>
<add name="OraAspNetConnectionString"
connectionString="Data Source=svr56:1521/TCO1103;Persist Security Info=True;User ID=Ora_Asp;Password=*****;"
providerName="Oracle.DataAccess.Client="/>
</connectionStrings>
The problem creating the connection is the Server Explorer also resolved itself once I used the same details in the Data source name box.
svr56:1521/TCO1103;Persist Security Info=True;User
ID=Ora_Asp;Password=*****;

asp.net mvc 3 membership, cannot connect to database

I have installed visual studio 2010 as well as mvc 3 and have been working on a site.
Later I installed sql server 2008 and when I tried to register a user through provided membership in asp.net mvc i had bunch of errors saying that there was no data base or something like that, so through a lot of research i finally added the aspnetdb to my sql server and enabled remote accessing... and as of right now when i try to register i get this error: Unable to connect to SQL Server database.
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)
this is my connection string:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
</connectionStrings>
I remember that some time ago working on my previous projects i had no problem with the membership provider and registering users loging in and so on.. so i'm really frustrated and don't know what i'm missing here.
You can use it directly from localhost, no need to put it in App_Data folder. Use this connection string:
<connectionStrings>
<add name="ApplicationServices" connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=*Name of your database*;Integrated Security=SSPI"/>
</connectionStrings>

ASP.NET MVC 3 - Automatically created MDF file

I created an ASP.NET MVC 3 project using the default template, when I ran it and created an account, it automatically created an MDF file in my App_Data directory.
The problem is my development machine had a community preview of SQL Server 'Denali' installed, and the application needs to run on SQL Server 2008 R2 - so I can't attach the MDF file to the production instance.
I've changed the MDF file to be a SQL Server 2008 R2 database, attached it to a local instance, and added this to my web.config file (no connection strings were present before this):
<add name="ApplicationServices" connectionString="Data Source=127.0.0.1;Initial Catalog=MyDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
But my application still seems to look for the older instance, when I try to login now, 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)
My question is - when Visual Studio 2010 automatically creates an MDF file for you, is there a connection string specified somewhere other than the web.config file?
I've searched through the code and can't seem to find one. I've verified I can connect to the SQL Server 2008 R2 database from other applications.
Any suggestions are welcome.
This was an issue in my Web.config file. It's fixed now.

Resources