Backup database with Entity Framework on ASP.Net - c#

I have an interesting case. I lost access to an Azure subscription. However, I still have access to publishing an ASP.Net application, because I configured a publishing profile, which still works. Note, that this is nothing illegal - internal company policies regarding VS subscriptions; enough to say, that application is mine, written by me, maintained by me etc.
I need to create a backup of the database, but the only thing I can do is to publish the application. Application obviously have connection string embedded in settings, but I cannot connect to the database due to firewall settings (Azure services only). So I need to find a way to dump the data with a controller and action.
So far I managed to dump most tables manually (eg. var data = context.Table.ToList();) and then output results into view. However, this won't export internal ASP.Net tables, which I cannot access directly (eg. user/role claims etc.)
Keep in mind, that we're talking about Azure SQL, which differs from the regular SQL Server.
How can I back up the database, so that I can migrate application to a different subscription?

If you also can publish your webapp. You can create a API function and invoke it. Something like: https://yourwebsite.azurewebsites.net/backup/start.
You can create PROCEDURE, and run it to generate .bak file to blob(
The premise is that you need to know blob related information.).
Sample code like :
C# SQL Server backup of a remote database to the remote default backup location without direct access to the remote location?

Related

Is OPENROWSET a "production-safe" method?

I'm working on a desktop app that was developed about 5 years ago using C# and mssql server 2000.
This app uses OPENROWSET to load some xls files, and it worked just fine til the institution in where I work upgraded databases (to something like msserver 2019 or sort of), and OPENTOWSET stop working.
Despite knowing there's plentiful of procedures and recipes to fix this issue (which I cannot apply coz database runs on server), my company's DBA is telling me the only way to fix such problem is to give admin-privileges to the app-user, and he cannot do that because of safety rules - dropping the problem to me. Now I have to replace OPENROWSET with something else.
So the question is: is that true?
There's no way for a skilled DBA to make OPENROWSET work on a remote DB with regular/non-admin user, fixing those errors about permissions and linked servers?
Is OPENROWSET "mature-or-safe-enough" to make it run on nowadays production servers?
It looks like you are attempting to open excel files from within your database. In which case, the default setup would not allow this:
https://learn.microsoft.com/en-us/sql/t-sql/functions/openrowset-transact-sql?view=sql-server-ver15#remarks
OPENROWSET can be used to access remote data from OLE DB data sources
only when the DisallowAdhocAccess registry option is explicitly set to
0 for the specified provider, and the Ad Hoc Distributed Queries
advanced configuration option is enabled. When these options are not
set, the default behavior does not allow for ad hoc access.
When accessing remote OLE DB data sources, the login identity of
trusted connections is not automatically delegated from the server on
which the client is connected to the server that is being queried.
Authentication delegation must be configured.
It sounds like this may possibly have been configured way back in the past for previous databases, but the DBA who now sees this regards it as a bit of a security problem. Whether or not he is right depends very much on how you use this, but it does sound like something that would be better avoided.
It may be better to load the excel files in your C# application and then send the BULK data to sql server to just save.

Can't save data in SQL Server database, where to place the file?

In my application I need to use a local database (the application I'm creating works with everything locally). I had one database that worked really bad, because sometimes it saved the data and other times don't. And if I published the program I couldn't find the database file.
But I am having some trouble to know where to place the database. I have created one in E:\PAP\Trabalhos\Trabalhos\database.mdf and other in E:\PAP\Trabalhos\Trabalhos\bin\Debug\database.mdf, but in any of those paths the database is recreated/goes back to the previous state, when I try to start the program.
In my connection string I have this:
Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|database.mdf; Integrated Security=True
and that points to the file ...\bin\debug\database.mdf
I want to be able to access the database in any computer I use the program and be able to actually save data.
Where is the recommended path to place the database file and be able to access it independent of the computer I am using?
Should I use Windows authentication or SQL Server authentication?
tl/dr: Database doesn't save data and I want to be able to access it in any computer without any extra steps.
You can't use "(LocalDB)\" and access it from any computer. LocalDB is by design accessible only from the applications running on the same computer (it is an embedded database).
To access database over the network you need to install instance of SQL Server, like full SQL Server Express instance or use some cloud service like AWS or Azure.

detach an SQL server from visual studio

I'm pretty new to developing in Visual studio and working on databases.
I am working on a program that deals with reading and writing data to a database that I created with Visual studio.
I need to work on this project from another computer and copying over the project files was a breeze but I'm facing issues when it came to copying the .mdf database file.
Upon research it seems like, at least in the Microsoft SQL Server program, that I would have to "Detach" the database before copying it over to a different computer. So I am assuming I would have to do something similar with my Visual Studio Database as well.
Anyone has any inputs with regards to this?
If there is not much that I can do, I guess I could recreate all my tables and everything in Microsoft SQL Server program, so that it would be easier to move the database if needed.
I was in a similar situation such as yourself when I began developing my first core application. You have a few different options including:
Detach an already created database from the hosted SQL Server service and "re-attach" to another SQL Server service that is accessible from the desired set of hosts. You have to essentially disconnect the database from the service before you are able to transfer or migrate it since the process will have an exclusive lock on the .mdf file. https://msdn.microsoft.com/en-us/library/ms190794.aspx
Create the necessary .sql scripts to construct the database and run in the appropriate order e.g. create database, create tables, etc. to re-construct the database at the service location.The neat thing about this technique is if you have already created the database (which it sounds like you have) SQL Server allows you to generate scripts rather than having to write them yourself. https://technet.microsoft.com/en-us/library/ms178078(v=sql.105).aspx
Finally you may use a subscription based service such as SQL Server through Azure to host the service for controlled global access aka DBaaS (Database as a Service). I can't post anymore links, but look at Microsoft's Azure SQL Server hosting service if you are curious about this option.
The unfortunate part you have to decide is how much time you would like to invest in this. I began developing the application from scratch which led me to developing scripts to conjure up the database for deployment purposes. Good luck!

Visual Studio 2012 move local .sdf file to server computer

here is the problem I am facing now. I have created an application that uses local database (this was created by Add -> New Item -> Local Database. Afterwards I have added tables under this .sdf database.
Then I have connected to this database using Add -> New Item -> ADO.NET Entity Data Model.
Everything works like a charm, unless I was asked to move this database to a place, where multiple people could access this database and work with it.
Therefore, as I have no previous experience with databases, I have treated this .sdf file as any other file (let's say Excel workbook) and I thought that I could simply take already existing database, copy it on server computer (e.g. \Server001\Database\Database1.sdf) and simply change connection string under app.config.
However the problem is that this does not work. As I didn't know how to change connection string, I created new application, where I have tried to connect to this database located on a server computer; however I received the following error:
SQL Server Compact does not support opening database files on a network share.
I already have fully functioning program, but I have no idea how to make it work with multiple users. I have tied to google for solution, but all I could find is how to create local database, not how to make it accessible by placing it in server computer.
Could you guys please help me? If you need more details, please let me know!
P.S. This is WPF application, .NET 4.5, created using Visual Studio 2012 Professional.
Thank you!
The error message pretty much sums up the problem: SQL Server Compact does not support opening database files on a network share.
SQL Server Compact (aka "local database") is to be consumed by a local application; even if it was a web app serving many requests, the application itself is local.
If you want to have multiple remote connections (i.e. centralized DB, distributed app), you should look at using an instance of SQL Server (any SKU would probably work, even SQL Server Express). Those will use MDF files instead of SDF files, so you might want to refer to Convert .sdf database to .mdf database. You'll probably also need to set up a user identity for your connection string, so check out this link on CREATE USER and Difference between a User and a Login in SQL Server to understand how that can be configured.

Using SQL Server databases on shared hosting

I'm attempting to deploy an ASP.NET MVC 4.0 application, and I am using a shared web hosting solution. I am able to successfully deploy everything, except for my preexisting databases, one for user accounts, and one for posts.
These two databases are in the App_Data directory of my application.
Every part of the application requiring database communication returns the following:
Invalid value for key 'attachdbfilename'
I should note that this problem does NOT occur when testing locally.
My web hosting provider has created a SQL Server database, providing me with a server IP, a username/database name, and a password. However I'm unsure how I can access it, and then replicate the current database in my application in the database on my web host.
Basically my question is, how do I get my SQL Server databases to work on my web hosting?
In this scenario you should not attach your own DB files. Create a new database (e.g. using SSMS or host's Control Panel) on the server provided by the host. Import your local data into that DB and use new server/db/uid/password in your connection string.
If your host already provided you the DB - just use existing DB (don't create a new one) but still import your local data into that DB.

Categories

Resources