我做了一个带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()); } } }
}
百度出来的代码:这代码怎么用啊
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()); } } }
}
http://topic.csdn.net/u/20110510/15/4441d944-4ce3-44ef-9bcd-7d243cc50e9a.html
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