 |
|
คัยเคยเขียนโปรแกรม Backup database มั่งครับ ขอตัวอย่างหน่อยครับ |
|
 |
|
|
 |
 |
|
นิดหน่อยคือตรงไหนเอย ?
ติดตั้งแต่ตอนรันเลยหรือป่าว ?
ของผมก็เอามาจาก ตย. เดียวกับคุณนั้นแหละ(จำไม่ได้แล้วว่าเว็บไหน) รันแล้ว Error !
แต่ผมกำหนดค่าให้ Server กับ Username เลย ทำให้ใช้งานได้ปกติ ไม่มีปัญหา !!
อันนี้คือที่ผมทดลองเล่นยังไม่ได้แก้ไขเพื่อไปใช้งาน จริง เอาไปลองดูครับ !
Code (C#)
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;
namespace SqlBackUp
{
public partial class Form1 : Form
{
private static Server srvSql;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//// Create a DataTable where we enumerate the available servers
//DataTable dtServers = SmoApplication.EnumAvailableSqlServers(true);
//// If there are any servers at all
//if (dtServers.Rows.Count > 0)
//{
// // Loop through each server in the DataTable
// foreach (DataRow drServer in dtServers.Rows)
// {
// // Add the name to the combobox
// cmbServer.Items.Add(drServer["Name"]);
// }
//}
ServerConnection srvConn = new ServerConnection(comboBox1.Text);
srvConn.LoginSecure = false;
srvConn.Login = txtUsername.Text;
srvConn.Password = txtPassword.Text;
srvSql = new Server(srvConn);
}
private void btnConnect_Click(object sender, EventArgs e)
{
//**************************************************
// If a server was selected at all from the combobox
if (comboBox1.Text != null && comboBox1.Text != "")
{
// Create a new connection to the selected server name
ServerConnection srvConn = new ServerConnection(comboBox1.Text);
// Log in using SQL authentication instead of Windows authentication
srvConn.LoginSecure = false;
// Give the login username
srvConn.Login = txtUsername.Text;
// Give the login password
srvConn.Password = txtPassword.Text;
// Create a new SQL Server object using the connection we created
srvSql = new Server(srvConn);
// Loop through the databases list
foreach (Database dbServer in srvSql.Databases)
{
// Add database to combobox
comboBox2.Items.Add(dbServer.Name);
}
}
else
{
// A server was not selected, show an error message
MessageBox.Show("Please select a server first", "Server Not Selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void btnCreate_Click(object sender, EventArgs e)
{
// If there was a SQL connection created
if (srvSql != null)
{
// If the user has chosen a path where to save the backup file
if (saveBackupDialog.ShowDialog() == DialogResult.OK)
{
// Create a new backup operation
Backup bkpDatabase = new Backup();
// Set the backup type to a database backup
bkpDatabase.Action = BackupActionType.Database;
// Set the database that we want to perform a backup on
bkpDatabase.Database = comboBox2.Text;
// Set the backup device to a file
BackupDeviceItem bkpDevice = new BackupDeviceItem(saveBackupDialog.FileName, DeviceType.File);
// Add the backup device to the backup
bkpDatabase.Devices.Add(bkpDevice);
// Perform the backup
bkpDatabase.SqlBackup(srvSql);
}
}
else
{
// There was no connection established; probably the Connect button was not clicked
MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void btnRestore_Click(object sender, EventArgs e)
{
// If there was a SQL connection created
if (srvSql != null)
{
// If the user has chosen the file from which he wants the database to be restored
if (openBackupDialog.ShowDialog() == DialogResult.OK)
{
// Create a new database restore operation
Restore rstDatabase = new Restore();
// Set the restore type to a database restore
rstDatabase.Action = RestoreActionType.Database;
// Set the database that we want to perform the restore on
rstDatabase.Database = comboBox2.Text;
// Set the backup device from which we want to restore, to a file
BackupDeviceItem bkpDevice = new BackupDeviceItem(openBackupDialog.FileName, DeviceType.File);
// Add the backup device to the restore type
rstDatabase.Devices.Add(bkpDevice);
// If the database already exists, replace it
rstDatabase.ReplaceDatabase = true;
// Perform the restore
rstDatabase.SqlRestore(srvSql);
}
}
else
{
// There was no connection established; probably the Connect button was not clicked
MessageBox.Show("A connection to a SQL server was not established.", "Not Connected to Server", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}
}
|
 |
 |
 |
 |
Date :
2011-09-23 15:14:16 |
By :
@TEENEE |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แล้วถ้าผมเขียนแบบไม่ต้องใส่ username กับ pass ล่ะครับ ต้องปรับเปลี่ยนตรงไหน ยังไงอ่ะครับ คือผมใช้ Windows Authentification อ่าครับ
|
 |
 |
 |
 |
Date :
2011-09-23 19:38:33 |
By :
farkram |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
มันทำ Restore ไม่ได้ครับ
|
 |
 |
 |
 |
Date :
2011-09-28 16:21:11 |
By :
farkram |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|