Database is removed from Microsoft SQL Server, Why? - asp.net

I created a new Empty website in Visual studio Express 2013.
I created an ASP.NET folder: App_Data.
Then I went to SQL Server Management Studio. I created a new Database in which I provided the path to the App_Data of the website that I created.
I named the database "FacebookData"
Then I went to Visual studio; I found the database in the App_Data folder and also in Server Explorer.
I went to Web.config, And under I added
<connectionStrings>
<add name="DataConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\FacebookData.mdf;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
I click Build -> Rebuild Solution
When I close SQL Server Management Studio, and reopen it again.
The Database named "FacebookData" is missed from the SQL Server Management Studio Databases list!!!.
I still find it in the Visual Studio in App_Data folder and Server Express.
If I close and open the SQL Server Management Studio before adding the to the Web.config. The Database is still exist in the SQL Server Management Studio. which makes me feel that there is something wrong in the connection string.
Any clues?

Your connection string says that database should be "attached" to SQL server when application is run. To have it permanently living in SQl server you should just use SQL server management studio to attach it manually, that way it stays attached. You also need to change you connection string to
"Server=your_sql_server_instance;Database=FacebookData;Integrated Security=True"

Related

MVC Migrate IdentityUser SQL Server 2012 Migration

Is it possible to change the IdentityUser to use a SQL Server 2012 database instead of the localdb that installs by default when creating a new ASP.NET Web Application?
I have an existing SQL Server 2012 database which I would like to use with the MVC 5 project, I can modify the connection string in the web config, however I get the error "The entity type ApplicationUser is not part of the model for the current context".
I've can't find any reference to AspNetUsers to that I can substitute for my User table.
Am I forced to modify the existing localdb and rewrite my previous database to work with the new AspNet table structure and if so can the database then be migrated and upscaled to SQL Server 2012?
Any assistance would be much appreciated :-)
Simply enable migrations for your project (assuming VS2013 - newer versions are different and don't require migrations to be enabled for vNext)
1. Enable-Migrations
2. Update-Database -Script -SourceMigration: $InitialDatabase
That will generate the scripts you need.
Solved this by closing Visual Studio 2013 and locating the path of the localdb which was WebApplication1\App_Data, there were two files aspnet-WebApplication1-20140430012043.mdf and aspnet-WebApplication1-20140430012043_log.ldf residing in that folder.
I modified the security permissions of WebApplication1\App_Data so that SQL Server 2012 would have no issues connecting to it.
I then opened Microsoft SQL Server Management Studio and proceeded to attach the database, once the database was attached to the instance, I backed the database up. Next I dropped the database and carried out a restore, this time renaming the database as per the project and relocating the mdf and ldf files.
I then opened my project in Visual Studio 2013 and modified the Web.config from:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-20140430012043.mdf;Initial Catalog=aspnet-WebApplication1-20140430012043;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
to:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=MYSERVER;Initial Catalog=WebApplication1;Integrated Security=True" providerName="System.Data.SqlClient" />
Ran the project and all is well.

Visual Studio , SQL Server can't connect properly

I am new to visual studio (2010) environment. I want to make an application in asp.net which requires database connection with MS SQL SERVER 2012( SQL Server Management Studio).
In "Add Connection" Wizard , I am specifying the database details and it is successfully connecting , but it is just displaying the "System Databases" , i.e. not displaying the databases I created.
I think there is some permissions problem.Thanks in advance for the help.
Fixed it by replacing the corrupted dll located here: "C:\Program Files (x86)\Common Files\microsoft shared\Visual Database Tools\dsref80.dll"
dsref80.dll
I seem to have solved this problem.
I did the following:
In the Server Explorer, I right clicked the Data Connections, chose "Add Connection..." and created a Microsoft SQL Server. I set the server name to .\SQLEXPRESS and the database name to Library.
I then added the following to app.config: It seems to work now.
<connectionStrings>
<add name="Library"
providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;Database=Library;Trusted_Connection=true;" />
</connectionStrings>

SQL Server mdf file cannot be connected by both ASP.NET application and SQL Server Management Studio

I have a simple ASP.NET website application. Using the new Entity Framework model-first approach, I created a database (.mdf file). However, whenever the application is running, SSMS cannot connect to the database (error 5123 or 5120), and vice versa.
Is this normal? Is there a way to get around this?
I have tried to run SSMS as Administrator, but that did not seem to help.
I am using VS 2012 Professional and SQL Server 2012 Express.
Here is the connection string of the app.
<connectionStrings>
<add name="MarketingContainer"
connectionString="metadata=res://*/App_Code.Marketing.csdl|res://*/App_Code.Marketing.ssdl|res://*/App_Code.Marketing.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL2012ADV\MSSQL\DATA\MarketingDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient"/>
</connectionStrings>
When you use AttachDbFileName you place a lock to the file and use as single user mode and only one connection can use the database.
For use the database between Visual Studio and SQL Server you must:
1. Attach the database (not the file) to SQL Server
2. Use the connection string to point to SQL Server
Max answer is in principle correct. I have now implemented his answer, using this connection string
add name="MarketingContainer" connectionString="metadata=res://*/App_Code.Marketing.csdl|res://*/App_Code.Marketing.ssdl|res://*/App_Code.Marketing.msl;provider=System.Data.SqlClient;provider connection string="data source=SQLSERVERNAME;initial catalog=DATABASENAME;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"
I should note that the reason that my problem arose in the first place was this. I use VS2012 to create an entity data model (edmx file). after I get the model to where I want it, I generate the database from model. at that point, the Generate Database Wizard shows up. New Connection. Change Data Source. I choose Microsoft SQL Server Database File (because I only had a mdf file at that point). If you do it this way, you end up with the application using LocalDB, and Attachdbfilename locks the file as Max said. However, what I should have done is first use SSMS to attach to the mdf file, and then rename the database as needed. and then in VS2012, Change Data Source, select Microsoft SQL Server instead. and now, both VS2012 and SSMS can access the database.

Connection string setting while deployment

I have ASP.NET project and attached mdf database.I want to deploy it to remote server.I have just ftp account.The project works well on my local but ı had problem while deploying.I changed my connection string like. Please help me what is the problem ?
<add name="libraryConnectionString"
connectionString=" Server =.\SQLExpress;
AttachDbFilename= myusername#ftp.myserver.com/App_Data/LIBRARY.mdf;
Integrated Security=SSPI;
User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
You have to create a Ms-Sql server database (remote) along with user via control panel or you may ask to your hosting provider to create a database and user. Once database is created then you may execute your script (You may use Visual Studio Database Publishing wizard) and construct a new connection string for your web-app.
Have a look at MSDN article - Deploying a Database by Using the Database Publishing Wizard
The Database Publishing Wizard in Visual Studio enables you to deploy
a SQL Server database (both schema and data) to a hosting environment.
You can run the wizard by right-clicking a database in Server Explorer
and then clicking Publish to provider.

Database in App_Data folder -> Unable to open the physical file "xyz.mdf"

I am creating a package of an asp.net mvc3 application to distribute it. Means I want to zip it and send it to someone for review. I want that you just start the solution in VS and it runs. So no need for changes in web.config.
I was using sql server with hard-coded DataSource. (COMPUTERNAME\SQLEXPRESS)
I changed it to .\SQLEXPRESS to make it relativ. Better.
Then i detached the db with SSMS, copied the DB to the App_Data folder, added AttachDBFilename=|DataDirectory|MyDatabase.mdf;
And (as found on msdn) Initial Catalog=; "use it but don't set it"->great feature ;)
This is the result:
<connectionStrings>
<add name="DBService" connectionString="Data Source=.\SQLEXPRESS;AttachDBFilename=|DataDirectory|MyDataBaseFile.mdf;Initial Catalog=;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
When I start the asp.net Development Server (hit "play") in VS Web Developer 2010 there is a Database error:
Unable to open the physical file ".........mdf". Operating system error 5: "5(Zugriff verweigert)".
Any suggestions how to attach the file to the solution? What about switching to Compact Edition?
Have you tried adding
User Instance=True
?
Data Source=.\SQLEXPRESS;AttachDbFilename="|DataDirectory|MyDataBaseFile.mdf";Integrated Security=True;User Instance=True

Resources