Wednesday, March 19, 2008

How to put your applications shortcut in Startup in VB.NET


'imports
Imports IWshRuntimeLibrary
'code to execute
Dim ws As New WshShell
Dim StartupDir As String = My.Computer.FileSystem.SpecialDirectories.Programs + "\Startup"
If Not IO.Directory.Exists(StartupDir) Then
IO.Directory.CreateDirectory(StartupDir)
End If
If Not IO.File.Exists(StartupDir + "\" + My.Application.Info.AssemblyName + ".lnk") Then
Dim shortCut As IWshRuntimeLibrary.IWshShortcut
' short cut files have a .lnk extension
shortCut = CType(ws.CreateShortcut(StartupDir + "\" + My.Application.Info.AssemblyName + ".lnk"), IWshRuntimeLibrary.IWshShortcut)

' set the shortcut properties
With shortCut
.TargetPath = System.Reflection.Assembly.GetExecutingAssembly.Location()
.WindowStyle = 1
.Description = "App to run at Windows Startup"
.WorkingDirectory = StartupDir
' the next line gets the first Icon from the executing program
.IconLocation = System.Reflection.Assembly.GetExecutingAssembly.Location() & ", 0"
.Save() ' save the shortcut file
End With
End If

Backup SQL Server DB in VB.NET


Dim saveFileDialog1 As New SaveFileDialog()
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim pathToBackUpFile As String = saveFileDialog1.FileName
Dim sql As String = " use master;"
sql += " declare @p varchar(300);"
sql += " set @p = '" + pathToBackUpFile + "';"
sql += " SET NOCOUNT OFF;"
sql += " BACKUP DATABASE [YourDatabaseName] TO DISK = @p "
sql += " WITH NOINIT, NOUNLOAD, NAME = N'Database Backup', "
sql += " NOSKIP , STATS = 10, NOFORMAT ;"

Dim cnStr As String = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master"'put your connection string to master db

Dim cnn As OleDb.OleDbConnection
cnn = New OleDb.OleDbConnection(cnStr)
cnn.Open()
Dim cmd As New OleDb.OleDbCommand(sql, cnn)
Dim i As String = -1
Try
i = cmd.ExecuteNonQuery()
cnn.Close()
cmd.Connection.Close()
Catch ex As Exception
cnn.Close()
cmd.Connection.Close()
i = ex.Message
End Try
If i = "-1" Then
MsgBox("BackedUp Database Successfully!", MsgBoxStyle.Information)
Else
MsgBox("Error in Backup!", MsgBoxStyle.Critical)
End If
End If

Restoring SQL Server Database in VB.NET

In VB.NET
Just make a connection to the Master Database of SQL Server
Then run this code against the connection


Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.Filter = "Bak Files|*.bak"
openFileDialog1.Title = "Select a Backup File"

If openFileDialog1.ShowDialog() = DialogResult.OK Then
Dim pathToBackUpFile As String = openFileDialog1.FileName
Dim sql As String = " use master;"
sql += " declare @p varchar(300);"
sql += " set @p = '" + pathToBackUpFile + "';"
sql += " RESTORE DATABASE [YourDatabaseName]FROM DISK = @p ;"
Dim cnStr As String = "Provider=SQLOLEDB;Data Source=.;Integrated Security=SSPI;Initial Catalog=master"'put your connection string to master db
Dim cnn As OleDb.OleDbConnection
cnn = New OleDb.OleDbConnection(cnStr)
cnn.Open()
Dim cmd As New OleDb.OleDbCommand(sql, cnn)
Dim i As String = -1
Try
i = cmd.ExecuteNonQuery()
cnn.Close()
cmd.Connection.Close()
Catch ex As Exception
cnn.Close()
cmd.Connection.Close()
i = ex.Message
End Try
If i = "-1" Then
MsgBox("Restored Database Successfully!", MsgBoxStyle.Information)
Else
MsgBox("Error in Restore!", MsgBoxStyle.Critical)
End If
End If