• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

数据库安装文件制作(C#)

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

最近在做项目时,将软件打包给技术部门用,里面因为用到数据库,所以最好是在安装文件执行完毕之后,自动还原数据库(sql)

想到了2个方法

  1. 做成安装包(自定义安装,用数据库备份文件还原)
  2. 利用sql脚本,生成,但osql还是有限制

这2个方法都是可行的,第二个方法会出现多个文件,第一个方法只有一个安装包;

显然一个方法方便些;

实现思路:

  1. 找到数据库备份文件
  2. 安装时还原数据库
  3. 卸载时删除数据库

重写 public override void Install(IDictionary stateSaver)和public override void Uninstall(IDictionary savedState)方法

自己编写好代码,编译运行,安装,却出现1001错误,找不到数据库备份文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration.Install;
using System.Collections;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Diagnostics;

namespace SmartHomeSetup
{
    [RunInstaller(true)]
    public class Form1 : System.Configuration.Install.Installer
    {
        private System.ComponentModel.Container components = null;
        public Form1()
        {
            InitializeComponent();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

//返回路径       

private static string PrepareSQL(string name)
        {
            string tempfile = "";
            try
            {
                Assembly asm = Assembly.GetExecutingAssembly();
                Stream fs = asm.GetManifestResourceStream(asm.GetName().Name + "." + name);
                BinaryReader br = new BinaryReader(fs);

                tempfile = Path.GetTempFileName();
                FileStream fsWrite = new FileStream(tempfile, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fsWrite);
                byte[] data = br.ReadBytes(1024);
                while (data != null && data.Length > 0)
                {
                    bw.Write(data);
                    data = br.ReadBytes(1024);
                }

                br.Close();
                fs.Close();
                bw.Close();
                fsWrite.Close();
            }
            catch (System.Exception e)
            {
                throw e;
            }

            return tempfile;
        }

        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
        }

        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);
            //创建数据库的连接字符串
            string DbConnection =
                "Server=" + Context.Parameters["databaseServer"] + ";" +
                "Trusted_Connection=true;" +
                "U>

重新编译,安装OK


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#winform基础1、Timer不起作用2、设置图片透明发布时间:2022-07-10
下一篇:
C#完美读取CSV发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap