当前位置: 编程技术>.net/c#/asp.net
C# 备份数据库的简单代码
来源: 互联网 发布时间:2014-08-30
本文导语: 代码如下: try { //备份数据库 string strg = Application.StartupPath.ToString(); strg = strg.Substring(0, strg.LastIndexOf("\")); strg = strg.Substring(0, strg.LastIndexOf("\")); strg += @"Data"; string sqltxt = @"BACKUP DATABASE db_MrCy TO Disk=...
代码如下:
try { //备份数据库 string strg = Application.StartupPath.ToString(); strg = strg.Substring(0, strg.LastIndexOf("\")); strg = strg.Substring(0, strg.LastIndexOf("\")); strg += @"Data"; string sqltxt = @"BACKUP DATABASE db_MrCy TO Disk='" + strg + "\" + txtpath.Text +".bak"+ "'"; SqlConnection conn = BaseClass.DBConn.CyCon(); conn.Open(); SqlCommand cmd = new SqlCommand(sqltxt, conn); cmd.ExecuteNonQuery(); conn.Close(); if (MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) { this.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); }