在Visual 2008中制作Installer时如何附加数据库 [英] how i Attach DB while making Installer in Visual 2008

查看:57
本文介绍了在Visual 2008中制作Installer时如何附加数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何在使用C语言在Visual 2008中制作Installer的同时附加数据库?

how i Attach DB while making Installer in Visual 2008 using C sharp

推荐答案

我更喜欢在SQL脚本中嵌入并执行SQL脚本,即我的Database.sql文件.自行设置.

因此,当您创建Windows应用程序部署项目时,右键单击解决方案中的启动项目(而不是安装项目),添加新项Installer.cs.

将database.sql文件添加到解决方案>您的启动模块.

右键单击数据库.属性>建立动作>嵌入式资源

接下来,覆盖Install方法

公共重写void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);

.

.

.

从已安装的程序集中读取SQL文件

{

//以字符串形式读取SQL文件
System.Reflection.Assembly Asm = System.Reflection.Assembly.GetExecutingAssembly();
流scriptStream = Asm.GetManifestResourceStream(Asm.GetName().Name +." +"database.sql");
StreamReader reader =新的StreamReader(scriptStream);

正则表达式regex = new Regex("^ GO",RegexOptions.IgnoreCase | RegexOptions.Multiline);
返回regex.Split(reader.ReadToEnd());

}

以编程方式执行SQL脚本的主要问题是SQL Server无法识别"GO"关键字. GO是用于批量执行命令的SQL Server Management Studio定界符,而本身不是SQL命令.

因此,在读取文件后,我们将删除"GO".

现在我们有了一个string [],每个SQL命令都分别存储在一个数组中.我们将逐一阅读它们,然后分别执行.

SqlConnection sqlConnection =新的SqlConnection();
sqlConnection.ConnectionString = connectionString;
string [] sSQLCommands = GetSqlCommands("Database.sql");
SqlCommand sqlCommand =新的SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.Connection.Open();
foreach(sSQLCommands中的字符串sSQLCommand)
{
if(sSQLCommand.Length> 0)
{
sqlCommand.CommandText = sSQLCommand;
sqlCommand.CommandType = CommandType.Text;
试试
{
sqlCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
扔前;
}
}
}
sqlCommand.Connection.Close();
}
catch(SqlException ex)
{
扔前;
}


如果需要回滚异常,则抛出安装异常,该异常将自动回滚安装程序.

抛出新的InstallException(缺少数据库脚本文件");

注意:如果该解决方案适合您,请标记为答案并投票赞成,以便其他人可以节省搜索时间.
I prefer to embed and execute the SQL script i.e. my Database.sql file in the setup itself.

So when you are creating a Windows Application Deployment project, right click on the startup project in your solution ( NOT the setup project), add the new item, Installer.cs.

Add the database.sql file to the solution> your startup module.

Right-click the database.sql> Properties> Build Action > Embedded Resource

Nextly, override the Install method

public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);

.

.

.

Read the SQL file from the installed assembly

{

// Read the SQL file as a string
System.Reflection.Assembly Asm = System.Reflection.Assembly.GetExecutingAssembly();
Stream scriptStream= Asm.GetManifestResourceStream(Asm.GetName().Name + "." + "database.sql");
StreamReader reader = new StreamReader(scriptStream);

Regex regex = new Regex("^GO",RegexOptions.IgnoreCase | RegexOptions.Multiline);
return regex.Split(reader.ReadToEnd());

}

The main problem with executing the SQL script programatically is that , the ‘GO’ keyword is not recognised by the SQL Server. The GO is the SQL Server Management studio delimiter to execute the commands in batches and is not a SQL Command in itself.

So after reading the file , we will remove the ‘GO’s.

Now we have a string[] with each of the SQL command stored seperately in a array. We will read each of them , one by one , and execute them seperately.

SqlConnection sqlConnection=new SqlConnection();
sqlConnection.ConnectionString=connectionString;
string[] sSQLCommands= GetSqlCommands("Database.sql");
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.Connection = sqlConnection;
sqlCommand.Connection.Open();
foreach(string sSQLCommand in sSQLCommands)
{
if(sSQLCommand.Length>0)
{
sqlCommand.CommandText = sSQLCommand;
sqlCommand.CommandType = CommandType.Text;
try
{
sqlCommand.ExecuteNonQuery();
}
catch(SqlException ex)
{
throw ex;
}
}
}
sqlCommand.Connection.Close();
}
catch( SqlException ex)
{
throw ex;
}


In case you need to rollback on a exception throw the install exception which will automatically rollback the setup.

throw new InstallException("Missing database script file");

NOTE : If the solution works for you please mark as answer and vote it up so that other people can save timing in searching.


这篇关于在Visual 2008中制作Installer时如何附加数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆