一.創(chuàng)建部署.NET打包項(xiàng)目
1. 在“文件”菜單上指向“添加項(xiàng)目”,然后選擇“新建項(xiàng)目”。
2.?在“添加新項(xiàng)目”對(duì)話框中,選擇“項(xiàng)目類型”窗格中的“安裝和部署項(xiàng)目”,然后選擇“模板”窗格中的“安裝項(xiàng)目”。在“名稱”框中鍵入 setup。
1.在.NET打包項(xiàng)目中添加文件msiexec.exe(一般可在c:\windows\system32\下找到)
2.在文件系統(tǒng)視圖中選擇應(yīng)用程序文件夾,在msiexec.exe上按右鍵,選擇創(chuàng)建快捷方式,重命名快捷方式為"卸載".
3.更改此快捷方式的Arguments 為"/x {產(chǎn)品id}",產(chǎn)品id的值為打包項(xiàng)目的ProductCode屬性值.
1.先生成安裝包,記下ProductCode(選擇解決方案資源管理器根目錄如setup1,再查看屬性標(biāo)簽,不是右鍵中的屬性),下面要用到
2.用VS.net建立一個(gè)新的控制臺(tái)程序uninst.exe文件
-
'power?by:?landlordh ?
-
'for?2000,xp,2003 ?
-
Module?uninstall ?
-
????Sub?Main() ?
-
????????Dim?myProcess?As?
Process
?=?
New
?Process ?
-
????????If?System.Environment.OSVersion.ToString.IndexOf("NT?5")?Then ?
-
????????????myProcess.Start("msiexec",?"/X{2B65D4A9-C146-4808-AB4B-321FB0779559}")
'改為自己的ProductCode ?
-
????????End?If ?
-
????????myProcess.Close() ?
-
????End?Sub ?
-
End?Module ?
3.將控制臺(tái)程序BIN目錄的exe文件加入到打包程序文件中,在程序組創(chuàng)建uninst.exe的快捷方式
-
usingSystem; ?
-
usingSystem.Collections; ?
-
usingSystem.ComponentModel; ?
-
usingSystem.Configuration.Install; ?
-
usingSystem.Reflection; ?
-
usingSystem.IO; ?
-
usingSystem.Data; ?
-
usingSystem.Data.SqlClient; ?
-
namespaceinstall{ ?
-
/// ?
-
///Installer1的摘要說明。 ?
-
/// ?
-
[RunInstaller(true)] ?
-
publicclassInstaller1:System.Configuration.Install.Installer{ ?
-
/// ?
-
///必需的設(shè)計(jì)器變量。 ?
-
/// ?
-
privateSystem.ComponentModel.Containercomponents
=
null
; ?
-
publicInstaller1(){ ?
-
//該調(diào)用是設(shè)計(jì)器所必需的。 ?
-
InitializeComponent(); ?
-
//TODO:在InitializeComponent調(diào)用后添加任何初始化 ?
-
} ?
-
/// ?
-
///清理所有正在使用的資源。 ?
-
/// ?
-
protectedoverridevoidDispose(booldisposing){ ?
-
if(disposing){ ?
-
if(components!=null){ ?
-
components.Dispose(); ?
-
} ?
-
} ?
-
base.Dispose(disposing); ?
-
} ?
-
?
-
#region組件設(shè)計(jì)器生成的代碼 ?
-
/// ?
-
///設(shè)計(jì)器支持所需的方法-不要使用代碼編輯器修改 ?
-
///此方法的內(nèi)容。 ?
-
/// ?
-
privatevoidInitializeComponent(){ ?
-
components
=
newSystem
.ComponentModel.Container(); ?
-
} ?
-
#endregion ?
-
privatestringGetSql(stringName){ ?
-
////調(diào)用osql執(zhí)行腳本 ?
-
// ?
-
//
System.Diagnostics.ProcesssqlProcess
=
newSystem
.Diagnostics.Process(); ?
-
// ?
-
//
sqlProcess.StartInfo.FileName
=
"osql.exe"
; ?
-
// ?
-
//
sqlProcess.StartInfo.Arguments
=
String
.Format("-U{0}-P{1}-d{2}-i{3}db.sql",
this.Context.Parameters["user"],this.Context.Parameters["pwd"],"master",this.
Context.Parameters["targetdir"]); ?
-
// ?
-
//
sqlProcess.StartInfo.WindowStyle
=
System
.Diagnostics.ProcessWindowStyle.Hidden; ?
-
// ?
-
//sqlProcess.Start(); ?
-
// ?
-
//sqlProcess.WaitForExit();//等待執(zhí)行 ?
-
// ?
-
//sqlProcess.Close(); ?
-
try{ ?
-
//
Assembly
AssemblyAsm
=Assembly.GetExecutingAssembly(); ?
-
//
System.IO.FileInfoFileInfo
=
newSystem
.IO.FileInfo(Asm.Location); ?
-
//
stringpath
=
FileInfo
.DirectoryName+@"\"+Name; ?
-
stringpath
=
this
.Context.Parameters["targetdir"]+Name; ?
-
FileStreamfs
=
newFileStream
(path,FileMode.Open,FileAccess.Read,FileShare.Read); ?
-
StreamReaderreader
=
newStreamReader
(fs,System.Text.Encoding.Default); ?
-
//System.Text.Encoding.ASCII; ?
-
returnreader.ReadToEnd(); ?
-
} ?
-
catch(Exceptionex){ ?
-
Console.Write("InGetSql:"+ex.Message); ?
-
throwex; ?
-
} ?
-
} ?
-
privatevoidExecuteSql(stringDataBaseName,stringSql){ ?
-
SqlConnectionsqlConnection1
=
newSqlConnection
(); ?
-
sqlConnection1.ConnectionString
=
string
.Format("
server
={0};
userid
={1};
password
={2};
-
Database
=master",this.Context.Parameters["server"],this.Context.Parameters["user"],
this.Context.Parameters["
pwd
"]); ?
-
System.Data.SqlClient.SqlCommandCommand
=
newSystem
.Data.SqlClient.
SqlCommand(Sql,sqlConnection1); ?
-
try{ ?
-
Command.Connection.Open(); ?
-
Command.Connection.ChangeDatabase(DataBaseName); ?
-
Command.ExecuteNonQuery(); ?
-
} ?
-
catch(Exceptionex){ ?
-
Console.Write("Inexceptionhandler:"+ex.Message); ?
-
} ?
-
finally{ ?
-
Command.Connection.Close(); ?
-
} ?
-
} ?
-
protectedvoidAddDBTable(stringstrDBName){ ?
-
try{ ?
-
ExecuteSql("master","CREATEDATABASE"+strDBName); ?
-
ExecuteSql(strDBName,GetSql("sql.txt")); ?
-
ExecuteSql("master","execsp_addlogin'myoamaster','myoamaster','"+strDBName+"',Null,Null"); ?
-
ExecuteSql(strDBName,"EXECsp_grantdbaccess'myoamaster','myoamaster'"); ?
-
ExecuteSql(strDBName,"execsp_addrolemember'db_owner','myoamaster'"); ?
-
} ?
-
catch(Exceptionex){ ?
-
Console.Write("Inexceptionhandler:"+ex.Message); ?
-
} ?
-
} ?
-
publicoverridevoidInstall(System.Collections.IDictionarystateSaver){ ?
-
base.Install(stateSaver); ?
-
AddDBTable(this.Context.Parameters["dbname"]); ?
-
} ?
-
} ?
-
}?
-
privatevoidCreateDataBase(stringstrSql,stringDataName,stringstrMdf,stringstrLdf) ?
-
{ ?
-
Stringstr; ?
-
SqlConnectionmyConn
=
newSqlConnection
(strSql); ?
-
//EXECsp_detach_db@
dbname
=
'BX_FreightMileage_2'
//需要先將數(shù)據(jù)庫(kù)分離出來(lái) ?
-
str
=
"EXECsp_attach_db@dbname='"
+DataName+"',@
filename1
=
'"+strMdf+"'
,@
filename2
=
'"+strLdf+"'
"; ?
-
SqlCommandmyCommand
=
newSqlCommand
(str,myConn); ?
-
?
-
myConn.Open(); ?
-
myCommand.ExecuteNonQuery(); ?
-
myConn.Close(); ?
-
?
-
}?
當(dāng)然.NET打包項(xiàng)目也要把這兩個(gè)數(shù)據(jù)庫(kù)文件也加進(jìn)來(lái)。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

