我做了一个带SQl数据库的C#winform项目,可是我发布的时候搞不定。原先是access数据库发布的,我能搞定。可是数据库做成sqlserver的时候,发布的时候出现了问题。我是把数据库mdf ldf文件分离。然后放到发布程序中database文件夹下面,把2.0平台和sqlserver2005简单版 都打包到安装程序中。在安装的时候mdf和ldf自动附加到sql上。但是貌似要写脚本程序,在第一次运行安装的时候,自动执行。····什么事脚本程序了,怎么写了,怎么放到安装程序里面了,怎么让他第一次安装的时候运行啊。
百度出来的代码:这代码怎么用啊
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;using System.Diagnostics;using System.Data.SqlClient;namespace DBSetUp
{
    [RunInstaller(true)]
    public partial class install : Installer
    {
        public install()
        {
            InitializeComponent();
        }         private void CreateDataBase()        {            // 启动SQL服务, 预防装完之后服务未启动            Process p = new Process();            p.StartInfo.FileName = "cmd.exe";             p.StartInfo.UseShellExecute = false;            p.StartInfo.RedirectStandardInput = true;            p.StartInfo.RedirectStandardOutput = true;            p.StartInfo.RedirectStandardError = true;            p.StartInfo.CreateNoWindow = true;
           
             p.Start();            p.StandardInput.WriteLine("net start MSSQL$yiqi");//Rybx为客户端数据库实例名            p.StandardInput.WriteLine("exit");            p.StandardOutput.ReadToEnd();             // “server”:客户端数据库实例名, “user id”:Sa ,“password”:Sa的密码            string strSql = string.Format("server={0}; user id={1}; password={2};Database=master", @"(local)\yiqi", "sa", "haozi110");            //需要附加的数据库文件,我的设置是在项目程序的安装目录下的database文件夹下            string strMdf = this.Context.Parameters["targetdir"] + @"\database\yiqi.mdf";            string strLdf = this.Context.Parameters["targetdir"] + @"\database\yiqi_log.ldf";             string str;            SqlConnection myConn = new SqlConnection(strSql);            str = "EXEC sp_attach_db @dbname = N'yiqi', @filename1 = N'" + strMdf + "',@filename2=N'" + strLdf + "'";            SqlCommand myCommand = new SqlCommand(str, myConn);            myConn.Open();            myCommand.ExecuteNonQuery();            myConn.Close();        }         protected override void OnAfterInstall(System.Collections.IDictionary savedState)        {            base.OnAfterInstall(savedState);            try            {                CreateDataBase();            }            catch (Exception ex)            {                throw new Exception(ex.ToString());            }        }    }
}

解决方案 »

  1.   

    制作vs2008发布安装程序
    http://topic.csdn.net/u/20110510/15/4441d944-4ce3-44ef-9bcd-7d243cc50e9a.html
      

  2.   

    还是有些差距的 ,那个是web的,我这个是单机版的winform应用程序。不需要远程数据库,用自己的数据库
      

  3.   

    给点实质性的意见呗。我想用的就是单机版的数据库,而且还是sql的,不是access,不存在远程访问。怎么发布······愁死我了
      

  4.   

    敢不敢来个人 给俺好好说说 带sql的winform项目发布,怎么一次性安装,详细步骤
      

  5.   

    一般 做C#程序,都吧数据库的连接串放在配置文件里
    1、放在 App.config<configuration>
       <appSettings>
           <ConnectionString> 连接串</ConnectionString> 
       </appSettings>
    </configuration>
    读取链接串
         System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString")
    链接串:Provider=SQLOLEDB;Persist Security Info=False;User ID=sa;Password=saInitial Catalog=数据库名;Data Source=172.16.7.68;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096
      

  6.   

    麻烦大家回答的时候看清楚我的要求,,,,···困难依然在·一星期了···给点力啊··单机版c#带sql数据库的winform项目发布·怎么实现自动安装sqlserver和sql数据库附加··连接字符串做好了,自动安装sql怎么安装,让我写脚本,也要给说点脚本到底怎么写啊···愁死我了 死的心都有啊
      

  7.   

    http://www.cnblogs.com/andyran/archive/2008/11/28/1343061.html  根据这个做也不行啊 到底哪的问题啊