18-08-16, 11:54 AM
وعليكم السلام ورحمة الله وبركاته
مفاد الرسالة قاعدة البيانات مستخدمة يعني فيه عامل اتصال فعال جرب تقطع الاتصال بقاعدة البيانات بشكل كامل وتعيد التجرية.
اوتابع هذا المثال
In SQL Server
The following query gives the name of the database and the server name:
Select * from sysservers where srvproduct='SQL Server'
go
Select * from sysdatabases
Output
![[صورة مرفقة: DatabaseName-and-ServerName-in-SQLServer.jpg]](http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/Images/DatabaseName-and-ServerName-in-SQLServer.jpg)
If you want to restore a database from a backup file, just execute the following query in SQL Server Management Studio.
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = 'test')
DROP DATABASE test RESTORE DATABASE test FROM DISK = 'E:/test.bak'
The backup of the student database has been created on the given location.
Output
![[صورة مرفقة: Restore-command-in-SQL-Server.jpg]](http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/Images/Restore-command-in-SQL-Server.jpg)
In Visual Studio 2010,
The SQL Server query above returns the server name and all database names. Now execute it using C# code. To do that create a Windows Forms application and drag and drop the following control onto the form.
![[صورة مرفقة: Restore-form-in-Visual-Studio.jpg]](http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/Images/Restore-form-in-Visual-Studio.jpg)
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Data.SqlClient;
namespace SQLBackUpApp
{
public partial class Form1 : Form
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label3.Visible = false;
serverName(".");
}
public void serverName(string str)
{
con = new SqlConnection("Data Source=" + str + ";Database=Master;data source=.; uid=sa; pwd=Micr0s0ft;");
con.Open();
cmd = new SqlCommand("select * from sysservers where srvproduct='SQL Server'", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
ComboBoxserverName.Items.Add(dr[2]);
}
dr.Close();
}
public void Createconnection()
{
con = new SqlConnection("Data Source=" + (ComboBoxserverName.Text) +";Database=Master;data source=.; uid=sa; pwd=Micr0s0ft;");
con.Open();
ComboBoxDatabaseName.Items.Clear();
cmd = new SqlCommand("select * from sysdatabases", con);
dr = cmd.ExecuteReader();
while(dr.Read())
{
ComboBoxDatabaseName.Items.Add(dr[0]);
}
dr.Close();
}
public void query(string que)
{
// ERROR: Not supported in C#: OnErrorStatement
cmd = new SqlCommand(que, con);
cmd.ExecuteNonQuery();
}
public void blank(string str)
{
if (string.IsNullOrEmpty(ComboBoxserverName.Text) |string.IsNullOrEmpty(ComboBoxDatabaseName.Text))
{
// label3.Visible = true;
MessageBox .Show("Server Name & Database can not be Blank");
return;
}
else
{
if (str == "restore")
{
OpenFileDialog1.ShowDialog();
// string a = ComboBoxDatabaseName.Text.ToString();
query("IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'" + ComboBoxDatabaseName.Text + "') DROP DATABASE " + ComboBoxDatabaseName.Text + " RESTORE DATABASE " + ComboBoxDatabaseName.Text + " FROM DISK = '" + OpenFileDialog1.FileName +"'");
label3.Visible = true;
label3.Text = "Database Backup file has been restore successfully";
}
}
}
private void ComboBoxserverName_SelectedIndexChanged(object sender,EventArgs e)
{
Createconnection();
}
private void cmbrestore_Click(object sender, EventArgs e)
{
blank("restore");
}
}
}
In the code above you can change the connection string corresponding to your database.
Now run the application and select the server name and database name to restore the database backup file.
![[صورة مرفقة: DatabaseName-and-ServerName-to-Restore-i...Server.jpg]](http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/Images/DatabaseName-and-ServerName-to-Restore-in-SQL-Server.jpg)
Now click on the "Restore" Button and select the backup file location from the disk.
![[صورة مرفقة: Database-restore-in-Visual-Studio.jpg]](http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/Images/Database-restore-in-Visual-Studio.jpg)
Now open the selected database to see the backup file data.
------
المصدر
مفاد الرسالة قاعدة البيانات مستخدمة يعني فيه عامل اتصال فعال جرب تقطع الاتصال بقاعدة البيانات بشكل كامل وتعيد التجرية.
اوتابع هذا المثال
In SQL Server
The following query gives the name of the database and the server name:
Select * from sysservers where srvproduct='SQL Server'
go
Select * from sysdatabases
Output
![[صورة مرفقة: DatabaseName-and-ServerName-in-SQLServer.jpg]](http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/Images/DatabaseName-and-ServerName-in-SQLServer.jpg)
If you want to restore a database from a backup file, just execute the following query in SQL Server Management Studio.
IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = 'test')
DROP DATABASE test RESTORE DATABASE test FROM DISK = 'E:/test.bak'
The backup of the student database has been created on the given location.
Output
![[صورة مرفقة: Restore-command-in-SQL-Server.jpg]](http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/Images/Restore-command-in-SQL-Server.jpg)
In Visual Studio 2010,
The SQL Server query above returns the server name and all database names. Now execute it using C# code. To do that create a Windows Forms application and drag and drop the following control onto the form.
![[صورة مرفقة: Restore-form-in-Visual-Studio.jpg]](http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/Images/Restore-form-in-Visual-Studio.jpg)
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Data.SqlClient;
namespace SQLBackUpApp
{
public partial class Form1 : Form
{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
label3.Visible = false;
serverName(".");
}
public void serverName(string str)
{
con = new SqlConnection("Data Source=" + str + ";Database=Master;data source=.; uid=sa; pwd=Micr0s0ft;");
con.Open();
cmd = new SqlCommand("select * from sysservers where srvproduct='SQL Server'", con);
dr = cmd.ExecuteReader();
while (dr.Read())
{
ComboBoxserverName.Items.Add(dr[2]);
}
dr.Close();
}
public void Createconnection()
{
con = new SqlConnection("Data Source=" + (ComboBoxserverName.Text) +";Database=Master;data source=.; uid=sa; pwd=Micr0s0ft;");
con.Open();
ComboBoxDatabaseName.Items.Clear();
cmd = new SqlCommand("select * from sysdatabases", con);
dr = cmd.ExecuteReader();
while(dr.Read())
{
ComboBoxDatabaseName.Items.Add(dr[0]);
}
dr.Close();
}
public void query(string que)
{
// ERROR: Not supported in C#: OnErrorStatement
cmd = new SqlCommand(que, con);
cmd.ExecuteNonQuery();
}
public void blank(string str)
{
if (string.IsNullOrEmpty(ComboBoxserverName.Text) |string.IsNullOrEmpty(ComboBoxDatabaseName.Text))
{
// label3.Visible = true;
MessageBox .Show("Server Name & Database can not be Blank");
return;
}
else
{
if (str == "restore")
{
OpenFileDialog1.ShowDialog();
// string a = ComboBoxDatabaseName.Text.ToString();
query("IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'" + ComboBoxDatabaseName.Text + "') DROP DATABASE " + ComboBoxDatabaseName.Text + " RESTORE DATABASE " + ComboBoxDatabaseName.Text + " FROM DISK = '" + OpenFileDialog1.FileName +"'");
label3.Visible = true;
label3.Text = "Database Backup file has been restore successfully";
}
}
}
private void ComboBoxserverName_SelectedIndexChanged(object sender,EventArgs e)
{
Createconnection();
}
private void cmbrestore_Click(object sender, EventArgs e)
{
blank("restore");
}
}
}
In the code above you can change the connection string corresponding to your database.
Now run the application and select the server name and database name to restore the database backup file.
![[صورة مرفقة: DatabaseName-and-ServerName-to-Restore-i...Server.jpg]](http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/Images/DatabaseName-and-ServerName-to-Restore-in-SQL-Server.jpg)
Now click on the "Restore" Button and select the backup file location from the disk.
![[صورة مرفقة: Database-restore-in-Visual-Studio.jpg]](http://csharpcorner.mindcrackerinc.netdna-cdn.com/UploadFile/rohatash/restore-sql-server-backup-file-with-C-Sharp/Images/Database-restore-in-Visual-Studio.jpg)
Now open the selected database to see the backup file data.
------
المصدر
سبحان الله والحمدلله ولا إله إلا الله والله أكبر
اللهم اغْفِرْ لِلمؤمنين والمؤمنات والمسلمين والمسلمات الأحياء منهم والأموات


