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();
}

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

c# 通过配置自动附加数据库的更多相关文章

  1. EF CodeFirst 如何通过配置自动创建数据库<当模型改变时>

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷 学无止境,精益求精    本篇为进阶篇,也是弥补自己之前没搞明白的地方,惭愧 ...

  2. Sqlserver2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法

    Sqlserver2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法 最近几天从网上找了几个asp.net的登录案例想要研究研究代码,结果在用 Sql Server2005附 ...

  3. SQLServer2005+附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法

    SQLServer2005+ 附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法 我们在用Sql SQLServer2005+附加数据库文件时弹出错误信息如下图的处理办法: 方案一: ...

  4. [经使用有效]Sqlserver2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法

    sqlserver2005附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法 最近几天从网上找了几个asp.net的登录案例想要研究研究代码,结果在用 Sql Server2005附 ...

  5. SQL2008附加数据库报错

    sql server 2008如何导入mdf,ldf文件 网上找了很多解决sql server导入其他电脑拷过来的mdf文件,多数是不全,遇到的解决方法不一样等问题,下边是找到的解决问题的最全面方法! ...

  6. Sqlserver 2012附加数据库时出错提示操作系统错误5(拒绝访问)错误5120的解决办法

    环境: Win10系统 SQLSERver 2012 情况: 使用混合登陆方式,sa账户密码正确登陆后,附加.mdf文件出现此错误. 尝试解决方法一:使用管理员运行SQLSERver2012,sa账户 ...

  7. C# 自动部署之附加数据库

    转自心存善念 原文 C# 自动部署之附加数据库 看着别人的网站能够自动安装,数据库自动附加,觉得很神奇很向往,但是始终米有去手动实践. 网上找了下资料,发现实现起来其实很简单 直接code priva ...

  8. 项目管理实践【六】自动同步数据库【Using Visual Studio with Source Control System to synchronize database automatically】

    在上一篇项目管理实践[五]自动编译和发布网站中,我们讲解了如何使用MSBuild+Robocopy+WebDeployment来自动编译和部署网站,今天,我们来看一下,如何使用MSBuild +SVN ...

  9. 转:winform 打包自动安装数据库

    vs2005 打包,并自动安装SQL数据库.创建部署项目    1.   在“文件”菜单上指向“添加项目”,然后选择“新建项目”.    2.   在“添加新项目”对话框中,选择“项目类型”窗格中的“ ...

随机推荐

  1. 南阳OJ 16 矩形嵌套

    描写叙述 有n个矩形,每个矩形能够用a,b来描写叙述,表示长和宽. 矩形X(a,b)能够嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度). ...

  2. Linux/UNIX之信号(2)

    信号(2) sigaction函数 sigaction函数的功能是检查或改动与制定信号相关联的处理动作.此函数代替了signal函数. #include <signal.h> int si ...

  3. Qrcode生成二维码支持中文,带图片,带文字

    1.下载Qrcode库源码, 下载地址:http://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library2.打开源码时, 部分类 ...

  4. SpringMVC与Mybatis框架整合遇到的坑(转)

    最近在做springmvc与mybatis的项目,遇到一些比较坑的问题.花了许多时间却发现其实解决的办法很简单.这里主要是讲我自己在整合这两个框架的时候遇到的一些问题做一个整理.希望遇到和我同样问题的 ...

  5. 假设拦截WebView的错误和OS升级到4.4后链接不能点击的问题

    android OS升级到4.4之后,有些WebView的链接我们点击无效了,以下能够解决当中的某一种情况: webviewClient的shouldOverrideUrlLoading方法必须返回f ...

  6. HDU 4454 - Stealing a Cake(三分)

    我比较快速的想到了三分,但是我是从0到2*pi区间进行三分,并且漏了一种点到边距离的情况,一直WA了好几次 后来画了下图才发现,0到2*pi区间内是有两个极值的,每个半圆存在一个极值 以下是代码 #i ...

  7. Android判断应用程序从后台回到前台

    MainActivity如下: package cc.testbackgroundtofront; import java.util.List; import android.app.Activity ...

  8. 编程算法 - 字典分词 代码(C)

    字典分词 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 给定字典, 给定一句话, 进行分词. 使用深度遍历(DFS)的方法. 使用一个參数string ...

  9. sql语句中 limi的用法

    SELECT * FROM table LIMIT [offset,] rows | rows OFFSET offset 使用查询语句时需要返回前几条或者中间的某几行数据时可以用到limit 例如 ...

  10. Android如何获得手机power_profile.xml文件

    上的能量消耗进行最近的测试,阅读文章一个月,最后,我们发现了一些新的想法,但产生的问题.那 工作无法再进行下去. 在Android手机中,对于手机中的每一个部件(cpu.led.gps.3g等等)执行 ...