Imports System.ComponentModel Imports System.Configuration.Install Imports System.IO Imports System.Reflection<RunInstaller(True)> Public Class DBCustomAction Inherits System.Configuration.Install.Installer#Region " 组件设计器生成的代码 " Public Sub New() MyBase.New() '该调用是组件设计器所必需的。 InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub 'Installer 重写 dispose 以清理组件列表。 Protected Overloads Overrides 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() End Sub Private Function GetSql(ByVal Name As String) As String Try ' Get the current assembly. Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly() ' Resources are named using a fully qualified name. Dim strm As Stream = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name) ' Read the contents of the embedded file. Dim reader As StreamReader = New StreamReader(strm) Return reader.ReadToEnd() Catch ex As Exception MsgBox("In GetSQL: " & ex.Message) Throw ex End Try End Function Private 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 Protected Sub AddDBTable(ByVal strDBName As String) Try ' Create the database. 'ExecuteSql("master", "CREATE DATABASE " + strDBName) ' Create the tables. 'ExecuteSql(strDBName, GetSql("26UpdateScript.sql")) Catch ex As Exception ' Report any errors and abort. 'MsgBox("In exception handler: " & ex.Message) 'Throw ex End Try End Sub Public Overrides 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("passwd")) '根据输入的数据库名称建立数据库 ExecuteSql(connStr, "master", "CREATE DATABASE " + Me.Context.Parameters.Item("database")) '调用osql执行脚本 Dim sqlProcess As New System.Diagnostics.Process() sqlProcess.StartInfo.FileName = "osql.exe " sqlProcess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -S {2} -d {3} -i {4}db.sql", Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("passwd"), Me.Context.Parameters.Item("server"), Me.Context.Parameters.Item("database"), Me.Context.Parameters.Item("targetdir")) sqlProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal 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 'AddDBTable(Me.Context.Parameters.Item("tanhm")) End Sub#End RegionEnd Class 或者用sql-dmo,网上有例子,sql server帮助里也可以查到
下面这段怎么翻译成c#的呢?? ---------------------将连接字符串写入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 SubEnd Class
而且我的是winform的,不是web的
演练:部署 Web 解决方案 http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/vsintro7/html/vbtskDeployingWebSolution.asp参照 http://www.itonline.gd.cn/ittech/list.asp?id=617 http://blog.csdn.net/younther/archive/2004/06/16/20185.aspx
Imports System.Configuration.Install
Imports System.IO
Imports System.Reflection<RunInstaller(True)> Public Class DBCustomAction
Inherits System.Configuration.Install.Installer#Region " 组件设计器生成的代码 " Public Sub New()
MyBase.New() '该调用是组件设计器所必需的。
InitializeComponent() '在 InitializeComponent() 调用之后添加任何初始化 End Sub 'Installer 重写 dispose 以清理组件列表。
Protected Overloads Overrides 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() End Sub Private Function GetSql(ByVal Name As String) As String
Try ' Get the current assembly.
Dim Asm As [Assembly] = [Assembly].GetExecutingAssembly() ' Resources are named using a fully qualified name.
Dim strm As Stream = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name) ' Read the contents of the embedded file.
Dim reader As StreamReader = New StreamReader(strm)
Return reader.ReadToEnd()
Catch ex As Exception
MsgBox("In GetSQL: " & ex.Message)
Throw ex
End Try End Function Private 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 Protected Sub AddDBTable(ByVal strDBName As String)
Try
' Create the database.
'ExecuteSql("master", "CREATE DATABASE " + strDBName) ' Create the tables.
'ExecuteSql(strDBName, GetSql("26UpdateScript.sql")) Catch ex As Exception
' Report any errors and abort.
'MsgBox("In exception handler: " & ex.Message)
'Throw ex
End Try
End Sub Public Overrides 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("passwd"))
'根据输入的数据库名称建立数据库
ExecuteSql(connStr, "master", "CREATE DATABASE " + Me.Context.Parameters.Item("database"))
'调用osql执行脚本
Dim sqlProcess As New System.Diagnostics.Process()
sqlProcess.StartInfo.FileName = "osql.exe "
sqlProcess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -S {2} -d {3} -i {4}db.sql", Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("passwd"), Me.Context.Parameters.Item("server"), Me.Context.Parameters.Item("database"), Me.Context.Parameters.Item("targetdir"))
sqlProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal
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
'AddDBTable(Me.Context.Parameters.Item("tanhm"))
End Sub#End RegionEnd Class
或者用sql-dmo,网上有例子,sql server帮助里也可以查到
通过http://community.csdn.net/Expert/topic/3058/3058459.xml?temp=.9891321
已经可以创建数据库了
可程序中的数据库连接字符串怎么通过创建数据库时修改呢??
http://dev.csdn.net/develop/article/27/27814.shtm
---------------------将连接字符串写入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 SubEnd Class
http://msdn.microsoft.com/library/chs/default.asp?url=/library/chs/vsintro7/html/vbtskDeployingWebSolution.asp参照
http://www.itonline.gd.cn/ittech/list.asp?id=617
http://blog.csdn.net/younther/archive/2004/06/16/20185.aspx