时间:2016-02-16 02:17 来源: 我爱IT技术网 作者:佚名
欢迎您访问我爱IT技术网,今天小编为你分享的编程技术是:【Asp.net与SQLserver一起打包部署安装图文教程】,下面是详细的讲解!
Asp.net与SQLserver一起打包部署安装图文教程
Imports System.ComponentModel
imports System.Configuration.Install
imports System.IO
imports System.Reflection
<runinstaller(true)> Public Class DBCustomActionClass DBCustomAction
inherits System.Configuration.Install.Installer
组件设计器生成的代码#region "组件设计器生成的代码 "
public Sub New()Sub New()
mybase.new()
'该调用是组件设计器所必需的
initializecomponent()
'在 InitializeComponent() 调用之后添加任何初始化
end Sub
' Installer 重写 dispose 以清理组件列表。
protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
if disposing Then
if Not (components Is Nothing) Then
components.dispose()
end If
end If
mybase.dispose(disposing)
end Sub
private components As System.ComponentModel.IContainer
<system.diagnostics.debuggerstepthrough()> Private Sub InitializeComponent()Sub InitializeComponent()
end Sub
#end Region
'执行sql 语句
private Sub ExecuteSql()Sub ExecuteSql(ByVal conn As String, ByVal DatabaseName As String, ByVal Sql As String)
dim mySqlConnection As New SqlClient.SqlConnection(conn)
dim Command As New SqlClient.SqlCommand(Sql, mySqlConnection)
command.connection.open()
command.connection.changedatabase(databasename)
try
command.executenonquery()
finally
'close Connection
command.connection.close()
end Try
end Sub
public Overrides Sub Install()Sub Install(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Install(stateSaver)
' ------------------------建立数据库-------------------------------------------------
try
dim connStr As String=String.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", Me.Context.Parameters.Item("server"), Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("pwd"))
'根据输入的数据库名称建立数据库
executesql(connstr, "master", "CREATE DATABASE " + Me.Context.Parameters.Item("dbname"))
'调用osql执行脚本
dim sqlProcess As New System.Diagnostics.Process
sqlprocess.startinfo.filename="osql.exe "
sqlprocess.startinfo.arguments=String.Format(" -U {0} -P {1} -d {2} -i {3}db.sql", Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("pwd"), Me.Context.Parameters.Item("dbname"), Me.Context.Parameters.Item("targetdir"))
sqlprocess.startinfo.windowstyle=ProcessWindowStyle.Hidden
sqlprocess.start()
sqlprocess.waitforexit() '等待执行
sqlprocess.close()
'删除脚本文件
dim sqlFileInfo As New System.IO.FileInfo(String.Format("{0}db.sql", Me.Context.Parameters.Item("targetdir")))
if sqlFileInfo.Exists Then
sqlfileinfo.delete()
end If
catch ex As Exception
throw ex
end Try
' ---------------------将连接字符串写入Web.config-----------------------------------
try
dim FileInfo As System.IO.FileInfo=New System.IO.FileInfo(Me.Context.Parameters.Item("targetdir") & "\web.config")
if Not FileInfo.Exists Then
throw New InstallException("没有找到配置文件")
end If
'实例化xml文档
dim XmlDocument As New System.Xml.XmlDocument
xmldocument.load(fileinfo.fullname)
'查找到appsettings中的节点
dim Node As System.Xml.XmlNode
dim FoundIt As Boolean=False
for Each Node In XmlDocument.Item("configuration").Item("appSettings")
if Node.Name="add" Then
if Node.Attributes.GetNamedItem("key").Value="connString" Then
'写入连接字符串
node.attributes.getnameditem("value").value=String.Format("Persist Security Info=False;Data Source={0};Initial Catalog={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1", _
me.context.parameters.item("server"), Me.Context.Parameters.Item("dbname"), Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("pwd"))
foundit=True
end If
end If
next Node
if Not FoundIt Then
throw New InstallException("web.Config 文件没有包含connString连接字符串设置")
end If
xmldocument.save(fileinfo.fullname)
catch ex As Exception
throw ex
end Try
end Sub
end Class
关于Asp.net与SQLserver一起打包部署安装图文教程的用户互动如下:
相关问题:
答: >>详细
相关问题:
答: >>详细
相关问题:
答: >>详细
- 【asp】asp.net url重写浅谈-net-url重写
- 【DataSet】DataSet、DataTable、DataRow区别详解
- 【asp】asp.net 动态添加多个用户控件-net-动态添
- 【ASP】ASP.NET中内嵌页面代码的一个问题-NET-内
- 【As】Asp.net中的页面乱码的问题-sp--pn-ne-et
- 【增加记录】asp.net中获取新增加记录的ID Access
- 【创建】ASP.NET Web API教程 创建域模型的方法详
- 【Asp】Asp.net 页面调用javascript变量的值-net-
- 【ASP】ASP.NET 5升级后如何删除旧版本的DNX-NET5
- 【404页面】ASP.NET设置404页面返回302HTTP状态码
- 评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
-
