1、添加一个新项目->选择类库模板->命名为DBCustomAction
2、单击项目右键->添加新项->选择安装程序类(命名为DBCustomAction.cs)
3、在服务器资源管理器中添加->连接到数据库->指定用户密码(选择允许保存密码)->数据库选择master
4、切换到DBCustomAction.cs的视图状态->将服务器资源管理器数据库连接中的master.dbo拖动到designer中
5、添加一个新项sql.txt(注意要使用小写),输入下列sql代码
CREATE TABLE [dbo].[MK_Employees] (
[Name] [char] (30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[Rsvp] [int] NULL ,
[Requests] [nvarchar] (4000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY];
ALTER TABLE [dbo].[MK_Employees] WITH NOCHECK ADD
CONSTRAINT [PK_MK_Employees] PRIMARY KEY CLUSTERED
(
[Name]
) ON [PRIMARY];
(P.S:也可以直接用SqlServer导出)
6、在sql.txt的右键属性中->生成操作->嵌入的资源
7、将DBCustomAction.cs切换到代码视图,添加下列代码
private string GetSql(string Name)
{
try
{
Assembly Asm = Assembly.GetExecutingAssembly();
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "."+Name);
StreamReader reader = new StreamReader(strm);
return reader.ReadToEnd();
}
catch (Exception ex)
{
Console.Write("In GetSql:"+ex.Message);
throw ex;
}
}
private void ExecuteSql(string DataBaseName,string Sql)
{
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConnection1);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName)
{
try
{
ExecuteSql("master","CREATE DATABASE "+ strDBName);
ExecuteSql(strDBName,GetSql("sql.txt"));
}
catch(Exception ex)
{
Console.Write("In exception handler :"+ex.Message);
}
}
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
AddDBTable(this.Context.Parameters["dbname"]);
}
8、再添加一个新项目,(选择添加到解决方案中)->项目类型为安装项目->命名为DBCustomAction Installer
9、选择应用程序文件夹->添加->项目输出->主输出
10、在方案资源管理器中->右键安装项目(DBCustomAction Installer)->视图->用户界面
11、选中启动结点->添加对话框->文本A
12、选动文本框A->右键->上移一直到最顶端
13、选择文本框A属性->修改BannerText,(Specify Database Name)
14、修改BodyText(This dialog allows you to specify the name of the database to be created on the database server. )
15、修改EditLabel1(Name of DB),修改Edit1Porperty(CUSTOMTEXTA1),将其他Edit2,3,4的Edit(2,3,4)Visible属性设为false;
16、在方案资源管理器中->右键安装项目(DBCustomAction Installer)->视图->自定义操作
17、选中安装结点->添加->双击应用程序文件夹->主输出来自DBCustomAction(活动)->右键属性->CustomActiveData属性修改为/dbname=[CUSTOMTEXTA1]
18、编译生成,OK!
我是通过以上方法打包数据的,问题是如何中sql.txt文件中写创建存储过程的语句。
(CREATE PROCEDURE sp_Contract_EquipmentOneDelete
@ContractID varchar(10),
@EquipmentID int
AS
begin tran
update t_Purchase_Contract_Equipment set IsDelete = 1
where ContractID = @ContractID and EquipmentID = @EquipmentID
if @@error!=0
begin
rollback
return 0
end
else
begin
commit
return 1
end
)
谢谢!
5 个解决方案
#1
难道没有人遇到过这个问题吗?
#2
up!
#3
private void ExecuteSql(string DataBaseName,string Sql,SqlParameter[] prams)
{
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConnection1);
Command.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
Command.Parameters.Add(parameter);
}
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
{
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConnection1);
Command.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
Command.Parameters.Add(parameter);
}
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
#4
TO;回复人: goody9807() ( )
能否给出完整代码,谢谢!急啊
能否给出完整代码,谢谢!急啊
#5
我没有完整代码 只是告诉你一个思路 希望能启发你一下
#1
难道没有人遇到过这个问题吗?
#2
up!
#3
private void ExecuteSql(string DataBaseName,string Sql,SqlParameter[] prams)
{
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConnection1);
Command.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
Command.Parameters.Add(parameter);
}
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
{
System.Data.SqlClient.SqlCommand Command = new System.Data.SqlClient.SqlCommand(Sql,sqlConnection1);
Command.CommandType = CommandType.StoredProcedure;
if (prams != null)
{
foreach (SqlParameter parameter in prams)
Command.Parameters.Add(parameter);
}
Command.Connection.Open();
Command.Connection.ChangeDatabase(DataBaseName);
try
{
Command.ExecuteNonQuery();
}
finally
{
Command.Connection.Close();
}
}
#4
TO;回复人: goody9807() ( )
能否给出完整代码,谢谢!急啊
能否给出完整代码,谢谢!急啊
#5
我没有完整代码 只是告诉你一个思路 希望能启发你一下