#region参数

publicstaticstring_serverName {get;set; }

publicstaticstring_dbName {get;set; }

publicstaticstring_userName {get;set; }

publicstaticstring_password {get;set; }

privatestring_setupType {get;set; }

privatestring_targetDir {get;set; }

//////资源中创建表结构及数据的文件名///privateconststring_StructureAndDataFileName ="CreateStructureData";

#endregion

publicoverridevoidInstall(IDictionary stateSaver)

{

base.Install(stateSaver);

//数据库配置 界面frmDb dbFrom =newfrmDb();

DialogResult DialogResult = dbFrom.ShowDialog();

if(DialogResult != DialogResult.OK)

{

thrownewInstallException("用户取消安装!");

}

SqlConnection connection =null;

connection = TestConnection(_serverName,"master", _userName, _password);

//创建数据库intresult =this.CreateDataBase(connection);

if(result >0)

{

CloseConnection(connection);

//使用创建的数据库connection = TestConnection(_serverName, _dbName, _userName, _password);

CreateStructureAndData(connection);

}

//创建表及增加数据CreateStructureAndData(connection);

//为空是表示有错误if(connection !=null)

{

ModifyConfig();

}

//关闭数据库CloseConnection(connection);

}

//////关闭数据库//////privatevoidCloseConnection(SqlConnection connection)

{

if(connection !=null)

{

//关闭数据库if(connection.State != System.Data.ConnectionState.Closed)

{

connection.Close();

connection.Dispose();

}

}

}

//////测试连接///////////////privateSqlConnection TestConnection(stringserverName,stringdbName,stringuserName,stringpassword)

{

stringconnectionString = GetConnectionString(serverName, dbName, userName, password);

SqlConnection connection =newSqlConnection(connectionString);

try

{

if(connection.State != ConnectionState.Open)

{

connection.Open();

}

returnconnection;

}

catch

{

CloseConnection(connection);

thrownewInstallException("安装失败!\n数据库配置有误,请正确配置信息!");

}

}

//////得到连接字符串//////////////////privatestringGetConnectionString(stringserverName,stringdbName,stringuserName,stringpassword)

{

stringconnectionString ="Data Source={0};Initial Catalog={1};User ID={2};Password={3}";

connectionString =string.Format(connectionString, serverName, dbName, userName, password);

returnconnectionString;

}

//////创建数据库/////////////////////publicintCreateDataBase(SqlConnection connection)

{

intresult = -1;

connection.ChangeDatabase("master");

stringcreateDBSql =@"if Exists(select 1 from sysdatabases where [name]=N'{0}')begindrop database {0}endGOCREATE DATABASE {0}";

createDBSql =string.Format(createDBSql, _dbName);

//因为有Go在SQLCommand中不认识,所以以Go为分隔符取sql语句char[] split =newchar[] {'G','O'};

string[] sqlList = createDBSql.Split(split);

SqlCommand command =null;

try

{

command = connection.CreateCommand();

command.CommandType = System.Data.CommandType.Text;

foreach(stringsqlIteminsqlList)

{

if(sqlItem.Length >2)

{

command.CommandText = sqlItem;

result = command.ExecuteNonQuery();

}

}

returnresult;

}

catch

{

CloseConnection(connection);

command.Dispose();

thrownewInstallException("安装失败!\n数据库配置不正确!");

}

}

//////分隔SQL语句/////////privatestring[] splitSql(stringsql)

{

Regex regex =newRegex("^GO", RegexOptions.IgnoreCase | RegexOptions.Multiline);

string[] sqlList = regex.Split(sql.ToUpper());

returnsqlList;

}

//////创建表结构及数据//////publicvoidCreateStructureAndData(SqlConnection connection)

{

StringBuilder builder =newStringBuilder();

SqlCommand command =null;

//错误标志boolisHaveError =false;

try

{

ResourceManager manager =newResourceManager(typeof(YXSchoolSetupService.Properties.Resources));

if(manager !=null)

{

connection.ChangeDatabase(_dbName);

command = connection.CreateCommand();

command.CommandType = CommandType.Text;

builder.Append(manager.GetString(_StructureAndDataFileName));

string[] sqlList = splitSql(builder.ToString());

foreach(stringsqlIteminsqlList)

{

if(sqlItem.Length >2)

{

command.CommandText = sqlItem;

command.ExecuteNonQuery();

}

}

}

else

{

isHaveError =true;

}

if(true== isHaveError)

{

CloseConnection(connection);

command.Dispose();

thrownewInstallException("数据库配置失败!\n请与开发人员联系!");

}

}

catch

{

CloseConnection(connection);

command.Dispose();

thrownewInstallException("数据库配置失败!\n请与开发人员联系!");

}

}

#region修改web.config的连接数据库的字符串

publicvoidModifyConfig()

{

System.IO.FileInfo FileInfo =newSystem.IO.FileInfo(_targetDir +"YXData.yixian");

if(!FileInfo.Exists)//不存在web.config文件{

thrownewInstallException("没有找到文统配置文件!");

}

System.Xml.XmlDocument xmlDocument =newSystem.Xml.XmlDocument();

xmlDocument.Load(FileInfo.FullName);

try

{

XmlElement element = xmlDocument.DocumentElement;

if(element !=null)

{

foreach(XmlNode nodeinelement)

{

switch(node.Name)

{

case"dbserver":

node.InnerText = _serverName;

break;

case"dbname":

node.InnerText = _dbName;

break;

case"dbpassword":

node.InnerText = _password;

break;

case"dbuser":

node.InnerText = _userName;

break;

default:

break;

}

}

}

xmlDocument.Save(FileInfo.FullName);

}

catch

{

thrownewInstallException("修改web.config配置文件失败!");

}

}

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐