Hi i want to back up my database.
Can sme 1 know the code of backup ?

Dani AI

Generated

As and noted, this is really a database question. For SQL Server there are two straightforward ways to run a backup from an ASP.NET/VB.NET app: execute a T-SQL BACKUP statement via ADO.NET, or use the SMO managed API for richer control. Short examples and key cautions follow.

' Example: run a T-SQL BACKUP from VB.NET
Imports System.Data.SqlClient

Dim connString As String = "Server=DBSERVER;Database=master;Integrated Security=True;"
Using cn As New SqlConnection(connString)
    cn.Open()
    Using cmd As New SqlCommand("BACKUP DATABASE [MyDatabase] TO DISK = N'C:\Backups\MyDatabase.bak' WITH INIT, STATS = 10", cn)
        cmd.CommandTimeout = 0
        cmd.ExecuteNonQuery()
    End Using
End Using

Important notes: the file path in BACKUP is interpreted by the database server, not the web server. The SQL Server service account (or the account used for the service) must have write permission to the target folder or UNC share. Connect to the master database for the command, set CommandTimeout large (or 0) for long backups, and verify backups with RESTORE VERIFYONLY. See the official BACKUP docs for syntax and options: BACKUP (Transact-SQL).

' Example: SMO backup (requires Microsoft.SqlServer.SqlManagementObjects)
Dim srv As New Microsoft.SqlServer.Management.Smo.Server("DBSERVER")
Dim bkp As New Microsoft.SqlServer.Management.Smo.Backup()
bkp.Action = Microsoft.SqlServer.Management.Smo.BackupActionType.Database
bkp.Database = "MyDatabase"
bkp.Devices.AddDevice("C:\Backups\MyDatabase.bak", Microsoft.SqlServer.Management.Smo.DeviceType.File)
bkp.Initialize = True
bkp.SqlBackup(srv)

SMO requires matching SMO assemblies (or the NuGet package) and gives more control (compression, media sets, progress). For scheduling, use SQL Server Agent (not available in Express) or a scheduled script. Test restores regularly and store backups off-server. SMO docs: Server Management Objects (SMO).

Recommended Answers

All 3 Replies

Try the db forum.

what do u mean by this ?

he mean to post your question in Database Section also.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.