vs2005的数据库部署问题,急呀
我用vs2005做的数据库部署文件 ,数据库安装到SQLserver上了,可是脚本没有运行,怎么回事呀,大侠帮我看看,急呀
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.IO
Imports System.Reflection
<RunInstaller(True)> Public Class DBCustomAction
Inherits System.Configuration.Install.Installer
'执行SQL 语句
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
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("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("-S{0} -U {1} -P {2} -d {3} -i {4}db.sql", Me.Context.Parameters.Item("server"), _
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
End Sub
End Class --------------------编程问答-------------------- 帮顶,sql2005问题多多 --------------------编程问答-------------------- '是的,这个问题我遇到过,我解决了.我把Install改了一下,就可以了:
'比如数据库名称为:dataname
Imports System.ComponentModel
Imports System.Configuration.Install
Imports System.IO
Imports System.Reflection
Public Class DBCustomAction
Public Sub New()
MyBase.New()
'组件设计器需要此调用。
InitializeComponent()
'调用 InitializeComponent 后添加初始化代码
End Sub
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
Command.Connection.Close()
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={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"))
Dim Path As String = String.Format("{0}db.sql", Me.Context.Parameters.Item("targetdir"))
'调用osql执行脚本
'''''''''''''''''''''''''''''''''''''
ExecuteSql(connStr, "master", "CREATE DATABASE " + "dataname")
Dim fs As New FileStream(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim sr As New StreamReader(fs, System.Text.Encoding.Default)
Dim x As String
Dim newSqlstr As String = ""
While sr.Peek <> -1
x = sr.ReadLine()
If x <> "GO" Then
newSqlstr = newSqlstr & " " & x
Else
ExecuteSql(connStr, "dataname", newSqlstr)
newSqlstr = ""
End If
End While
'''''''''''''''''''''''''''''''''''''''
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("server"), Me.Context.Parameters.Item("targetdir"))
sqlProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
sqlProcess.Start()
sqlProcess.WaitForExit() '等待执行
sqlProcess.Close()
Catch ex As Exception
Throw ex
End Try
End Sub
End Class
'因为 ExecuteSql(connStr, "master", "CREATE DATABASE " + "dataname")已经在master表中添加过记录了,所以
'脚本中不需要CREATE DATABASE dataname这一行脚本了
'下面是数据库脚本
use dataname
GO
CREATE TABLE [dbo].[ConcentratorData] (
[ID] [int] IDENTITY (1, 1) NOT NULL ,
[ConcentratorID] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[ConcentratorData] [nvarchar] (4000) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[DataType] [int] NOT NULL ,
[ReadingDate] [datetime] NOT NULL
) ON [PRIMARY]
GO --------------------编程问答-------------------- 友情帮顶。。。 --------------------编程问答-------------------- 。。 --------------------编程问答-------------------- 。。 --------------------编程问答-------------------- 。。
补充:.NET技术 , VB.NET