有关.net打包自动安装数据库问题 我搜索了很多资料但都是VB.NET的asp.net打包自动安装数据库 ,谁给个C#.NET的asp.net打包自动安装数据库的程序,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 MSDN里就有例子呀,你从 System.Configuration.Install.Installer下继承一个类,写上自己的实现.Net的安装程序会调用化的方法。演练:安装期间使用自定义操作创建数据库ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/vsintro7/html/vxwlkwalkthroughusingcustomactiontocreatedatabaseduringinstallation.htm zmofcsdn(张三),谢谢你,但你给的网址打不开,MSDN例子在哪里呀,找不到的| 楼主,打包数据库的程序,是独立于应用程序的另一个项目可以在同一个解决方案中,添加这两个项目,你可以不用管他是什么的啊为什么在乎是vb。net,还是C#呢 自动安装数据库:可用C#单独写个程序来安装数据库,将数据库生成SQL文件,C#安装程序执行SQL文件就生成你所需的数据库!然后C#.NET的asp.net打包程序调用C#安装程序. http://www.51cto.com/html/2005/0920/3411.htmhttp://oftengo.com/info/463-1.htm 怎么全是VB的例子,难道C#就没有吗?郁闷! 自己把VB.net的改一下不就可以了吗?VB.net能做到,C#也肯定能行的。。我这里有一个现成的。。你看一下吧。。 /// <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(); } } /// <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; } } 如何用C#控制LED显示屏? AxMicrosoft.Vbe.Interop.Forms 是什么 求高手!!! C# 读取pop3邮件 如何根据IP得到字节序 ? 网络的或者主机的都可以 谢谢 问一个简单问题...........保存文件对话框 winform绑定数据的问题 如何生存类似于网页另存为的文件和文件夹 关于即时通信的问题 请教如何连接远程数据库SQLSERVER!!winform!高分求解!! 关于BITMAPFILEHEADER结构大小的问题? 请问.net有自带的加密吗?或者有什么第三方的加密工具 那个大哥帮我解释几行用ildasm.exe反编译的代码????
可以在同一个解决方案中,添加这两个项目,你可以不用管他是什么的啊
为什么在乎是vb。net,还是C#呢
C#安装程序执行SQL文件就生成你所需的数据库!然后C#.NET的asp.net打包
程序调用C#安装程序.
http://oftengo.com/info/463-1.htm
/// 根据指定数据库连接串、数据库名和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();
}
}
/// 重载安装方法
/// </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;
}
}