odbc.JPGEste script automatiza la creación de una entra ODBC para conectarse a un servidor SQL Server.

CreateDSN.vbs

Visual Basic:
  1. '===================
  2. '
  3. ' File: CreateDSN.vbs
  4. '
  5. ' AUTHOR: Dixan Martinez
  6. ' Date : 2/2/2000
  7. '
  8. ' COMMENT:
  9. '
  10. '===================
  11.  
  12.  
  13.  
  14.    Dim DataSourceName
  15.    Dim DatabaseName
  16.    Dim Description
  17.    Dim DriverPath
  18.    Dim DriverName
  19.    Dim LastUser
  20.    Dim Regional
  21.    Dim Server
  22.  
  23.    Const SystemFolder= 1
  24.    Dim fso
  25.    Dim SysFolder
  26.    Dim SysFolderPath
  27.  
  28.    Set fso = wscript.CreateObject("Scripting.FileSystemObject")
  29.    Set SysFolder =fso.GetSpecialFolder(SystemFolder)
  30.    SysFolderPath= SysFolder.Path
  31.  
  32.    Set oArg= wscript.Arguments
  33.  
  34.    if oArg.Count <> 4 then
  35.        call Usage
  36.    end if
  37.  
  38.    'Specify the DSN parameters.
  39.  
  40.    DataSourceName = oArg(0)
  41.    DatabaseName = oArg(1)
  42.    Description = "Odbc de BD" & DatabaseName
  43.    DriverPath = SysFolderPath & "\sqlsrv32.dll"
  44.    Server = oArg(2)
  45.    LastUser  = oArg(3)
  46.  
  47.    DriverName = "SQL Server"
  48.  
  49.    'Creamos objetos que escriben en el registro
  50.         Set WshShell = WScript.CreateObject("WScript.Shell")
  51.  
  52.     'Establecemos el Key del DSn en el registro
  53.     Dim RegEdPath
  54.     RegEdPath= "HKLM\SOFTWARE\ODBC\ODBC.INI\" & DataSourceName & "\"
  55.     'Grabamos
  56.     WshShell.RegWrite  RegEdPath  , ""
  57.  
  58.    'Ponemos los valores de la nueva Key correspondiente al DSN
  59.  
  60.    WshShell.RegWrite  RegEdPath & "Database" , DatabaseName
  61.    WshShell.RegWrite  RegEdPath & "Description" , Description
  62.    WshShell.RegWrite  RegEdPath & "Driver" , DriverPath
  63.    WshShell.RegWrite  RegEdPath & "LastUser" , LastUser
  64.    WshShell.RegWrite  RegEdPath & "Server" , Server
  65.  
  66.    'Escribo en el Key "ODBC Data Sources" para poder listar el nuevo DSN
  67.    ' en el ODBC Manager
  68.  
  69.    WshShell.RegWrite "HKLM\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources\" & DataSourceName , DriverName
  70.  
  71.    
  72.  
  73. wscript.Quit
  74.  
  75. '--------------------
  76. Sub Usage
  77.      wscript.echo "Create a DSN to access a SQL Server database with ODBC"
  78.      wscript.echo "Uso :"
  79.      wscript.echo "CreateDSN.vbs <DSNName> <DataBaseName> <SQLServer> <User> "
  80.      wscript.echo ""
  81.      wscript.echo "Ejemplo 1 : CreaDSN.vbs NuevoDSN Pub MySQLServer AdminUser"
  82.      wscript.Quit
  83. end sub

Popularidad: 64%