Club Builder - Foros de discusión Club Builder
Foro de C++Builder en español
 
 FAQFAQ   BuscarBuscar   MiembrosMiembros   Grupos de UsuariosGrupos de Usuarios   RegistrarseRegistrarse 
 PerfilPerfil   Entra para ver tus mensajes privadosEntra para ver tus mensajes privados   EntrarEntrar 

Backup SQL Server

 
Publicar nuevo tema   Responder al tema    Foros de discusión -> Consultas
Ver tema anterior :: Ver tema siguiente  
Autor Mensaje
Mensaje    Asunto: Backup SQL Server Responder citando

hola amigos del foro
Necesito una ayuda con este cpp que encontre en la carpeta del instalador del sql server DEVTOOLS\SAMPLES\BACKUP, este viene junto con dos ficheros .dsp y .dsw los cuales desconozco.

quiciera que pudiera correr en c++ builder 6 pero me da el primer error en:
IClientVirtualDeviceSet2* vds = NULL ; con el error siguiente: [C++ Error] simple.cpp(6Cool: E2451 Undefined symbol 'IClientVirtualDeviceSet2'. Aca les dejo el .cpp, espero cualquier ayuda por favor.



Código:
/***********************************************************************
Copyright (c) 2000, Microsoft Corporation
All Rights Reserved.
***********************************************************************/
// This source code is an intended supplement to the Microsoft SQL
// Server online references and related electronic documentation.
//
// This sample is for instructional purposes only.
// Code contained herein is not intended to be used "as is" in real applications.
//
// simple.cpp
//
// This is a sample program used to demonstrate the Virtual Device Interface
// feature of Microsoft SQL Server.
//
// The program will backup or restore the 'pubs' sample database.
//
// The program accepts a single command line parameter. 
// One of:
//  b   perform a backup
//  r   perform a restore
// 


// To gain access to free threaded COM, you'll need to define _WIN32_DCOM
// before the system headers, either in the source (as in this example),
// or when invoking the compiler (by using /D "_WIN32_DCOM")
//
// This sample uses mixed mode security, other than Windows NT Authentication,
// to establish connections. If you want to use Windows NT Authentication, please
// do not use the -U or -P options in the isql command.
// Make the necessary changes to the hard-coded values, such as server
// name, user name and password in the isql command.
#define _WIN32_DCOM

#include <objbase>    // for 'CoInitialize()'
#include <stdio>      // for file operations
#include <ctype>      // for toupper ()
#include <process>    // for spawn ()

#include "vdi.h"        // interface declaration
#include "vdierror.h"   // error constants

#include "vdiguid.h"    // define the interface identifiers.
         // IMPORTANT: vdiguid.h can only be included in one source file.
         //


void performTransfer (
    IClientVirtualDevice*   vd,
    int                     backup );

int execSQL (int doBackup);



//
// main function
//
int main(int argc, char *argv[])
{
    HRESULT                     hr;
    IClientVirtualDeviceSet2*   vds = NULL ;
    IClientVirtualDevice*       vd=NULL;

    VDConfig                    config;
    int                         badParm=TRUE;
    int                         doBackup;
    int                         hProcess;
    int                         termCode;

    // Check the input parm
    //
    if (argc == 2)
    {
        if (toupper(argv[1][0]) == 'B')
        {
            doBackup = TRUE;
            badParm = FALSE;
        }
        else if (toupper(argv[1][0]) == 'R')
        {
            doBackup = FALSE;
            badParm = FALSE;
        }
    }

    if (badParm)
    {
        printf ("useage: simple {B|R}\n"
            "Demonstrate a Backup or Restore using the Virtual Device Interface\n");
        exit (1);
    }

    printf ("Performing a %s using a virtual device.\n",
        (doBackup) ? "BACKUP" : "RESTORE");

    // Initialize COM Library
    // Note: _WIN32_DCOM must be defined during the compile.
    //
    hr = CoInitializeEx (NULL, COINIT_MULTITHREADED);

    if (!SUCCEEDED (hr))
    {
        printf ("Coinit fails: x%X\n", hr);
        exit (1);
    }

    // Get an interface to the device set.
    // Changes from SQLServer v7.0:
   //    - we've introduced a symbol for the class ID.
   //    - We've added a new "multi-instance-aware" interface.
    //
    hr = CoCreateInstance (
        CLSID_MSSQL_ClientVirtualDeviceSet, 
       NULL,
       CLSCTX_INPROC_SERVER,
       IID_IClientVirtualDeviceSet2,
       (void**)&vds);

    if (!SUCCEEDED (hr))
    {
        // This failure might happen if the DLL was not registered,
   // or if the application is using the wrong interface id (IID).
        //
        printf ("Could not create component: x%X\n", hr);
        printf ("Check registration of SQLVDI.DLL and value of IID\n");
        goto exit;
    }

    // Setup the VDI configuration we want to use.
    // This program doesn't use any fancy features, so the
    // only field to setup is the deviceCount.
    //
    // The server will treat the virtual device just like a pipe:
    // I/O will be strictly sequential with only the basic commands.
    //
    memset (&config, 0, sizeof(config));
    config.deviceCount = 1;

    // Create the virtual device set
   // for use by the default instance.
    // Change the first parameter to identify a named instance.
   //
    hr = vds->CreateEx (NULL, L"SUPERBAK", &config);
    if (!SUCCEEDED (hr))
    {
        printf ("VDS::Create fails: x%X", hr);
        goto exit;
    }

    // Send the SQL command, by starting 'isql' in a subprocess.
    //
    printf("\nSending the SQL...\n");

    hProcess = execSQL (doBackup);
    if (hProcess == -1)
    {
        printf ("execSQL failed.\n");
        goto shutdown;
    }

    // Wait for the server to connect, completing the configuration.
    //
    hr = vds->GetConfiguration (10000, &config);
    if (!SUCCEEDED (hr))
    {
        printf ("VDS::Getconfig fails: x%X\n", hr);
        if (hr == VD_E_TIMEOUT)
        {
            printf ("Timed out. Was Microsoft SQLServer running?\n");
        }
        goto shutdown;
    }

    // Open the single device in the set.
    //
    hr = vds->OpenDevice (L"SUPERBAK", &vd);
    if (!SUCCEEDED(hr))
    {
        printf ("VDS::OpenDevice fails: x%X\n", hr);
        goto shutdown;
    }

    printf ("\nPerforming data transfer...\n");
   
    performTransfer (vd, doBackup);
   
   
shutdown:

    // Close the set
    //
    vds->Close ();

    // Obtain the SQL completion information, by waiting for isql to exit.
    //
    if (hProcess == _cwait( &termCode, hProcess, NULL))
    {
        if (termCode == 0)
            printf("\nThe SQL command executed successfully.\n");
        else
            printf("\nThe SQL command failed.\n");
    }
    else
    {
        printf ("cwait failed: %d\n", errno);
    }

    // COM reference counting: Release the interface.
    //
    // Rather than releasing it, the application has the
    // option to 'Create' another set, reusing the interface
    // that it currently has.  Of course that applies to
    // a real application, not this simple sample!
    //
    vds->Release () ;

exit:

    // Uninitialize COM Library
    //
    CoUninitialize () ;

    return 0 ;
}

//
// Execute a basic backup/restore, by spawning a process to execute 'isql'.
//
// Returns:
//  -1      : failed to spawn
//  else    : a "process handle"
//
int execSQL (int doBackup)
{
    const char *const bCmd = "-Q\"BACKUP DATABASE PUBS TO VIRTUAL_DEVICE='SUPERBAK'\"";

    const char *const rCmd = "-Q\"RESTORE DATABASE PUBS FROM VIRTUAL_DEVICE='SUPERBAK'\"";
    int rc;

    // Spawn off the isql utility to execute the SQL.
    // Notice the '-b' which causes an error to set a non-zero
    // exit code on error.
    // If you want to use Windows NT Authentication, please do not use the -U or -P options.
    rc = _spawnlp( _P_NOWAIT, "isql", "isql", "-Usa", "-P", "-b",
        (doBackup) ? bCmd : rCmd, NULL);

    if (rc == -1)
    {
        printf ("Spawn failed with error: %d\n", errno);
    }
   
    return (rc);
}

// This routine reads commands from the server until a 'Close' status is received.
// It simply reads or writes a file 'superbak.dmp' in the current directory.
//
void performTransfer (
    IClientVirtualDevice*   vd,
    int                     backup )
{
    FILE *          fh;
    char*           fname = "superbak.dmp";
    VDC_Command *   cmd;
    DWORD           completionCode;
    DWORD           bytesTransferred;
    HRESULT         hr;

    fh = fopen (fname, (backup)? "wb" : "rb");
    if (fh == NULL )
    {
        printf ("Failed to open: %s\n", fname);
        return;
    }

    while (SUCCEEDED (hr=vd->GetCommand (INFINITE, &cmd)))
    {
        bytesTransferred = 0;
        switch (cmd->commandCode)
        {
            case VDC_Read:
                bytesTransferred = fread (cmd->buffer, 1, cmd->size, fh);
                if (bytesTransferred == cmd->size)
                    completionCode = ERROR_SUCCESS;
                else
                    // assume failure is eof
                    completionCode = ERROR_HANDLE_EOF;
                break;

            case VDC_Write:
                bytesTransferred = fwrite (cmd->buffer, 1, cmd->size, fh);
                if (bytesTransferred == cmd->size )
                {
                    completionCode = ERROR_SUCCESS;
                }
                else
                    // assume failure is disk full
                    completionCode = ERROR_DISK_FULL;
                break;

            case VDC_Flush:
                fflush (fh);
                completionCode = ERROR_SUCCESS;
                break;
   
            case VDC_ClearError:
                completionCode = ERROR_SUCCESS;
                break;

            default:
                // If command is unknown...
                completionCode = ERROR_NOT_SUPPORTED;
        }

        hr = vd->CompleteCommand (cmd, completionCode, bytesTransferred, 0);
        if (!SUCCEEDED (hr))
        {
            printf ("Completion Failed: x%X\n", hr);
            break;
        }
    }

    if (hr != VD_E_CLOSE)
    {
        printf ("Unexpected termination: x%X\n", hr);
    }
    else
    {
        // As far as the data transfer is concerned, no
        // errors occurred.  The code which issues the SQL
        // must determine if the backup/restore was
        // really successful.
        //
        printf ("Successfully completed data transfer.\n");
    }

    fclose (fh);
}



_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mensaje    Asunto: Solucion Responder citando

Hola amigos del foro
aca les dejo la solucion a esto sin utilizar el .cpp que he puesto mas arriba:

Código:
AnsiString paraBK = "-S ANDROMEDA -U sa -P sa -Q\"BACKUP DATABASE [IncidenciasSQL] to disk='c:\\backup.bak'\"";

ShellExecute(Application->Handle, "open", "c:\\osql.exe",
paraBK.c_str(), NULL, SW_HIDE) ;


como pueden ver en el tema ejecutar script de mssqlserver publicado por mi, dejo claro el uso de osql para ejecutar scrips de mssql en c++ builder, espero lo hayan entendido, ahora deje un ejemplo de como ejecutar ese script de crear backup, espero ayude a los que como yo estaban perdidos
saludos
_________________
Todos somos ignorantes, lo que no todos ignoramos lo mismo...
Volver arriba
Ver perfil de usuario Enviar mensaje privado Enviar email
Mostrar mensajes de anteriores:   
Cambiar a:  
No puedes publicar temas nuevos en este foro
No puedes responder a temas en este foro
No puedes editar tus mensajes en este foro
No puedes borrar tus mensajes en este foro
No puedes votar en las encuestas de este foro


Hosting WordPress Indedmedia desde 3,99 €
© 2002–2026 Club Builder · Funciona con phpBB.