C#备份数据库文件到本地磁盘

private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string strg = Application.StartupPath.ToString();
                strg = strg.Substring(0, strg.LastIndexOf("\\"));
                strg = strg.Substring(0, strg.LastIndexOf("\\"));
                strg += @"\Data";
                MessageBox.Show(strg);
                MessageBox.Show( txtpath.Text);
                string sqltxt = @"BACKUP DATABASE SIC 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());
            }

你可能感兴趣的:(C#)