日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

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

發(fā)布時間:2023/11/30 C# 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#生成安装文件后自动附加数据库的思路跟算法 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

namespace?AdminZJC.DataBaseControl
{
?///?<summary>
?///?數(shù)據(jù)庫操作控制類
?///?</summary>
?public?class?DataBaseControl
?{
?///?<summary>
?///?數(shù)據(jù)庫連接字符串
?///?</summary>
?public?string?ConnectionString;

?///?<summary>
?///?SQL操作語句/存儲過程
?///?</summary>
?public?string?StrSQL;

?///?<summary>
?///?實例化一個數(shù)據(jù)庫連接對象
?///?</summary>
?private?SqlConnection?Conn;

?///?<summary>
?///?實例化一個新的數(shù)據(jù)庫操作對象Comm
?///?</summary>
?private?SqlCommand?Comm;

?///?<summary>
?///?要操作的數(shù)據(jù)庫名稱
?///?</summary>
?public?string?DataBaseName;

?///?<summary>
?///?數(shù)據(jù)庫文件完整地址
?///?</summary>
?public?string?DataBase_MDF;

?///?<summary>
?///?數(shù)據(jù)庫日志文件完整地址
?///?</summary>
?public?string?DataBase_LDF;

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

?///?<summary>
?///?備份文件路徑
?///?</summary>
?public?string?DataBaseOfBackupPath;

?///?<summary>
?///?執(zhí)行創(chuàng)建/修改數(shù)據(jù)庫和表的操作
?///?</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("數(shù)據(jù)庫操作成功!",?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
?}
?catch?(Exception?ex)
?{
?MessageBox.Show(ex.Message,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
?}
?finally
?{
?Conn.Close();
?}
?}

?///?<summary>
?///?附加數(shù)據(jù)庫
?///?</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("附加數(shù)據(jù)庫成功",?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
?}
?catch?(Exception?ex)
?{
?MessageBox.Show(ex.Message,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
?}
?finally
?{
?Conn.Close();
?}
?}

?///?<summary>
?///?分離數(shù)據(jù)庫
?///?</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("分離數(shù)據(jù)庫成功",?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
?}
?catch?(Exception?ex)
?{
?MessageBox.Show(ex.Message,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
?}
?finally
?{
?Conn.Close();
?}
?}

?///?<summary>
?///?備份數(shù)據(jù)庫
?///?</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("備份數(shù)據(jù)庫成功",?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
?}
?catch?(Exception?ex)
?{
?MessageBox.Show(ex.Message,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
?}
?finally
?{
?Conn.Close();
?}
?}

?///?<summary>
?///?還原數(shù)據(jù)庫
?///?</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("還原數(shù)據(jù)庫成功",?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
?}
?catch?(Exception?ex)
?{
?MessageBox.Show(ex.Message,?"信息提示",?MessageBoxButtons.OK,?MessageBoxIcon.Information);
?}
?finally
?{
?Conn.Close();
?}
?}
?}
}

/*
?///調(diào)用事例:
?
?   還原數(shù)據(jù)庫
?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();
?}
?
    附加數(shù)據(jù)庫
?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();
?}
?
    備份數(shù)據(jù)庫
?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();
?}
?
    分離數(shù)據(jù)庫
?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();
?}

總結(jié)

以上是生活随笔為你收集整理的C#生成安装文件后自动附加数据库的思路跟算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。