using System;
using System.Collections.Generic;
using
using System.Configuration.Install;
using System.Data;
using System.Data.SqlClient;
namespace InstallDB
{
[RunInstaller( true )]
public partial class InstallerDB : Installer {
public InstallerDB() {
InitializeComponent();
}
private void CreateDataBase( string strSql, string DataName, string strMdf, string strLdf, string path, string strSql1) {
#region ### 附加数据库
String str;
SqlConnection myConn = new SqlConnection(strSql);
str = " EXEC sp_attach_db @dbname = ' " + DataName + " ', @filename1 = ' " + strMdf + " ',@filename2= ' " + strLdf + " ' ";
SqlCommand myCommand = new SqlCommand(str, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
#endregion #region ###新建数据库用户的登陆名和密码(登陆名:admin 密码:123)
string str2 = " exec sp_addlogin 'admin','123' " + " " + " use " + DataName + "" + " " + " exec sp_adduser 'Eric' " + " " + " exec sp_addrolemember 'db_owner','Eric' " ;
SqlCommand cmd2 = new SqlCommand(str2, myConn);
cmd2.ExecuteNonQuery();
myConn.Close();
#endregion #region ###把用户安装路径存入数据库
SqlConnection cn = new SqlConnection(strSql1);
string str3 = " insert into T_DataPath (DataPath) values(' " + path + " ') " ;
cn.Open();
SqlCommand cmd3 = new SqlCommand(str3, cn);
cmd3.ExecuteNonQuery();
cn.Close();
#endregion
}
public override void Install(System.Collections.IDictionary stateEricver) {
string server = this .Context.Parameters[ " server " ];
string uid = this .Context.Parameters[ " user " ];
string pwd = this .Context.Parameters[ " pwd " ];
string path = this .Context.Parameters[ " targetdir " ];
string strSql = " server= " + server + " ;uid= " + uid + " ; pwd= " + pwd + " ;database=master " ;
string strSql1 = " server= " + server + " ;uid= " + uid + " ; pwd= " + pwd + " ;database=Library " ;
string DataName = " Library " ;
string strMdf = path + " \\textilerails_development.MDF "; // + @"\" + "+textilerails_development.mdf+";
string strLdf = path + " \\textilerails_development.LDF "; // +@"\" + "+textilerails_development.ldf+";
base .Install(stateEricver);
this .CreateDataBase(strSql, DataName, strMdf, strLdf,path,strSql1);
// 调用上面的方法
}
}
}