程序在附件里,具体的功能是部署WINFORM安装包,要求在安装的时候能够使用SQL SERVER的控制台工具osql.exe。通过-E参数进行信任登陆,通过-i参数让osql.exe去执行一个数据库脚本文件。具体代码:
      //得到当前正在运行的程序集实例
          System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            
            //得到当前程序集的路径
           string Path = assembly.Location;
            //去掉本程序集的文件名
            Path = Path.Replace("installdb.dll","");
            //加上脚本文件的文件名
            string SqlPath = Path + "DemoSql.sql";
            
            //创建数据库安装进程
           System.Diagnostics.Process P = new Process();
            //设定进程参数
            P.StartInfo = new ProcessStartInfo("osql.exe"," -E -i "+SqlPath);
           
            //打开进程
            P.Start();
但是通过安装包安装完成之后,在SQL企业管理器并没有生成新的数据库,找了很长时间都不知道是何原因,请高手帮我看一下,并说明错在哪里(DemoSql脚本应该具体放在哪个目录?)?谢谢了。100分送上。

解决方案 »

  1.   

    我的Web项目打包时这么实现的你看看吧 
    你的安装的文件夹名称最好不要有空格,否则osql.exe可能会执行失败string connStr = string.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", this.Context.Parameters["server"], this.Context.Parameters["user"], this.Context.Parameters["pwd"]);ExecuteSql(connStr, "master", "CREATE DATABASE " + "数据库名称 ");  
    Process sqlProcess = new Process();
    sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
    sqlProcess.StartInfo.FileName = "osql.exe";
    sqlProcess.StartInfo.Arguments = string.Format("-S {0} -U {1} -P {2} -d {3} -i {4}", "server", "user","pwd", "dbname", "安装文件的路径\*.sql");
    sqlProcess.Start();
    sqlProcess.WaitForExit();
    sqlProcess.Close();
      

  2.   


    private void ExecuteSql(string connStr, string DatabaseName, string Sql)
    {
        SqlConnection conn = new SqlConnection(connStr);
        SqlCommand cmd = new SqlCommand(Sql, conn);
        conn.Open();
        conn.ChangeDatabase(DatabaseName);
        try
        {
            cmd.ExecuteNonQuery();
        }
        finally
        {
            conn.Close();
        }
    }
      

  3.   

    to ldb5736030,有具体的安装包DEMO吗?有请发给我好吗?谢谢。
    EMAIL:[email protected]
      

  4.   

    我的脚本代码这样写就可以通过,并且在企业管理器能够看到建立的表格。
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[BookType_table]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    drop table [dbo].[BookType_table]
    GO
    CREATE TABLE [dbo].[BookType_table] (
    [BookType] [varchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL 
    ) ON [PRIMARY]
    GO
    但是在脚本面前添加这样的代码就提示什么“未能在sysdatabases中找到数据库'dbbook'所对应的条目,没有找到具有该名称的条目,请确保正确输入了名称”,为什么这样?我添加的脚本也没有错呀。哪个高手指导一下。谢谢。
    use master
    GO
    if exists (select * from sysdatabases where name='dbbook')
    drop database dbbook
    GO  
    create database dbbook
    GO  
    use dbbook
    GO