none
备份数据库的方式和方法...请各位高手帮忙 RRS feed

  • 问题

  • 在写的程序里备份数据库用什么方式最好???(有的是把数据库备份文件转换为xml文件...............但我不知道这种方法是不是最好的....)

    窗体Form1

    放置控件有:Textbox1.Text(他的值等于一个路径如:C:\csgl\database\   ) , Button1

    目的:当运行打开Form1后点击Button1按钮,就将数据库备份到指定的路径里,备份文件名为当天的日期如:2008-8-27.Xml

     

    如果转换成XML文件是最简单的方法..就请高手们告诉这个代码改怎么写???

    谢谢

    2008年8月26日 23:27

全部回复

  • 转成xml不是最好的方法,
    代码:
    http://dotnet.aspx.cc/article/f3eb8683-db3c-4460-adb7-7c5c12348a11/read.aspx

    最好使用数据库本身的方方法。如果是sql server,参见

    http://www.jaron.cn/chs_scripts/11/2004-10/101629.shtml
    http://www.codeproject.com/KB/dotnet/SQL_Server_backup.aspx




    2008年8月26日 23:45
    版主
  • 对......就是SQL SERVER 2000的 数据库,2楼的朋友能不能给我传一个实例.....你给的VB.NET DMO的那个实例我放上去有出错

    代码如下:

     

    Public Class Form5
        Public sa As String
        Public passerd As String
        Public file As String
        Public database As String
        Public sqlserver As String

        Public flag As Boolean = True

        Private WithEvents PBackup As New SQLDMO.Backup()''' 未定义类型' SQLDMO.Backup '

     


        Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If flag = True Then
                Me.BACKUP(Me.database, Me.sqlserver, Me.sa, Me.passerd, Me.file)
                Exit Sub
            Else
                Exit Sub
            End If

        End Sub

     

     Public Function BACKUP(ByVal DATABASENAME As String, ByVal SQLServer As String, ByVal ROLE As String, ByVal PASSWORD As String, ByVal backfilename As String)


            Me.ProgressBar1.Value = 0
            Me.BackColor = System.Drawing.Color.RoyalBlue

            On Error GoTo ErrHandler

            Dim SQLSER As New SQLDMO.SQLServer'''未定义类型“ SQLDMO.SQLServer”

            SQLSER.Connect(SQLServer, ROLE, PASSWORD)
            PBackup.Database = DATABASENAME

            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

            PBackup.Files = backfilename
            PBackup.SQLBackup(SQLSER)

            SQLSER.DisConnect()
            SQLSER = Nothing
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            Exit Function

    ErrHandler:

            MsgBox("备份失败,请检查源数据库是否存在、角色权限是否足够或SQL SERVER服务是否被打开" & Chr(13) & "信息:" & Err.Description)
            Resume Next
        End Function

     

     

    Private Sub PBackup_Complete(ByVal Message As String) Handles PBackup.Complete

            Me.ProgressBar1.Value = 100
            PBackup = Nothing

           

        End Sub

     

    Private Sub PBackup_PercentComplete(ByVal Message As String, ByVal Percent As Integer) Handles PBackup.PercentComplete
            Me.BackColor = System.Drawing.Color.RoyalBlue

            Me.ProgressBar1.Value = Percent
            If Percent = 100 Then

                Me.ProgressBar1.Value = 100
                PBackup = Nothing
                          

            End If
        End Sub

    End Class

     

     

     

    请2楼的朋友帮我看一下这段代码是否是备份数据库的代码...而且我要指定默认的备份路径如:c:/csgl/database

    和备份名称如:2008-8-27.*

     

    我该怎样修改???

    2008年8月27日 4:00
  • 引用SQLDMO.dll

    2008年8月28日 1:34
    版主
  • Public Class Form5
        Public sa As String
        Public password As String
        Public file As String
        Public database As String
        Public sqlserver As String

        Public flag As Boolean = True


        Private WithEvents PBackup As New SQLDMO.Backup()

       
        Private Sub Form5_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            Try
                Me.Dispose()
                Me.Hide()
                MDIParent1.Enabled = True
            Catch ex As Exception
                MsgBox(ex.ToString, MsgBoxStyle.Information, "try")
            End Try
        End Sub

        Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If flag = True Then
                Me.BACKUP(Me.database, Me.sqlserver=, Me.sa, Me.password, Me.file)
                Exit Sub
            Else
                Exit Sub
            End If
        End Sub

     

     

          

        Public Sub BACKUP(ByVal DATABASENAME As String, ByVal SQLServer As String, ByVal ROLE As String, ByVal PASSWORD As String, ByVal backfilename As String)


            Me.ProgressBar1.Value = 0


            On Error GoTo ErrHandler

            Dim SQLSER As New SQLDMO.SQLServer

            SQLSER.Connect(SQLServer, ROLE, PASSWORD)
            PBackup.Database = DATABASENAME

            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor

            PBackup.Files = backfilename
            PBackup.SQLBackup(SQLSER)

            SQLSER.DisConnect()
            SQLSER = Nothing
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default
            MsgBox("备份数据库成功", MsgBoxStyle.Information, "提示")
            Exit Sub

    ErrHandler:

            MsgBox("备份失败,请检查源数据库是否存在、角色权限是否足够或SQL SERVER服务是否被打开" & Chr(13) & "信息:" & Err.Description)
            Resume Next
        End Sub
        Private Sub PBackup_Complete(ByVal Message As String) Handles PBackup.Complete

            Me.ProgressBar1.Value = 100
            PBackup = Nothing

            Me.Close()

        End Sub
        Private Sub PBackup_PercentComplete(ByVal Message As String, ByVal Percent As Integer) Handles PBackup.PercentComplete
            Me.BackColor = System.Drawing.Color.RoyalBlue

            Me.ProgressBar1.Value = Percent
            If Percent = 100 Then

                Me.ProgressBar1.Value = 100
                PBackup = Nothing

                Me.Close()

            End If
        End Sub

     


    End Class

     

    添加引用:COM-Microsoft SQLDMO Object Library

     

    后, 代码没有出错。运行后就跳进

     

    ErrHandler:

            MsgBox("备份失败,请检查源数据库是否存在、角色权限是否足够或SQL SERVER服务是否被打开" & Chr(13) & "信息:" & Err.Description)

     

    紧接着就提示:没有指定数据库..................无错误运行...........

     

     

    我想问一下是什么原因出错.....怎么指定数据库?

    还有就是如果备份成功,备份的文件在哪个位置上,备份文件的名称又是什么?

     

     

    2008年8月28日 2:47
  •  feiyun0112 写:

     

    http://dev.csdn.net/article/22/22497.shtm[/quote]

     

    谢谢版主.....OK了

     

    2008年8月28日 4:29