Is there a way to password protect a SQL LocalDB .mdf file in C# application? - sql-server

I am creating a C# WinForms desktop application along with a SQL Server LocalDB database. I have a setup installer to install the application on desktop. Since it is not on the web server, I have to put the db.mdf file on the customer's computer. But then the user may try to attach this database to SQL Server installed in their own PC and access the data from outside the application. Is there any way I can password protect the file so that it can only be accessed from the application?
I tried to change the extension of file so that it won't be oblivious that its an .mdf file but it does not work without .mdf extension.
Please help me if there is any way I can find a solution to this or any other database I can use for the purpose.

Related

How can I copy Dll's to Program Files folder in Windows 2008 Server using C#

We already have a C# application for Deploying Dll's to different environments(DEV/QA/PROD). This application when asked copies the required Dll's to Program Files folder. This was working fine in Windows 2003 server. But now we migrated our server environments to Windows Server 2008 which brings UAC into picture.
Now when we try to copy the Dll's from local desktop to Server Program files using our application the application is failing.
Can any one suggest me what is the C# code that can make my application to elevate it privileges which allows me to copy the Dll's to remote server.
Regards
Masthan
Add an application assembly manifest that includes requestedExecutionLevel of requireAdministrator.
That way your update program will be run as an administrator.
You could also change your installer to give all users Full Control to your application folder; that way any user could modify your application.

How to set up Database for ASP.NET MVC Application

I created an data driven ASP.NET MVC Application locally and it works fine. Within my App_Data Folder of my project i see two databases: ASPNETDB.mdf and myProjectDatabase.mdf.
I uploaded my project files an the webserver, so that I can actually open the website. But I couldn't figure out how to connect my Database that I created locally to that website. My Website uses Authentication so that it is necessary that a user logs in/registers. But because the database is not connected this obviously doesn't work, because the application needs a database to save and retrieve user information.
I'm using Visual Studio 2010 Express and downloaded Microsoft SQL Server Management Studio as well to work with my database. With Microsoft SQL Server Management Studio I already connected to my database with the login information my provider gave me. But how can I import my local databases? Do I need to import ASPNETDB.mdf as well, because that is where the user information are stored?
I would be very thankful for help
What version of SQL Server do you have on your production server? SQL Server Express can attach to those local MDF files. If you're using SQL Server Standard, you'll need to attach them to the SQL Process.
Also, the ASPNETDB.MDB file is the authentication database that is automatically created in your MVC project. That is the DB that stores usernames and passwords, so yes, you will need to move that database over as well.

sql server mdf file database attachment

I'm having a bear of a time getting visual studio 2010 (ultimate I think) to properly attach to my database. It was moved from it's original spot to #MYAPP#/#MYAPP#.Web/App_Data/#MDF_FILE#.mdf. I have three instances of SQL Server running on this machine. I have tried to replace the old mdf file with my new one and cannot get the connectionstring right for it.
What I'm really trying to do is to just open a DB instance and run a DB create script. Then I can have a DB that was generated via my edmx (generate database from model) in Silverlight business application (c#)
Right now, when I go to Server Explorer in VS, choose add new connection, choose MS SQL Server Database File (SqlClient), choose my file location (app_data directory), use Windows authentication, and hit the Test Connection button, I get the following error:
Unable to open the physical file "". Operating system error 5: "5(Access Denied.)". An attempt to attach to an auto-named database for file"" failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
The mdf file was created on the same machine by
Connecting to (local) in SQL Server management studio.
Getting a new query.
Pasting in the SQL from the generated ddl file.
Adding CREATE DATABASE [NcrCarDatabase]; GO; before the pasted SQL and
Executing the query.
I then
Disconnected from the DB in Management Studio.
Closed Management Studio
Navigated to the DATA directory for that instance
Copied the mdf and ldf files to my application's app_data folder.
I am now trying to connect to the same file inside visual studio.
I hope that gives more clarity to my problems :).
Connection string is:
Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\SourceCode\NcrCarDatabase\NcrCarDatabase.Web\App_Data\NcrCarDatabase.mdf;Integrated
Security=True;Connect Timeout=30;User
Instance=True
I was getting "access denied" until I ran Visual Studio as an administrator. I then had to detach the database from MSSMS before I could get it into Visual Studio.
You mentioned that your MDF file was moved. Was the MDF properly attached to SQL Express after the move? See: How to: Attach a Database File to SQL Server Express.
Access denied might well mean that you already have an active instance that has opened the file prior to you doing it - and if you have three active instances, it is likely one of them that is the culprit.
You can try to connect to the different instances with Management Studio and see if you can find the base there. If yes, the detach it from the instance and re-try from Visual Studio. Or, if possible, try to shut down all three instances and then re-try from Visual Studio. If that works, restart the instances one by one to determine which of them is trying to get the file.
I've previously answered a similar question that might help.
Your Sql server service runs under a given account. You should determine the account through the service properties then change the db file security access to full access for that account.
Give Security to Full control for your application Folder and .mdf, .ldf files. Write click on the folder which contains .mdf,.ldf files
Go to property
click security in that
Click Full Control in allow.
and them proceed your process.

What is a way to access .accdb files for reading and writing?

I have an Microsoft Access Database (.accdb) that stores information needed in an application.
Is there a way to distribute my application with the access database without having the user have access installed or get the user to go and download and install the office runtime?
The windows form application that uses the access database is developed in VB.net in Visual Studio 2008.
An installation of Access is not needed. Your installation should include all the necessary files to interact with the accdb file.
Are you using something other than .NET Framework Data Provider for OLE DB?

How to deploy a windows form application including database to another PC?

All
I have built a windows form application using VS 2005 Standard edition which uses SQL Express as the database.
I'd now like to deploy my application to another PC that has .NET framework and SQL Server already installed.
The bit I am unclear about is what do I need to do to ensure that my database is also deployed with my application?
I have spent most of the day reading various articles from msdn to other blogs and I can't get a clear understanding on what is the best way to deploy my application. Some of the varying advice includes:
Add a 'setup' project and create a setup.exe file
Use a custom action to create a database during installation (uses installer class)
Manually detach .mdf and .ldf from database and use command line on target PC to reattach to database
Amend .config file to get new connection string (from target machine) and then reference that in my ado.net code
etc. etc.
Can anybody cut through the confusion for me and help me out?
Thanks
Here’s the simplest way I can think of:
Build the application
Copy the
executable (in the Bin directory) and
the app.config file to the other
machine. The executable and
app.config need to be in the same
folder
Backup the database on your
machine and restore it on the other
machine
Change app.config to point to
the restored database

Resources