我是按照网上的一篇“一次WEB安装包开发过程”来操作的,但是最后安装时,提示我的数据库安装类没有实例。
类如下:using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Data.SqlClient;
namespace ClassLibrary1
{
[RunInstallerAttribute(true)] public partial class Installer1 : Installer
{
public Installer1()
{
InitializeComponent();
Creat();
} private void Creat()
{
string connStr = string.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", this.Context.Parameters["server"], this.Context.Parameters["user"], this.Context.Parameters["pwd"]); string strSql = "EXEC sp_attach_db @dbname = N'" + this.Context.Parameters["dbname"] + "',"
+ "@filename1 = N'" + this.Context.Parameters["targetdir"] + "App_Data\\Database1.mdf',"
+ "@filename2 = N'" + this.Context.Parameters["targetdir"] + "App_Data\\Database1_log.ldf'";
ExecuteSql(connStr, "master", strSql);
}
private void ExecuteSql(string connStr, string DatabaseName, string Sql)
{
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(Sql, conn); conn.Open();
conn.ChangeDatabase(DatabaseName);
try
{
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
}
类如下:using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.Data.SqlClient;
namespace ClassLibrary1
{
[RunInstallerAttribute(true)] public partial class Installer1 : Installer
{
public Installer1()
{
InitializeComponent();
Creat();
} private void Creat()
{
string connStr = string.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", this.Context.Parameters["server"], this.Context.Parameters["user"], this.Context.Parameters["pwd"]); string strSql = "EXEC sp_attach_db @dbname = N'" + this.Context.Parameters["dbname"] + "',"
+ "@filename1 = N'" + this.Context.Parameters["targetdir"] + "App_Data\\Database1.mdf',"
+ "@filename2 = N'" + this.Context.Parameters["targetdir"] + "App_Data\\Database1_log.ldf'";
ExecuteSql(connStr, "master", strSql);
}
private void ExecuteSql(string connStr, string DatabaseName, string Sql)
{
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(Sql, conn); conn.Open();
conn.ChangeDatabase(DatabaseName);
try
{
cmd.ExecuteNonQuery();
}
finally
{
conn.Close();
}
}
}
}
解决方案 »
- winfrom程序在win7系统下禁止aero晃动
- 如何在事件触发是选中ComboBox
- C#.NET如何设置启动窗体 [散分]
- UserControl_EvaModels.Label1”不可访问,因为它受保护级别限制
- 有关异常的这道题目,请大家指点?
- 【菜鸟请教】二数相除,小数部分如何只取二位???并将它以百分比的方式显示???
- 100分求利用API函数BeginPath等方法画一个由两点直线和圆弧组成的多边形
- 请问在哪里可以下载C#的编程软件。
- c# 通过循环怎么样才能重复执行应用程序
- 设计器控件打不开Image属性,提示“已添加了具有相同键的项”
- 这段子程序为什么有警告呢(新手请教)
- datatable传值到datagridview,不会换行!
我是在安装程序里继承了Installer
类。
应该是自动生成DLL的吧?
public void AttachDB()
{
using(SqlConnection conn = new SqlConnection(connectionString))
{ conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
comm.CommandText = "sp_attach_db";
comm.Parameters.Add(new SqlParameter(@"dbname", SqlDbType.NVarChar));
comm.Parameters[@"dbname"].Value = dataBaseName;
comm.Parameters.Add(new SqlParameter(@"filename1", SqlDbType.NVarChar));
comm.Parameters[@"filename1"].Value = dataBase_MDF;
comm.Parameters.Add(new SqlParameter(@"filename2", SqlDbType.NVarChar));
comm.Parameters[@"filename2"].Value = dataBase_LDF; comm.CommandType = CommandType.StoredProcedure;
comm.ExecuteNonQuery();
conn.Close();
}
}
public Installer1()
{
InitializeComponent();
Creat();
}
Creat()函数不应该在这儿调用。应该在下面的函数中调用:
public override void Install(IDictionary savedState)
{
// base.Install(savedState);
Creat();
}谢谢各位了。