C#生成安装文件后自动附加数据库的思路跟算法

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;
using System.ServiceProcess;

namespace AdminZJC.DataBaseControl
{
 /// <summary>
 /// 数据库操作控制类
 /// </summary>
 public class DataBaseControl
 {
 /// <summary>
 /// 数据库连接字符串
 /// </summary>
 public string ConnectionString;

 /// <summary>
 /// SQL操作语句/存储过程
 /// </summary>
 public string StrSQL;

 /// <summary>
 /// 实例化一个数据库连接对象
 /// </summary>
 private SqlConnection Conn;

 /// <summary>
 /// 实例化一个新的数据库操作对象Comm
 /// </summary>
 private SqlCommand Comm;

 /// <summary>
 /// 要操作的数据库名称
 /// </summary>
 public string DataBaseName;

 /// <summary>
 /// 数据库文件完整地址
 /// </summary>
 public string DataBase_MDF;

 /// <summary>
 /// 数据库日志文件完整地址
 /// </summary>
 public string DataBase_LDF;

 /// <summary>
 /// 备份文件名
 /// </summary>
 public string DataBaseOfBackupName;

 /// <summary>
 /// 备份文件路径
 /// </summary>
 public string DataBaseOfBackupPath;

 /// <summary>
 /// 执行创建/修改数据库和表的操作
 /// </summary>
 public void DataBaseAndTableControl()
 {
 try
 {
 Conn = new SqlConnection(ConnectionString);
 Conn.Open();

 Comm = new SqlCommand();
 Comm.Connection = Conn;
 Comm.CommandText = StrSQL;
 Comm.CommandType = CommandType.Text;
 Comm.ExecuteNonQuery();

 MessageBox.Show("数据库操作成功!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 finally
 {
 Conn.Close();
 }
 }

 /// <summary>
 /// 附加数据库
 /// </summary>
 public void AddDataBase()
 {
 try
 {
 Conn = new SqlConnection(ConnectionString);
 Conn.Open();

 Comm = new SqlCommand();
 Comm.Connection = Conn;
 Comm.CommandText = "sp_attach_db";

 Comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));
 Comm.Parameters[@"dbname"].Value = DataBaseName;
 Comm.Parameters.Add(new SqlParameter(@"filename1", SqlDbType.NVarChar));
 Comm.Parameters[@"filename1"].Value = DataBase_MDF;
 Comm.Parameters.Add(new SqlParameter(@"filename2", SqlDbType.NVarChar));
 Comm.Parameters[@"filename2"].Value = DataBase_LDF;

 Comm.CommandType = CommandType.StoredProcedure;
 Comm.ExecuteNonQuery();

 MessageBox.Show("附加数据库成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 finally
 {
 Conn.Close();
 }
 }

 /// <summary>
 /// 分离数据库
 /// </summary>
 public void DeleteDataBase()
 {
 try
 {
 Conn = new SqlConnection(ConnectionString);
 Conn.Open();

 Comm = new SqlCommand();
 Comm.Connection = Conn;
 Comm.CommandText = @"sp_detach_db";

 Comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));

 Comm.Parameters[@"dbname"].Value = DataBaseName;

 Comm.CommandType = CommandType.StoredProcedure;
 Comm.ExecuteNonQuery();

 MessageBox.Show("分离数据库成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 finally
 {
 Conn.Close();
 }
 }

 /// <summary>
 /// 备份数据库
 /// </summary>
 public void BackupDataBase()
 {
 try
 {
 Conn = new SqlConnection(ConnectionString);
 Conn.Open();

 Comm = new SqlCommand();
 Comm.Connection = Conn;
 Comm.CommandText = "use master;backup database @dbname to disk = @backupname;";

 Comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));
 Comm.Parameters[@"dbname"].Value = DataBaseName;
 Comm.Parameters.Add(new SqlParameter(@"backupname", SqlDbType.NVarChar));
 Comm.Parameters[@"backupname"].Value = @DataBaseOfBackupPath + @DataBaseOfBackupName;

 Comm.CommandType = CommandType.Text;
 Comm.ExecuteNonQuery();

 MessageBox.Show("备份数据库成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 finally
 {
 Conn.Close();
 }
 }

 /// <summary>
 /// 还原数据库
 /// </summary>
 public void ReplaceDataBase()
 {
 try
 {
 string BackupFile = @DataBaseOfBackupPath + @DataBaseOfBackupName;
 Conn = new SqlConnection(ConnectionString);
 Conn.Open();

 Comm = new SqlCommand();
 Comm.Connection = Conn;
 Comm.CommandText = "use master;restore database @DataBaseName From disk = @BackupFile with replace;";

 Comm.Parameters.Add(new SqlParameter(@"DataBaseName", SqlDbType.NVarChar));
 Comm.Parameters[@"DataBaseName"].Value = DataBaseName;
 Comm.Parameters.Add(new SqlParameter(@"BackupFile", SqlDbType.NVarChar));
 Comm.Parameters[@"BackupFile"].Value = BackupFile;

 Comm.CommandType = CommandType.Text;
 Comm.ExecuteNonQuery();

 MessageBox.Show("还原数据库成功", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 catch (Exception ex)
 {
 MessageBox.Show(ex.Message, "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
 }
 finally
 {
 Conn.Close();
 }
 }
 }
}

/*
 ///调用事例:
 
    还原数据库
 private void button0_Click(object sender, EventArgs e)
 {
 DataBaseControl DBC = new DataBaseControl();
 DBC.ConnectionString = "Data Source=(local);User id=sa;Password=123456; Initial Catalog=master";
 DBC.DataBaseName = "MyDatabase";
 DBC.DataBaseOfBackupName = @"back.bak";
 DBC.DataBaseOfBackupPath = @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/";
 DBC.ReplaceDataBase();
 }
 
    附加数据库
 private void button1_Click_1(object sender, EventArgs e)
 {
 DataBaseControl DBC = new DataBaseControl();
 DBC.ConnectionString = "Data Source=(local);User id=sa;Password=123456; Initial Catalog=master";
 DBC.DataBaseName = "MyDatabase";
 DBC.DataBase_MDF = @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/MyDatabase_Data.MDF";
 DBC.DataBase_LDF = @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/MyDatabase_Log.LDF";
 DBC.AddDataBase();
 }
 
    备份数据库
 private void button2_Click(object sender, EventArgs e)
 {
 DataBaseControl DBC = new DataBaseControl();
 DBC.ConnectionString = "Data Source=(local);User id=sa;Password=123456; Initial Catalog=master";
 DBC.DataBaseName = "MyDatabase";
 DBC.DataBaseOfBackupName = @"back.bak";
 DBC.DataBaseOfBackupPath = @"D:/Program Files/Microsoft SQL Server/MSSQL/Data/";
 DBC.BackupDataBase();
 }
 
    分离数据库
 private void button3_Click(object sender, EventArgs e)
 {
 DataBaseControl DBC = new DataBaseControl();
 DBC.ConnectionString = "Data Source=(local);User id=sa;Password=123456; Initial Catalog=master";
 DBC.DataBaseName = "MyDatabase";
 DBC.DeleteDataBase();
 }

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/389216.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python交互式和文件式_使用Python创建和自动化交互式仪表盘

python交互式和文件式In this tutorial, I will be creating an automated, interactive dashboard of Texas COVID-19 case count by county using python with the help of selenium, pandas, dash, and plotly. I am assuming the reader has some familiarity with python,…

不可不说的Java“锁”事

2019独角兽企业重金招聘Python工程师标准>>> 前言 Java提供了种类丰富的锁&#xff0c;每种锁因其特性的不同&#xff0c;在适当的场景下能够展现出非常高的效率。本文旨在对锁相关源码&#xff08;本文中的源码来自JDK 8&#xff09;、使用场景进行举例&#xff0c…

数据可视化 信息可视化_可视化数据以帮助清理数据

数据可视化 信息可视化The role of a data scientists involves retrieving hidden relationships between massive amounts of structured or unstructured data in the aim to reach or adjust certain business criteria. In recent times this role’s importance has been…

VS2005 ASP.NET2.0安装项目的制作(包括数据库创建、站点创建、IIS属性修改、Web.Config文件修改)

站点&#xff1a; 如果新建默认的Web安装项目&#xff0c;那它将创建的默认网站下的一个虚拟应用程序目录而不是一个新的站点。故我们只有创建新的安装项目&#xff0c;而不是Web安装项目。然后通过安装类进行自定义操作&#xff0c;创建新站如下图&#xff1a; 2、创建新的安项…

docker的基本命令

docker的三大核心&#xff1a;仓库(repository),镜像(image),容器(container)三者相互转换。 1、镜像(image) 镜像&#xff1a;组成docker容器的基础.类似安装系统的镜像 docker pull tomcat 通过pull来下载tomcat docker push XXXX 通过push的方式发布镜像 2、容器(container)…

seaborn添加数据标签_常见Seaborn图的数据标签快速指南

seaborn添加数据标签In the course of my data exploration adventures, I find myself looking at such plots (below), which is great for observing trend but it makes it difficult to make out where and what each data point is.在进行数据探索的过程中&#xff0c;我…

使用python pandas dataframe学习数据分析

⚠️ Note — This post is a part of Learning data analysis with python series. If you haven’t read the first post, some of the content won’t make sense. Check it out here.Note️ 注意 -这篇文章是使用python系列学习数据分析的一部分。 如果您还没有阅读第一篇文…

实现TcpIp简单传送

private void timer1_Tick(object sender, EventArgs e) { IPAddress ipstr IPAddress.Parse("192.168.0.106"); TcpListener serverListener new TcpListener(ipstr,13);//创建TcpListener对象实例 ser…

SQLServer之函数简介

用户定义函数定义 与编程语言中的函数类似&#xff0c;SQL Server 用户定义函数是接受参数、执行操作&#xff08;例如复杂计算&#xff09;并将操作结果以值的形式返回的例程。 返回值可以是单个标量值或结果集。 用户定义函数准则 在函数中&#xff0c;将会区别处理导致语句被…

无向图g的邻接矩阵一定是_矩阵是图

无向图g的邻接矩阵一定是To study structure,tear away all flesh soonly the bone shows.要研究结构&#xff0c;请尽快撕掉骨头上所有的肉。 Linear algebra. Graph theory. If you are a data scientist, you have encountered both of these fields in your study or work …

移动pc常用Meta标签

移动常用 <meta charset"UTF-8"><title>{$configInfos[store_title]}</title><meta content"widthdevice-width,minimum-scale1.0,maximum-scale1.0,shrink-to-fitno,user-scalableno,minimal-ui" name"viewport"><m…

前端绘制绘制图表_绘制我的文学风景

前端绘制绘制图表Back when I was a kid, I used to read A LOT of books. Then, over the last couple of years, movies and TV series somehow stole the thunder, and with it, my attention. I did read a few odd books here and there, but not with the same ferocity …

Rapi

本页内容 ●引言●SMARTPHONE SDK API 库●管理设备中的目录文件●取系统信息●远程操作电话和短信功能 Windows Mobile日益成熟&#xff0c;开发者队伍也越来越壮大。作为一个10年的计算机热爱者和程序员&#xff0c;我也经受不住新技术的诱惑&#xff0c;倒腾起Mobile这个玩具…

android 字符串特殊字符转义

XML转义字符 以下为XML标志符的数字和字符串转义符 " ( 或 &quot;) ( 或 &apos;) & ( 或 &amp;) lt(<) (< 或 <) gt(>) (> 或 >) 如题&#xff1a; 比如&#xff1a;在string.xml中定义如下一个字符串&#xff0c;…

如何描绘一个vue的项目_描绘了一个被忽视的幽默来源

如何描绘一个vue的项目Source)来源 ) Data visualization is a great way to celebrate our favorite pieces of art as well as reveal connections and ideas that were previously invisible. More importantly, it’s a fun way to connect things we love — visualizing …

数据存储加密和传输加密_将时间存储网络应用于加密预测

数据存储加密和传输加密I’m not going to string you along until the end, dear reader, and say “Didn’t achieve anything groundbreaking but thanks for reading ;)”.亲爱的读者&#xff0c;我不会一直待到最后&#xff0c;然后说&#xff1a; “没有取得任何开创性的…

熊猫分发_熊猫新手:第一部分

熊猫分发For those just starting out in data science, the Python programming language is a pre-requisite to learning data science so if you aren’t familiar with Python go make yourself familiar and then come back here to start on Pandas.对于刚接触数据科学的…

多线程 进度条 C# .net

前言  在我们应用程序开发过程中&#xff0c;经常会遇到一些问题&#xff0c;需要使用多线程技术来加以解决。本文就是通过几个示例程序给大家讲解一下多线程相关的一些主要问题。 执行长任务操作  许多种类的应用程序都需要长时间操作&#xff0c;比如&#xff1a;执行一…

window 10 多版本激活工具

window 10 通用版激活工具 云盘地址&#xff1a;https://pan.baidu.com/s/1bo3L4Kn 激活工具网站&#xff1a;http://www.tudoupe.com/win10/win10jihuo/2017/0516/6823.html 转载于:https://www.cnblogs.com/ipyanthony/p/9288007.html