SQL数据库mdf文件如何通过安装软件附加到已安装sqlserver的服务器中

wq_quake 2005-07-28 07:56:40
基于SQLserver2000的程序写好了,准备做一个安装程序,现在不知道怎么才能够通过install shield设置才能让mdf文件和ldf文件自动导入到sqlserver2000中!对参与者和答题者表示同样的感谢!
...全文
1036 7 打赏 收藏 举报
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
scy2510 2005-12-10
看不懂呀,我也想学这个呀,找了好久的资料了呀
收藏一下,这是vb的程序吗
  • 打赏
  • 举报
回复
wq_quake 2005-07-29
怎么用啊大哥,这么复杂的话,我现在都想专门写个小程序来完成。直接用SQL命令附加数据库得了。说句心里话,哥们你真强,学习!
  • 打赏
  • 举报
回复
猪儿滚滚 2005-07-28
搬个凳子来学习
  • 打赏
  • 举报
回复
summerICEREDTEA 2005-07-28
--zlp321002(职业-->烧人民币)
vb.net?
  • 打赏
  • 举报
回复
summerICEREDTEA 2005-07-28
--zlp321002(职业-->烧人民币)
nb啊 MARK 学习
  • 打赏
  • 举报
回复
zlp321002 2005-07-28
--说我贴子太长,不让发,你凑合看看....
  • 打赏
  • 举报
回复
zlp321002 2005-07-28
--用我的安装服务,测试通过

...........
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"))
'判断如果目标计算机中有数据库,则删除原来数据库,重新最新数据库
'(1):支持同时安装多个数据库。
'(2):支持历史数据装载
'(3):为支持以后扩充,要写WebConfig时候,WebConfig中必须预留"connString" 字样
'例子:Me.Context.Parameters.Item("dbname")="water,waterSJSGNQ"
'得到目标计算机的虚拟目录地址
Dim stLocation As String
Dim ASM As [Assembly] = [Assembly].GetExecutingAssembly()
stLocation = ASM.Location
Dim dir As System.IO.Directory
stLocation = dir.GetParent(stLocation).FullName()

Dim DataBaseArray() As String
Dim Loopi As Integer
DataBaseArray = Split(Me.Context.Parameters.Item("dbname"), ",")
For Loopi = 0 To UBound(DataBaseArray)
'建立数据库,未加栽数据
ExecuteSql(connStr, "master", "IF EXISTS (SELECT * FROM MASTER..sysdatabases WHERE NAME=N'" & DataBaseArray(Loopi) & " ') DROP DATABASE " & DataBaseArray(Loopi) & " CREATE DATABASE " & DataBaseArray(Loopi) & " ")
'加载历史数据
CreateDataBase(DataBaseArray(Loopi), stLocation)
'将连接字符串写入Web.config,为将来预留程序接口
'WriteWebConfig(DataBaseArray(Loopi))
Next

'删除目标计算机的OSQL文件
Dim sqlFileInfo As New System.IO.FileInfo(String.Format("{0}OSQL.EXE", Me.Context.Parameters.Item("targetdir")))
If sqlFileInfo.Exists Then
sqlFileInfo.Delete()
End If
Catch ex As Exception
Throw ex
End Try


End Sub


'在数据库实现数据加载
Private Sub CreateDataBase(ByVal DatabaseName As String, ByVal stLocation As String)
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"))
'调用OSQL,实现数据的装载
Dim SQLString As String
SQLString = "use master declare @Data_Path as varchar(100),@Log_Path as varchar(100) "
SQLString = SQLString + " select @Data_Path=ltrim(rtrim(filename)) from " & DatabaseName & "..sysfiles where charindex(''MDF'',filename)>0 "
SQLString = SQLString + " select @Log_Path=ltrim(rtrim(filename)) from " & DatabaseName & "..sysfiles where charindex(''LDF'',filename)>0 "
SQLString = SQLString + " RESTORE DATABASE " & DatabaseName & " FROM DISK = ''" & stLocation & "\" & DatabaseName & ".bak'' with replace, move ''" & DatabaseName & "_Data'' to @Data_Path, move ''" & DatabaseName & "_Log'' to @Log_Path "
SQLString = " exec('master..xp_cmdShell '' osql -U " & Me.Context.Parameters.Item("user") & " -P " & Me.Context.Parameters.Item("pwd") & " -S " & Me.Context.Parameters.Item("targetdir") & " -Q '' " & SQLString & " ') "

Dim sqlProcess As New System.Diagnostics.Process
sqlProcess.StartInfo.FileName = "osql.exe "
sqlProcess.StartInfo.Arguments = SQLString
ExecuteSql(connStr, "master", SQLString)
sqlProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
sqlProcess.Start()
sqlProcess.WaitForExit() '等待执行
sqlProcess.Close()
'删除目标计算机的数据库备份文件
Dim sqlFileInfo As New System.IO.FileInfo(String.Format("{0}" & DatabaseName & ".bak", Me.Context.Parameters.Item("targetdir")))
If sqlFileInfo.Exists Then
sqlFileInfo.Delete()
End If
End Sub


' ---------------------将连接字符串写入Web.config-----------------------------------
Private Sub WriteWebConfig(ByVal DatabaseName As String)
Dim di As IO.DirectoryInfo
di = New IO.DirectoryInfo(Me.Context.Parameters.Item("targetdir"))
Dim fi As IO.FileInfo
For Each fi In di.GetFiles("web.config")
ProcessString(fi.FullName, DatabaseName)
Next
End Sub



Public Sub ProcessString(ByVal FileName As String, ByVal DatabaseName As String)
Try
Dim FileInfo As System.IO.FileInfo = New System.IO.FileInfo(FileName)
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"), DatabaseName, 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
..........
  • 打赏
  • 举报
回复
相关推荐
发帖
应用实例

2.7w+

社区成员

MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
帖子事件
创建了帖子
2005-07-28 07:56
社区公告
暂无公告