I'm designing a Windows application using C# in Visual Studio. I need to create back up button or something that would back up my data. How do I do that?
Simlpy create a back up for you data base. I should not be a big problem to triger that proces from your code.
check this sites:
How to Create Full Database Backup on MS SQL Server for a Database using T-SQL Backup Database command and SqlCmd Utility
How to: Create a Full Database Backup
Create a new store procedure similar to the content below and call it from your code.
Copy Code USE AdventureWorks2008R2;
GO
BACKUP DATABASE AdventureWorks2008R2
TO DISK = 'Z:\SQLServerBackups\AdventureWorks2008R2.Bak'
WITH FORMAT,
MEDIANAME = 'Z_SQLServerBackups',
NAME = 'Full Backup of AdventureWorks2008R2';
GO
Related
I have a problem I need to solve, but not know how exactly. I have a WinForms application (C#) which connects to an online MySQL server - no problem there. In this application I have an option to make database backups (basically I dump this database to a local file on a computer).
I would like to locally "open" this backup on client's computer (to check some old data) - I don't want to make database restore on my server, because database must still be in use for other users. I want to make clean install of MySQL on a local computer and connect to it trough localhost (like I do for testing ), but I do not have physical access to that computer. I can send MySQL installer to my client, but how to go about automatically creating user with password and database from my dump file?
I know how to create a new database if it doesn't exist, but how to do it if it's clean install of MySQL server - no user and password yet.
string connStr = "server=localhost;user=root;port=3306;password=????;";
using (var conn = new MySqlConnection(connStr))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "CREATE DATABASE IF NOT EXISTS `hello`;";
cmd.ExecuteNonQuery();
}
Any help and direction is appreciated.
Regards!
I don't know whether I understand your problem. However,if you install a new Mysql,you can use root user through no database.
Anyway, one thing you need to know,Connect database must has user and password for mysql or sqlserver.
You may need to be more concise description of your problem.
I have developed a winform application using C# and SQL Server 2008. Now my job is to implement it on client's machine.
I am looking for the best way to create the database tables and stored procedure on client machine to run this application. I have generated the script of all my database objects. And now i want to create all database objects on client's machine with one click C# code that read each table or stored procedure script file (i.e. .sql or .txt) and create them.
No need for smo, but a bit ugly
SqlCommand getDataPath = new SqlCommand("select physical_name from sys.database_files;", baseConnection); // get default path where the sqlserver saves files
string temp = getDataPath.ExecuteScalar().ToString();
temp = temp.Replace(temp.Split('\\').Last(), string.Empty);
StringBuilder sqlScript = new StringBuilder(Scripts.CreateDatabase); //CreateDatabase could be in ressources
///The #### are used to replace the hardcorededpath in your script
sqlScript.Replace("####MAINDATAFILENAME####", string.Concat(temp, "test.mdf"));
sqlScript.Replace("####LOGDATAFILENAME####", string.Concat(temp, "test_log.ldf"));
string[] splittedScript = new string[] { "\r\nGO\r\n" }; //remove GO
string[] commands = sqlScript.ToString().Split(splittedScript,
StringSplitOptions.RemoveEmptyEntries);
Then run every command in commands(SqlCommand cmd = new SqlCommand(command[x], baseConnection);)
Note: For some reasons this needs adminrights, so create a manifestfile.
You need to use SMO to complete this task. The normal ADO.NET stuff will complain about multi-statement execution and the like. It's really pretty easy once you integrated with SMO and have the scripts.
Visual Studio supports database projects, which generates deployment scripts for you. It also allows for deployments from scratch or to upgrade existing databases. The deployments can be automated as part a build within visual studio or the build server. If you're using TFS you can also source-control your database.
Basically no more messing about with scripts!
http://msdn.microsoft.com/en-us/library/ff678491(v=vs.100).aspx
http://msdn.microsoft.com/en-us/library/xee70aty.aspx
I have create a C# application that allows a database to be backed up and restored. It does this by first backing the database up to a local file on the Sql server using:
Backup backup = new Backup();
backup.Devices.AddDevice(Path.GetFullPath(backupFilePath), DeviceType.File);
...
backup.SqlBackup(server);
And then I create the new database by restoring from the backup file using:
Restore restore = new Restore();
restore.Devices.AddDevice(Path.GetFullPath(backupFileToRestoreFrom), DeviceType.File);
...
restore.SqlRestore(server);
After the new database has been created I want to delete the temp backup file that we created. Because I have admin rights on the Sql server box, I can delete the file on the remote server using:
File.Delete("\\SqlServer\C$\Backups\BackupFileToDelete.bak")
and it works. However, if somebody else who doesn't have rights on the Sql server box runs the app, it will throw an exception about not having permissions.
So is there a Sql SMO function that I can call to delete the backup file that was created on the remote Sql server?
If you delete the file using the xp_cmdshell then it will use the rights of the sql server instead of the rights of the person.
For example:
EXEC master..xp_cmdshell 'Del \\SqlServer\C$\Backups\BackupFileToDelete.bak', NO_OUTPUT
Here's the reference on xp_cmdshell
The important thing to note from that article is:
Because malicious users sometimes attempt to elevate their privileges
by using xp_cmdshell, xp_cmdshell is disabled by default. Use
sp_configure or Policy Based Management to enable it. For more
information, see xp_cmdshell Server Configuration Option.
Another possibility is to use SQLCLR, which is more secure than using xp_cmdshell. There's even a CodePlex project call SQLCLR File Functions that has the functionality written as stored procs for me. I think I'm going to see if I can convince our DBAs to go this route. The downside is that I believe the sprocs that this creates would need to be installed on every existing and new SQL Server that gets created; not much different than having to enable xp_cmdshell on every server though I guess.
I Created an application in c# with Visual Studio 2008, and i'am using SQL Server 2012 for implement databases for the applications.
My Question is :
How can i create an installation that contains the database without using the atach databse method.
Please F1 ! F1 !
You can export a script that will create and populate the database with data. What is the setup tool you are using ?
Or you can make the C# app create the database and default data in the first launch. In both cases you have to make a script file to create/populate the database.
To export the SQL script
right click on the database name in the SQL server manager
tasks->script
in C# open the scrip file using StreamReader and read all the lines. Then using SqlCommand execute the script file. First you need to open connections to Master database as you do not have your database created yet. Then modify the connection string and replace the master database with your database name.
The connection string can be stored in the Application Settings and you can provide a settings dialog to modify it for simplicity make the default connection string connect to the localhost computer and your database name.
The following code modify a setting key names Setting1
MessageBox.Show(Properties.Settings.Default.Setting1);
Properties.Settings.Default.Setting1 = "New Value";
Properties.Settings.Default.Save();
You can also create a custom action in your installer (.msi) file - here's a link that tells you the steps to do that. adding link to example with C# - here
I am developing a desktop application in c# & sql 2005. With candidate data entry form I want to provide option to attach required documents(in pdf format) wid data. Kindly let me know the best method. Thank you in advance.
Simply create a table that will contain the filename and server path for the file to be attached, then create a method to copy the attached file to the server location and store the relivant information (name and path) in the table. Use other methods to retrive the file from the server location when requested. Simple.
I personaly prefer to store the documents as BLOBs since server file structures and paths can change over time.
Well then unfortunately you have to either manage the file storage yourself using the servers file system, or you could store it in the db itself (IT WILL GET BLOATED!!!)
See sql server 2005 file storage
How To: Encrypt and Manage Documents with SQL Server 2005
OK, then for file management
see this example
File Manager Component