Sql代碼
-- 備份數(shù)據(jù)庫
backup database db_CSManage to disk="c:\backup.bak"
-- 還原數(shù)據(jù)庫,必須先備份該數(shù)據(jù)庫的日志文件到原先的備份文件中
backup log db_CSManage to disk="c:\backup.bak"
restore database db_CSManage from disk="c:\backup.bak"
-- 備份數(shù)據(jù)庫
backup database db_CSManage to disk="c:\backup.bak"
-- 還原數(shù)據(jù)庫,必須先備份該數(shù)據(jù)庫的日志文件到原先的備份文件中
backup log db_CSManage to disk="c:\backup.bak"
restore database db_CSManage from disk="c:\backup.bak"
其中db_CSManage是數(shù)據(jù)庫名稱,disk后的路徑即是備份文件存儲的路徑。
知道了SQL語句,那么在.NET代碼中就容易多了,備份的.NET代碼如下:
C#代碼
try
{
if (txtPath.Text != "" && txtName122.Text != "")
{
getSqlConnection geCon = new getSqlConnection();
SqlConnection con = geCon.GetCon();
string strBacl = "backup database db_CSManage to disk="" + txtPath.Text.Trim() + "\\" + txtName.Text.Trim() + ".bak"";
SqlCommand Cmd = new SqlCommand(strBacl, con);
if (Cmd.ExecuteNonQuery() != 0)
{
MessageBox.Show("數(shù)據(jù)備份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
else
{
MessageBox.Show("數(shù)據(jù)備份失??!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("請?zhí)顚憘浞莸恼_位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
}// end
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
try
{
if (txtPath.Text != "" && txtName122.Text != "")
{
getSqlConnection geCon = new getSqlConnection();
SqlConnection con = geCon.GetCon();
string strBacl = "backup database db_CSManage to disk="" + txtPath.Text.Trim() + "\\" + txtName.Text.Trim() + ".bak"";
SqlCommand Cmd = new SqlCommand(strBacl, con);
if (Cmd.ExecuteNonQuery() != 0)
{
MessageBox.Show("數(shù)據(jù)備份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
else
{
MessageBox.Show("數(shù)據(jù)備份失敗!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("請?zhí)顚憘浞莸恼_位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
}// end
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
以下是還原數(shù)據(jù)庫的代碼,在示例中發(fā)現(xiàn)開頭得先刪除與該數(shù)據(jù)庫相關(guān)的進程,然后在還原之前得先把數(shù)據(jù)庫日志備份到開始的備份文件中,然后才還原備份文件,要不然會出錯的,代碼如下:
C#代碼
if (textPaht.Text != "")
{
getSqlConnection geCon = new getSqlConnection();
SqlConnection con = geCon.GetCon();
if (con.State == ConnectionState.Open)
{
con.Close();
}
string DateStr = "Data Source=niunan\\sqlexpress;Database=master;User id=sa;PWD=123456";
SqlConnection conn = new SqlConnection(DateStr);
conn.Open();
//-------------------殺掉所有連接 db_CSManage 數(shù)據(jù)庫的進程--------------
string strSQL = "select spid from master..sysprocesses where dbid=db_id( "db_CSManage") ";
SqlDataAdapter Da = new SqlDataAdapter(strSQL, conn);
DataTable spidTable = new DataTable();
Da.Fill(spidTable);
SqlCommand Cmd = new SqlCommand();
Cmd.CommandType = CommandType.Text;
Cmd.Connection = conn;
for (int iRow = 0; iRow <= spidTable.Rows.Count - 1; iRow++)
{
Cmd.CommandText = "kill " + spidTable.Rows[iRow][0].ToString(); //強行關(guān)閉用戶進程
Cmd.ExecuteNonQuery();
}
conn.Close();
conn.Dispose();
//--------------------------------------------------------------------
SqlConnection sqlcon = new SqlConnection(DateStr);
sqlcon.Open();
string sql = "backup log db_CSManage to disk="" + textPaht.Text.Trim() + "" restore database db_CSManage from disk="" + textPaht.Text.Trim() + """;
SqlCommand sqlCmd = new SqlCommand(sql, sqlcon);
sqlCmd.ExecuteNonQuery();
sqlCmd.Dispose();
sqlcon.Close();
?石家莊網(wǎng)站建設(shè)的優(yōu)點:我們是專屬定制,制作符合您公司業(yè)務(wù)、風(fēng)格的網(wǎng)站,這就做到給您制作的網(wǎng)站是網(wǎng)絡(luò)上獨一無二的網(wǎng)站,這樣更有利于搜索引擎的收錄。如果您喜歡網(wǎng)絡(luò)上某個風(fēng)格的網(wǎng)站或者網(wǎng)站模板,我們是只仿制而不直接套用,我們會對其中的結(jié)構(gòu)和樣式做增減優(yōu)化,做…
?競價排名營銷是一種通過 競價購買搜索引擎中關(guān)鍵詞或關(guān)鍵短語的廣告方式。競價排名營銷分為兩種類型。 第一種,直接為真正的搜索引擎結(jié)果的排名付費,也就是說付的錢越多,網(wǎng)頁所獲得的排名名也就越高。 第二種則更類似于普通的廣告。這種競價排名營銷方式通過…
?網(wǎng)站優(yōu)化對網(wǎng)站來說是非常重要的,直接決定和影響著網(wǎng)站所起到的作用,以及在各個地方當(dāng)中的排名。在進行seo優(yōu)化的過程中,我們必須要堅持一定的原則,真正的把這些事情做得更好,才可以確保最終的結(jié)果,這對于我們來說是非常重要的事。 SEO優(yōu)化主要分為8小步: …