有关.net打包自动安装数据库问题

moneybag 2006-01-09 02:55:48
我搜索了很多资料但都是VB.NET的asp.net打包自动安装数据库 ,
谁给个C#.NET的asp.net打包自动安装数据库的程序,谢谢!
...全文
294 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lengfeng8866 2006-03-03
  • 打赏
  • 举报
回复
/// <summary>
/// 重载安装方法
/// </summary>
/// <param name="stateSaver"></param>
public override void Install(IDictionary stateSaver)
{
base.Install (stateSaver);

// ========================== 建立数据库 =================================
try
{
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"]);

ExecuteSql(connStr,"master","if exists(select * from master.dbo.sysdatabases where [name] = '" + this.Context.Parameters["dbname"] + "') drop database " + this.Context.Parameters["dbname"]);
ExecuteSql(connStr,"master","CREATE DATABASE " + this.Context.Parameters["dbname"]);

// 调用osql执行脚本
Process sqlProcess = new Process();
sqlProcess.StartInfo.FileName = "osql.exe";
sqlProcess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -d {2} -i {3}\\TestInstall.sql", this.Context.Parameters["user"], this.Context.Parameters["pwd"], this.Context.Parameters["dbname"], this.Context.Parameters["targetdir"]);
sqlProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
sqlProcess.Start();
sqlProcess.WaitForExit(); //等待执行...
sqlProcess.Close();

// 删除SQL脚本
FileInfo fileInfo = new FileInfo(String.Format("{0}\\TestInstall.sql",this.Context.Parameters["targetdir"]));
if(fileInfo.Exists)
fileInfo.Delete();
}
catch(Exception ex)
{
throw ex;
}

// ============== 将用户的数据库配置信息写入web.config文件 ======================
try
{
FileInfo configFile = new FileInfo(this.Context.Parameters["targetdir"] + "\\web.config");
if(!configFile.Exists)
throw new InstallException("没有找到WEB应用程序的Web.config文件!");

XmlDocument xmlDoc = new XmlDocument(); // 实例化xml文档
xmlDoc.Load(configFile.FullName);

// 查找到appsettings中的节点
bool isFond = false;
foreach(XmlNode node in xmlDoc["configuration"]["appSettings"])
{
if(node.Name == "add")
if(node.Attributes.GetNamedItem("key").Value == "ConnString")
{
// 写入连接字符串
string conStr = String.Format("Persist Security Info=False;Data Source={0};Initial Catalog={1};User ID={2};Password={3};",
this.Context.Parameters["server"], this.Context.Parameters["dbname"], this.Context.Parameters["user"], this.Context.Parameters["pwd"]);
node.Attributes.GetNamedItem("value").Value = conStr;
isFond = true;
}
}

if(!isFond)
throw new InstallException("web.Config 文件没有包含connString连接字符串设置");
xmlDoc.Save(configFile.FullName);
}
catch(Exception ex2)
{
throw ex2;
}
}
lengfeng8866 2006-03-03
  • 打赏
  • 举报
回复
/// <summary>
/// 根据指定数据库连接串、数据库名和SQL语句,创建数据库
/// </summary>
/// <param name="conn">数据库连接串</param>
/// <param name="DatabaseName">数据库名</param>
/// <param name="Sql">要执行的SQL语句</param>
private void ExecuteSql(string conn, string DatabaseName, string Sql)
{
SqlConnection mySqlConnection = new SqlConnection(conn);
SqlCommand command = new SqlCommand(Sql,mySqlConnection);
command.Connection.Open();
command.Connection.ChangeDatabase(DatabaseName);
try
{
command.ExecuteNonQuery();
}
catch
{
throw new InstallException("创建数据库时,执行SQL语句时出错!");
}
finally
{
mySqlConnection.Close();
}
}
lengfeng8866 2006-03-03
  • 打赏
  • 举报
回复
自己把VB.net的改一下不就可以了吗?VB.net能做到,C#也肯定能行的。。我这里有一个现成的。。你看一下吧。。
centralplains 2006-03-03
  • 打赏
  • 举报
回复
guanzhuing
wls12342004 2006-03-03
  • 打赏
  • 举报
回复
楼上正解
giggsjw 2006-02-25
  • 打赏
  • 举报
回复
觉得cw888()办法可行
vision_n 2006-01-11
  • 打赏
  • 举报
回复
关注ing
moneybag 2006-01-11
  • 打赏
  • 举报
回复
自己顶
moneybag 2006-01-09
  • 打赏
  • 举报
回复
怎么全是VB的例子,难道C#就没有吗?郁闷!
lidong6 2006-01-09
  • 打赏
  • 举报
回复
http://www.51cto.com/html/2005/0920/3411.htm
http://oftengo.com/info/463-1.htm
moneybag 2006-01-09
  • 打赏
  • 举报
回复
谢谢各位!cw888() ,有像你所说的例子吗?
cw888 2006-01-09
  • 打赏
  • 举报
回复
自动安装数据库:可用C#单独写个程序来安装数据库,将数据库生成SQL文件,
C#安装程序执行SQL文件就生成你所需的数据库!然后C#.NET的asp.net打包
程序调用C#安装程序.
hjh1982 2006-01-09
  • 打赏
  • 举报
回复
楼主,打包数据库的程序,是独立于应用程序的另一个项目
可以在同一个解决方案中,添加这两个项目,你可以不用管他是什么的啊
为什么在乎是vb。net,还是C#呢
moneybag 2006-01-09
  • 打赏
  • 举报
回复
zmofcsdn(张三),谢谢你,但你给的网址打不开,MSDN例子在哪里呀,找不到的|
八米二康 2006-01-09
  • 打赏
  • 举报
回复
MSDN里就有例子呀,你从 System.Configuration.Install.Installer下继承一个类,写上自己的实现.Net的安装程序会调用化的方法。

演练:安装期间使用自定义操作创建数据库

ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/vsintro7/html/vxwlkwalkthroughusingcustomactiontocreatedatabaseduringinstallation.htm
moneybag 2006-01-09
  • 打赏
  • 举报
回复
高手们哪里去了?各位帮帮忙,小弟在些先谢谢了

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧