首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

请教:在用vs打包的时候,的安装类文件里面 对数据库的操作

2012-03-11 
请问:在用vs打包的时候,的安装类文件里面 对数据库的操作?在Installer1.csclassInstaller1:Install在里面

请问:在用vs打包的时候,的安装类文件里面 对数据库的操作?
在Installer1.cs    
class   Installer1:Install

在里面写代码,实现数据库的还原
有个数据库的还原文件   db1.bak

因为是客户端安装   所以是不知道   帐号密码的?

[解决办法]
因为是客户端安装 所以是不知道 帐号密码的?
------------------------------
这个你应该在安装的时候弹出窗口让用户自己输入帐号和密码,或者采用windows验证.记的petShop就是这样的。


在里面写代码,实现数据库的还原
有个数据库的还原文件 db1.bak
------------------------
怎么用代码还原数据库就不知道了,没做过,不好意思,~_~
[解决办法]
给个例子给你吧:

public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand() ;
string err = string.Empty;
try
{
/*文件改名*/
//delete files
try
{
FileInfo f1 = new FileInfo(this.Context.Parameters[ "TargetDir "]+ "DataBase\\xxxxxxxxx.mdf ");
if(f1.Exists)
f1.Delete();
FileInfo f2 = new FileInfo(this.Context.Parameters[ "TargetDir "]+ "DataBase\\xxxxxxxxxxxxx_log.LDF ");
if(f2.Exists)
f2.Delete();
}
catch
{}
//delete files
FileInfo fileInfo = new FileInfo(this.Context.Parameters[ "TargetDir "]+ "DataBase\\xxxxxxxxxxxx.html ");
fileInfo.CopyTo(this.Context.Parameters[ "TargetDir "]+ "DataBase\\xxxxxxxxx.mdf ",false);
fileInfo.Delete();
FileInfo fileInfo2 = new FileInfo(this.Context.Parameters[ "TargetDir "]+ "DataBase\\xxxxxxxx_log.html ");
fileInfo2.CopyTo(this.Context.Parameters[ "TargetDir "]+ "DataBase\\xxxxxxxxxxxx_log.LDF ",false);
fileInfo2.Delete();

#region *** 附加数据库 ***
string connstr = String.Format( "server={0};database=master;uid={1};pwd={2} ",this.Context.Parameters[ "Server "],this.Context.Parameters[ "DBUser "],this.Context.Parameters[ "DBPASS "]);
// '根据输入的数据库名称建立数据库
con = new SqlConnection(connstr);
con.Open();
cmd = new SqlCommand( "sp_attach_db ",con);
cmd.CommandType = CommandType.StoredProcedure ;
SqlParameter dbname = new SqlParameter( "@dbname ",SqlDbType.NVarChar,20);
dbname.Value = "xxxxxxxxxxx " ;
cmd.Parameters.Add(dbname);
SqlParameter filename1 = new SqlParameter( "@filename1 ",SqlDbType.NVarChar,50);
filename1.Value = this.Context.Parameters[ "TargetDir "]+ "DataBase\\xxxxxxxxx.mdf " ;
cmd.Parameters.Add(filename1);
SqlParameter filename2 = new SqlParameter( "@filename2 ",SqlDbType.NVarChar,50);
filename2.Value = this.Context.Parameters[ "TargetDir "]+ "DataBase\\xxxxxxxxxxx_log.LDF " ;
cmd.Parameters.Add(filename2);
cmd.ExecuteNonQuery();
#endregion

#region *** 修改WebConfig ***
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(this.Context.Parameters[ "TARGETDIR "]+ "\\Web.config ");
xmlDocument.DocumentElement.FirstChild.ChildNodes[0].Attributes[ "value "].Value = this.Context.Parameters[ "Server "];
//xmlDocument.DocumentElement.FirstChild.ChildNodes[1].Attributes[ "value "].Value = this.Context.Parameters[ "dbname "];
xmlDocument.DocumentElement.FirstChild.ChildNodes[2].Attributes[ "value "].Value = this.Context.Parameters[ "DBUser "];
xmlDocument.DocumentElement.FirstChild.ChildNodes[3].Attributes[ "value "].Value = this.Context.Parameters[ "DBPASS "];


xmlDocument.Save(this.Context.Parameters[ "TARGETDIR "]+ "\\Web.config ");
#endregion

/*创建快捷方式
* 桌面和启动*/
WshShell shell = new WshShellClass();
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)+ "\\ "+ "xxxxxxxxxxxxxxxxx.lnk ");
string fullpath = this.Context.Parameters[ "TargetDir "].Trim();
fullpath = fullpath.Substring(0,fullpath.Length-1);
fullpath = fullpath.Substring(fullpath.LastIndexOf(@ "\ ")+1);
shortcut.TargetPath = "http://localhost/ "+fullpath+ "/index.aspx ";
shortcut.IconLocation = this.Context.Parameters[ "TargetDir "]+@ "\manage\Tray.ico ";
shortcut.Description = "xxxxxxxxxxxxxxxxxx ";
shortcut.Save();
IWshShortcut shortcut2 = (IWshShortcut)shell.CreateShortcut(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Programs)+ "\\启动\\ "+ "xxxxxxxxxxx.lnk ");
shortcut2.TargetPath = this.Context.Parameters[ "TargetDir "]+@ "\manage\managecenter.exe ";
shortcut2.WorkingDirectory = this.Context.Parameters[ "TargetDir "]+ "manage " ;
shortcut2.Description = "xxxxxxxxx ";
shortcut2.IconLocation = this.Context.Parameters[ "TargetDir "]+@ "\manage\Tray.ico ";
shortcut2.WindowStyle = 1 ;
shortcut2.Save();
IWshShortcut shortcut3 = (IWshShortcut)shell.CreateShortcut(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Favorites)+ "\\ "+ "xxxxxxxxxx.lnk ");
shortcut3.TargetPath = "http://localhost/xxxxxx/ ";
shortcut3.IconLocation = this.Context.Parameters[ "TargetDir "]+@ "\manage\Tray.ico ";
shortcut3.Description = "xxxxxxxxxx ";
shortcut3.Save();

}
catch(Exception ex)
{
throw new Exception( "创建数据库时出错: "+ex.Message);
}
finally
{
con.Close();
con.Dispose();
cmd.Dispose();
}
}
[解决办法]
jf

热点排行