0% found this document useful (0 votes)
30 views7 pages

Viet-Nam HTML

The document discusses code for backing up and restoring a SQL Server database in C#. It includes code snippets showing how to connect to the database, run backup and restore commands, and handle exceptions. The code provides a way to backup a database to a file location and restore it from that backup file location.

Uploaded by

Xuan Hoa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views7 pages

Viet-Nam HTML

The document discusses code for backing up and restoring a SQL Server database in C#. It includes code snippets showing how to connect to the database, run backup and restore commands, and handle exceptions. The code provides a way to backup a database to a file location and restore it from that backup file location.

Uploaded by

Xuan Hoa
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

a ch: KHU CNG CNG 2 BC LINH M, P.I KIM, Q.HONG MAI, TP. H NI in thoi: (04)6413378 http://www.buaxua.

vn | Xem tip: http://www.buaxua.vn/Thong-tin/Dia-chi-cua-cac-Sieu-thi-oViet-Nam.html

sao lu v khi phc d liu private void btRestore_Click(object sender, EventArgs e) { try { string path = "D:\\backupUITGPP.bak"; string sqlRestore = "Use master Restore Database [UITGPP] fro m disk='" + path + "'"; SqlConnection con = new SqlConnection("Data Source=ACUMENPC\\SQLEXPRESS;Initial Catalog=Ketnoimoi;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand(sqlRestore, con); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Database da duoc restore "); } catch (SqlException ex) { MessageBox.Show(ex.Message, "Restore Database"); return; } catch (Exception ex) { } }

private void btBackUp_Click(object sender, EventArgs e) { try { string sqlBackup = "BACKUP DATABASE [UITGPP] TO DISK='D:\\bac kupUITGPP.bak'"; SqlConnection con=new SqlConnection("Data Source=ACUMENPC\\SQLEXPRESS;Initial Catalog=UITGPP;Integrated Security=True"); con.Open(); SqlCommand cmd = new SqlCommand(sqlBackup, con); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show(" backup c s d liu"); } catch (SqlException ex) { MessageBox.Show(ex.Message, "Backup Database"); return; } catch (Exception ex) { } }

bool run = true; String strConn = "SERVER=" + txtSERVER.Text + ";UID=" + txtUID.Text + ";PWD=" + txtPWD.Text; string strSQLBackup = " USE master EXEC sp_dropdevice 'MYBACKUP' " + " EXEC sp_addumpdevice 'disk', 'MYBACKUP','##PATH##'" + " BACKUP DATABASE ##DATABASE## TO MYBACKUP "; try { strSQLBackup = strSQLBackup.Replace("##PATH##", this.txtPath.Text.Trim()); strSQLBackup = strSQLBackup.Replace("##DATABASE##", this.txtDATABASE.Text.Trim()); //Kim tra kt ni SqlConnection cn = new SqlConnection(strConn); try { cn.Open(); } catch (Exception) { MessageBox.Show("Cu hnh cha ng , vui lng chn li!!!"); return; } //Run backup try { btnChapnhan.Enabled = run; btnChapnhan.ImageIndex = 2; SqlCommand cmdBackup = new SqlCommand(strSQLBackup, cn); cmdBackup.ExecuteScalar(); } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { cn.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message); }

string strConn = "SERVER=" + txtSERVER.Text + ";UID=" + txtUID.Text + ";PWD=" + txtPWD.Text; string strSQLBackup = " USE master Exec sp_databases "; SqlConnection cn = new SqlConnection(strConn); try { cn.Open(); } catch { } SqlCommand cmdBackup = new SqlCommand(strSQLBackup, cn); object ob = cmdBackup.ExecuteNonQuery();

Bi vit ny ti xin trnh by cch gii quyt Backup v Restore CSDL SQL Server c vit bng C#. Nu c vn g cn thiu khuyt, xin c nhn kin ng gp phn Bnh lun. Rt cm n cc bn quan tm. //Th tc kt ni d liu private void Connect() { try { myCnn = new SqlConnection("Data Source=localhost; Initial Catalog='';User ID=sa;Password=12345;"); myCnn.Open(); if (myCnn.State != ConnectionState.Open) { //Nu kt ni b li th thng bo cho ngi dng bit Interaction.MsgBox("Khng th kt ni n Server, hy kim tra li my ch CSDL ca bn" + Strings.Chr(13) + "Hoc lin h ti tc gi qua email ", MsgBoxStyle.Critical, "Li kt ni"); } } catch (Exception ex) { //Nu c li th bo li Interaction.MsgBox("Khng th kt ni n Server, hy kim tra li my ch CSDL ca bn" + Strings.Chr(13) + "Hoc lin h ti tc gi qua email ", MsgBoxStyle.Critical, "Li kt ni"); System.Environment.Exit(0); } } public void close_Cnn() { //ng kt ni n CSDL if (myCnn.State == ConnectionState.Open) { myCnn.Close(); myCnn.Dispose(); } } public void Create_Cmd(string strSQL) { //To Command ly d liu Cmd = myCnn.CreateCommand(); Cmd.CommandText = strSQL; Cmd.ExecuteNonQuery(); } private void Button1_Click(System.Object sender, System.EventArgs e) { try { Connect(); Create_Cmd("BACKUP DATABASE TENDATABASE TO DISK = N'ng dn cn sao lu\\tenfile.bak'"); close_Cnn(); Interaction.MsgBox("CSDL c backup hon tt"); } catch (Exception ex) { Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical, "Li Backup"); System.Environment.Exit(0); } }

private void Button2_Click(System.Object sender, System.EventArgs e) { try { Connect(); Create_Cmd("RESTORE DATABASE TENDATABASE FROM DISK = N'ng dn sao lu\\tn file.bak' WITH FILE = 1, NOUNLOAD, STATS = 10"); close_Cnn(); } catch (Exception ex) { Interaction.MsgBox(ex.Message, MsgBoxStyle.Critical, "Li Restore"); System.Environment.Exit(0); } } }

You might also like