Sql代码
-- 备份数据库
backup database db_CSManage to disk="c:\backup.bak"
-- 还原数据库,必须先备份该数据库的日志文件到原先的备份文件中
backup log db_CSManage to disk="c:\backup.bak"
restore database db_CSManage from disk="c:\backup.bak"
-- 备份数据库
backup database db_CSManage to disk="c:\backup.bak"
-- 还原数据库,必须先备份该数据库的日志文件到原先的备份文件中
backup log db_CSManage to disk="c:\backup.bak"
restore database db_CSManage from disk="c:\backup.bak"
其中db_CSManage是数据库名称,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("数据备份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
else
{
MessageBox.Show("数据备份失败!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("请填写备份的正确位置及文件名!", "提示框", 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("数据备份成功!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
else
{
MessageBox.Show("数据备份失败!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("请填写备份的正确位置及文件名!", "提示框", MessageBoxButtons.OK, MessageBoxIcon.Information);
}// end
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
以下是还原数据库的代码,在示例中发现开头得先删除与该数据库相关的进程,然后在还原之前得先把数据库日志备份到开始的备份文件中,然后才还原备份文件,要不然会出错的,代码如下:
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 数据库的进程--------------
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(); //强行关闭用户进程
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();
石家庄网站建设的优点:我们是专属定制,制作符合您公司业务、风格的网站,这就做到给您制作的网站是网络上独一无二的网站,这样更有利于搜索引擎的收录。如果您喜欢网络上某个风格的网站或者网站模板,我们是只仿制而不直接套用,我们会对其中的结构和样式做增减优化,做…
竞价排名营销是一种通过 竞价购买搜索引擎中关键词或关键短语的广告方式。竞价排名营销分为两种类型。 第一种,直接为真正的搜索引擎结果的排名付费,也就是说付的钱越多,网页所获得的排名名也就越高。 第二种则更类似于普通的广告。这种竞价排名营销方式通过…
网站优化对网站来说是非常重要的,直接决定和影响着网站所起到的作用,以及在各个地方当中的排名。在进行seo优化的过程中,我们必须要坚持一定的原则,真正的把这些事情做得更好,才可以确保最终的结果,这对于我们来说是非常重要的事。 SEO优化主要分为8小步: …