Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Help me please
C#
How to take backup using sql server 2005 stored procedure in vb6


What I have tried:

this is my sql stored procedure

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go

create procedure [dbo].[GetBackup]
@SQLSTATEMENT VARCHAR (2000)
As
Set Nocount on;

SET @SQLSTATEMENT='f:\Print_Branch\Print_Branch' + CONVERT (nvarchar (30), Getdate(),112)+ '.bak'
Backup database Print_Branch to disk= @SQLSTATEMENT WITH NOFORMAT, INIT, NAME = N'Print_Branch-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
Posted
Updated 27-Apr-16 2:48am
Comments
Suvendu Shekhar Giri 27-Apr-16 3:20am    
Are you getting any error?
Richard Deeming 27-Apr-16 9:27am    
Why have you declared @SQLSTATEMENT as a parameter, when the procedure always overwrites it, and the value is never returned to the client?

Just declare the variable instead:
create procedure [dbo].[GetBackup]
As
DECLARE @SQLSTATEMENT varchar(2000);
SET NOCOUNT ON;

SET @SQLSTATEMENT = ...

As the previous solution is in VB.NET and you have mentioned VB6 three times in your post, I'm guessing you could do with a link that helps you write a ... er ... VB6 solution.

This CP article should help How to Use Stored Procedures in VB6[^]
 
Share this answer
 
The following may help you.

VB
 Private cnstr As String = "Provider=SQLOLEDB.1;Password=MyDataBase;   Persist Security Info=True;User ID=xxxx;Initial Catalog=xxxx;Data Source=192.168.1.##"
   Dim cn As New OleDb.OleDbConnection(cnstr)
          Sub Main()
       Try
           OpenConnection()

           Dim sql As New OleDb.OleDbCommand("SELECT '['+ SPECIFIC_SCHEMA + '].[' + SPECIFIC_NAME + ']' AS StoredProcedure,  SPECIFIC_NAME as SPName FROM INFORMATION_SCHEMA.ROUTINES", cn)
           Dim Da As New OleDb.OleDbDataAdapter(sql)
           Dim StoredProcedures As New DataTable("StoredProcedures")
           Dim SP As DataRow
           Dim OutputDirectory As String = CurDir() & "\"
           Dim OutputFile As String

           Da.Fill(StoredProcedures)

           For Each SP In StoredProcedures.Rows
               OutputFile = OutputDirectory & SanitizeFileName(SP("SPName")) & ".sql"
               Call SaveSPToFile(OutputFile, SP("StoredProcedure"), SP("SPName"))
           Next

           CloseConnection()
       Catch ex As Exception
           CloseConnection()
           Debug.Print(ex.ToString)
       End Try
   End Sub

Private Sub SaveSPToFile(ByVal FilePath As String, ByVal StoredProcedure As String, ByVal StoredProcedureName As String)
       Try
           If System.IO.File.Exists(FilePath) Then
               Kill(FilePath)
           End If
           System.IO.File.Create(FilePath)

           Dim sql As OleDb.OleDbCommand = cn.CreateCommand()
           sql.CommandType = CommandType.StoredProcedure
           sql.CommandText = "sp_HelpText '" & StoredProcedure & "'"

           Dim sqlrdr As OleDb.OleDbDataReader = sql.ExecuteReader() 'This is where it errors.....
           Dim wrtr As New System.IO.StreamWriter(FilePath)
           Do While sqlrdr.Read
               wrtr.WriteLine(sqlrdr.GetString(0))
           Loop
           sqlrdr.Close()
           wrtr.Dispose()
       Catch ex As Exception
           CloseConnection()
           Debug.Print(ex.ToString)
       End Try
   End Sub
 
Share this answer
 
Comments
CHill60 27-Apr-16 8:46am    
I don't recall VB6 having Try-Catch

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900