Use SMO to locate correct backup path - c#

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.

Related

How can I retrieve every .bak file with PowerShell and C#?

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,

deployable database on CD

I have a Microsoft SQL database that is currently connected to a winforms C# application, it works fine on the single computer, but i would like it to be usable on a CD for any user.
I tried putting it in as a localDb but for some reason the database is duplicated and put into the bin folder, it causes multiple issues in recording data, for instances i save user ID 5 it saves in bin but never makes changes to the real database. Then next i go to create it, the user ID changes to 7 with user 6 not visible in either two databases (yes it is auto incremented by 1)
Any suggestions or best methods on making a database useable and readable via CD if the winform application is also on the CD
I have not tried this my self, but according to the documentation SQLite supports read-only databases.
If the file is read-only (due to permission bits or because it is located on read-only media like a CD-ROM) then SQLite opens the database for reading only. The entire SQL database is stored in a single file on the disk. But additional temporary files may be created during the execution of an SQL command in order to store the database rollback journal or temporary and intermediate results of a query.
see https://www.sqlite.org/c_interface.html
.NET SQLite providers are available here:
https://github.com/praeclarum/sqlite-net
http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki

C# Sql Server Express not Saved Permanently

I am using Visual Studio C# 2008 and SQL Server Express. i got a question
i have the following data on my database
and then, i want to insert a new data
after that, i have to make sure that the entered data is saved on my database
it was there, the data was entered succesfully ! but then, when i take a look at my database table
the entered data was not saved permanently. i need to make the entered data saved permanently. how do i resolve this ?
thanks !
You are using the User Instances = true + AttachDbFileName "feature." When you use this "feature" each application you use will open a different copy of the original MDF file. So your C# app opens one copy, you insert a row, but this is never seen in the copy that is open in SSMS / Visual Studio or wherever else you might review the data.
To fix this, STOP USING THIS "FEATURE".
Create/attach your database to a proper instance of SQL Server, and point to it from your app and SSMS / Visual Studio by referencing the server and the logical database name, not the path to an MDF file.
You'll notice I called this a "feature" - in quotes - multiple times. This is because it is not a feature and has caused countless, countless users before you to become absolutely confused about why their inserts and updates "don't work"...

How do I connect my application to eXist-DB

i've created a c# application that is effectively a workflow system that moves XML documents around folder structures depending on their status, among other things.
What i'd now like to do is use a database to store the XML files instead of local folders, but I still need to be able to query them with Linq / XQuery. I've chosen eXist-DB as my database it is gets some fairly good praise.
My question is, how do I connect to my newly created DB from my C# application. If i choose the 'Connect to a DB' option in Visual Studio i'm presnted with a bunch of SQL server options, but nothing that appears to be of any use to connect to my nice new eXist DB.

Failed to update database "*.mdf" because read only EntityFramework

I have a C# .NET Framework 4.0 desktop application with Entity Framework as DAL.
When a try to save a data into DBContext on anybodies machine but mine, I recieved an exception
Failed to update database "*.mdf" read only
I keep my DB near .exe file, in folder "DAL/AppData".
How can I allow write access on other machines?
Can I do it programmatically?
I've read that I can place DB into AppRoaming Folder, but this is not my variant.
Thanks in advance.
I keep my DB near .exe file, in folder "DAL/AppData".
Yes. Can it be you mean that this is in the programs file folder, you know.
THe one that windows specs of the last 10 years say is "read only" for normal users?
OUCH.
THer are folders for storing data. There is a SpecialFolders enumeration to get the valid path of every such folder.
How can I allow write access on other machines? Can I do it programmatically?
On a SQL Server, this is done by allowing the other computer to access the server, not the data files. I.e. you connect to the SQL Server on the other machine, which is having the database loaded.
I've read that I can place DB into AppRoaming Folder, but this is not my variant.
First, that would be stupid unless you do actually plan for roaming - SQL shold go into a local folder, never roaming.
Second, "not my variant" is like "Hey, I drive the car against the rules, what can I do not to get speeding tickets". And "following the law is not my variant". Your variant is something WIndows does not care for. Learn how to install your software according to the windows guideline which is VERY clear where changing data should NOT be.

Categories

Resources