I'm developing a Windows Form client application in C# .NET that allows the user to backup the database. Every time he makes a backup the following logic is executed in a sp in SQL Server:
DECLARE #File VARCHAR (1000) = 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\Backup\DBCompany-'
+ (SELECT REPLACE(REPLACE(CONVERT(VARCHAR(500), GETDATE(), 120),':','_'), ' ','_')+ '.bak');
BACKUP DATABASE [DBCompany]
TO DISK = #File
NAME = 'DBCompany';
GO
As you can see the .bak file gets a name according to the date and time it was saved, so I have differents files in my directory like these:
DBCompany-2015-06-04_20_21_08.bak
DBCompany-2014-02-24_19_01_39.bak
DBCompany-2014-01-22_23_30_58.bak
....
I know the syntax to restore a .bak file but I'm trying to make it possible for the user to select one of these files and restore it, but I can't see any way to retrieve these files and show them to him other than using a OpenFileDialog control (which I don't want to).
As far as I researched this can be done integrating PowerSheel into my C# code. Can someone point me in the right direction on how this can be done?
You can easily do this from C# without Powershell. Assuming the app is running on the same box as the SQL Express instance, below is an example to load a combo box:
var backupFileList = System.IO.Directory.GetFiles(#"C:\Program Files\Microsoft SQL Server\MSSQL12.SQLEXPRESS\MSSQL\Backup\DBCompany", "*.bak");
foreach(var backupFile in backupFileList)
{
this.comboBoxBackupFiles.Items.Add(backupFile);
};
If you know the physical location of backup files, you can go with System.IO; namespace. From this
Get the list of files available in the physical location.
List all the file names and show the file names to user.
once the selection is made by the user restore the selected backup file.
Sorry, It could be very lengthy code to do, so I have given description to achieve your requirement.
Thanks,
Related
I have a simple WinForms Application written in C# using Visual Studio which includes a SQLite Database..
The Database’s Build Action Property is set to “Content” and the Copy To Output Directory is set to “Copy if Newer”.
The application is ready for publishing. I’d like to make an installer. What is the best way to go about this?
I anticipate a few problems.. Where should I save the database file? If it gets saved into the Program Files Folder, the Database would become Read-Only.. Where should I save it and how should I go about doing so?
I will take your question as: "Where to store writable files during installation and use of application?"
There is 2 special places for this:
ApplicationData special folder. This folder is accessable by current user and is actually placed in C:\Users\\AppData\Roaming (Win7). This path is stored in %APPDATA% environment variable and can be used like this:
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
You may safely store your files in following folder structure there: %APPDATA%\YourCompanyName\YourAppName\.
CommonApplicationData special folder. Almost the same as above, except it is accessable by all users and is actually placed in __ (Win7). This path is stored in %ALLUSERSPROFILE% environment variable and can be used like this:
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
As before, you may safely store your files in following folder structure there: %ALLUSERSPROFILE%\YourCompanyName\YourAppName\.
As for your case, here how you can get a db filepath:
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
var dbPath = Path.Combine(appDataPath, "YourCompanyName\YourAppName", "your_db_name.sqlite3");
Real difference is: does this database are common for all windows users, or each user should have his own database.
I am developing a software using a MSSQL database for holding the data. In the program I implemented a function for creating a backup with SMO.
Now I am trying to implement a restore function. It works without any problems when the user has to insert the path manually. But I want to implement a select file dialog like this one the SQL Server Management Studio (SSMS) is opening when selecting a custom medium (see on the screenshot).
I already found the Microsoft.SqlServer.Management.Smo.Server class with the method EnumDirectories, but it returns only directory names and no files. When I confirm the selection I need the path in format C:\Directory\FileName.bak.
Is it possible to meet my needs with using SMO?
Many months ago I found a solution. I just developed a custom SelectFileDialog which is able to connecto to SQL server and work with the following SQL functions:
For searching available drives:
exec xp_fixeddrives
For reading folders and files:
exec xp_dirtree 'PATH', 1, 1
The PATH variable is dynamicly replaced depends on which folder is expanded by the user. It works without problems.
I'm writing a C# WinForms application, and one of the components of the application is a SQLite database.
If the user is running the program for the first time, the program is supposed to create the necessary folders and files (namely, the database file) in the user's home directory. That works fine.
However, the database also needs to be set up (i.e., tables need to be added). I have a SQL script that will create the necessary tables; however, it is currently stored in the solution directory and I'm not sure if this is the best practice for when the program actually gets packaged into an .exe file.
The script will be the same every time the database needs to be set up, so I'm thinking there are probably a few options:
Have the program read from the SQL script and apply it to the database (preferred unless there is a better way)
Load the contents of the script file into memory (hard-code it into a string) and have the application run it that way (not preferred because of future versions, there needs to be a way to update the existing structure so as to not obliterate the existing database, so this way could get complicated)
Include the SQL script as part of the program package or a standalone file (very dangerous because users aren't supposed to know about that)
So what is the best way to run SQL statements from a "companion" script file? How does all of this get packaged when the program is ready for production, and how can I ensure that this file will be accessible by the program every time it is needed?
You can set the file to be copied in output directory. Select the file in solution explorer and then in property window, set Copy to Output Directory to Copy Always. This way the file will be copied in output directory and you can load it this way:
var path = System.IO.Path.Combine(Application.StartupPath, #"Script.txt");
var content = System.IO.File.ReadAllText(path);
If the file in root of the solution, use filename as above. If the file is in a folder in your solution, for example for a file in Folder1, use #"Folder1\Script.txt" in above code.
As another option you can add the file to Resources.resx. Then it will be included in resources and you can simply access it this way:
var content = Properties.Resources.Script;
Include an encrypted version of the sql file. Have your program load, decrypt it then execute it. At least that's how I'd do it.
Maybe someone can help me with the following problem:
I created a winform application in visual studio which i want to publish. The application contains a map Sqlscripts with .sql files in it.
FileInfo file = new FileInfo("../Sqlscript/View.sql");
string SQLscript = file.OpenText().ReadToEnd();
i thought about creating a directory for those files in C:\ when running the application, but ten i realized that it's not safe doing it this way. No one should have acces to sql querys. Can anyone help me for an alternate?
Thanks!
Embed your .sql file as resource (i.e, file content will be embedded in compiled code), and read it at runtime with Properties.Resources.yourresourcename
string sql = Properties.Resources.yourresourcename;
To add a file as resource, open Resources.resx in solution explorer -> Add Resource -> Add existing file (or add new text file)
Obscurity != security. If someone wants to, they can always get the contents of the file. Be it reverse engineering, or simply reading out the memoty of the program. How important is it that thay can't edit / view the SQL? Otherwise I would generate the SQL on a server somewhere. Make sure the server only accepts parameters and generates / runs the SQL there.
I never see database files in the installation folders of random programs, yet they obviously have one. My question is how do they do it?
EDIT: My database can be either on SQL Server, MySql, or Access I'm not bothered, however I would like the client to not have to download SQL Server or any other programs in order to be able to use mine.
You never see database files in the installation folders because installation folders are meant for programs, not for data. The data go into the appdata folders, such as "C:\Documents and Settings\User1\Application Data\Company1\Application1" or "C:\Users\User1\AppData\Company1\Application1" depending on your OS.
I never see database files in the installation folders of random programs yet they obliviously have one
If they are oblivious to database files then they don't need them. That is why you don't see them.
You can place the database file in Hidden Mode so that user can't see it until and unless he has Show Hidden Files option true
Or instead of placing database file along with exe you can place in dedicated application directory like C:\users\username\appdata\yourapp\
My suggestion would be to store your own database as a flat XML file (for example, a plain .NET DataSet saved to file via DataSet.WriteXml ) then apply your own fixed encryption to that file. the key to encrypt/decrypt will be inside your program code and need never be altered. By storing your own data as XML you wont need a client. At the start of your program, Read and decrypt your datafile into memory, then save and encrypt out when needed.