It is currently Fri Mar 22, 2019 9:32 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: [Done] Creating an ODBC DSN is not OK with version 5.70B1
PostPosted: Tue Jul 17, 2018 10:16 am 
Offline
New User
New User

Joined: Mon Oct 23, 2017 7:28 am
Posts: 5
Problem detected with version 5.70 beta 1 when I want to create automatically the ODBC data source name
The return code of the command below is different between the version 6.62
Code:
Result = SQLConfigDataSource_(0, #ODBC_ADD_DSN, Driver, Base)

It's the same problem described in the post https://www.purebasic.fr/english/viewtopic.php?f=4&t=69462#p514276
It had been fixed in October 2017 with version 6.62

EXECUTION OK WITH 5.62
Code:
SourceOrBaseName -> SQL SERVER
ODBC_FindDriver = 'SQL Server'
ODBC_FindDriver = 'SQL Server Native Client 11.0'
ODBC_FindDriver = 'ODBC Driver 13 for SQL Server'
DSN -> DSN=SQL Server
Driver -> ODBC Driver 13 for SQL Server
ODBC_DeleteConnection : SQLConfigDataSource = 1
MakeConnection : Attribut Server=SERVEUR;Description=Purebasic-ODBC;DSN=SQL Server
MakeConnection : Attribut - Database = Server=SERVEUR;Description=Purebasic-ODBC;DSN=SQL Server ;Database=FIN
Driver = ODBC Driver 13 for SQL Server
connectstring = Server=SERVEUR;Description=Purebasic-ODBC;DSN=SQL Server ;Database=FIN
longueur : 91
Base OK 19858856
SQLConfigDataSource_ Driver ODBC Driver 13 for SQL Server
SQLConfigDataSource_ Base   19858856
MakeConnection : ConfigDataSource = 1
DSN -> DSN=SQL Server
Driver -> ODBC Driver 13 for SQL Server
ODBC_DeleteConnection : SQLConfigDataSource = 0


EXECUTION NOT OK WITH 5.70 beta
Code:
SourceOrBaseName -> SQL SERVER
ODBC_FindDriver = 'SQL Server'
ODBC_FindDriver = 'SQL Server Native Client 11.0'
ODBC_FindDriver = 'ODBC Driver 13 for SQL Server'
DSN -> DSN=SQL Server
Driver -> ODBC Driver 13 for SQL Server
ODBC_DeleteConnection : SQLConfigDataSource = 0
MakeConnection : Attribut Server=SERVEUR;Description=Purebasic-ODBC;DSN=SQL Server
MakeConnection : Attribut - Database = Server=SERVEUR;Description=Purebasic-ODBC;DSN=SQL Server ANAEL;Database=FIN
Driver = ODBC Driver 13 for SQL Server
connectstring = Server=SERVEUR;Description=Purebasic-ODBC;DSN=SQL Server;Database=FIN
longueur : 91
Base OK 21104040
SQLConfigDataSource_ Driver ODBC Driver 13 for SQL Server
SQLConfigDataSource_ Base   21104040
MakeConnection : ConfigDataSource = 0



PROGRAMME
Code:
XIncludeFile "odbc_sqlserver_debug.pbi"

DSN.s = "SQL Server"
ODBC_DeleteConnection(Driver, DSN)
Server.s = "SERVER"
Database.s = "FIN"
User.s = "xx"
Password.s = "xxxx"


If ODBC_MakeConnection(Driver, Server, Database, Description, DSN)
  If OpenDatabase(9, DSN, User, Password)
      Gosub Trt
  Else
    MessageRequester("OPENDATABASE FAILED", "Server : " + Server + Chr(10) + "Driver : " + Driver, #MB_ICONERROR)
  EndIf 
  ODBC_DeleteConnection(Driver, DSN)
Else
  MessageRequester("MAKECONNECTION FAILED", "Server : " + Server + Chr(10) + "Driver : (" + Driver + ")" + Chr(10) + " Database : (" + Database + ")", #MB_ICONERROR)
EndIf


odbc_sqlserver_debug.pbi
Code:
; Direct ODBC Connection
#ODBC_ADD_DSN = 1 ; Add Data source
#ODBC_REMOVE_DSN = 3 ; Remove Data source


; -----------------------------------------------------------------------------
; Bugfix ASCII, Unicode PB v5.4x and PB v5.6x
; By mk-soft version v1.02

Import "odbccp32.lib"
  CompilerIf #PB_Compiler_Unicode
    CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
      SQLInstallerError(iError, *pfErrorCode, *ErrorMSGBuf, cbErrorMsgMax, *ErrorMSGLen) As "_SQLInstallerErrorW@20"
      SQLConfigDataSource(Handle, Type, strDriver.s, *strAttribtues) As "_SQLConfigDataSourceW@16"
    CompilerElse
      SQLInstallerError(iError, *pfErrorCode, *ErrorMSGBuf, cbErrorMsgMax, *ErrorMSGLen) As "SQLInstallerErrorW"
      SQLConfigDataSource(Handle, Type, strDriver.s, *strAttribtues) As "SQLConfigDataSourceW"
    CompilerEndIf
  CompilerElse
    SQLInstallerError(iError, *pfErrorCode, *ErrorMSGBuf, cbErrorMsgMax, *ErrorMSGLen)
    SQLConfigDataSource(Handle, Type, strDriver.s, *strAttribtues)
  CompilerEndIf
EndImport

Import "odbc32.lib"
  CompilerIf #PB_Compiler_Unicode
    CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
      SQLConnect(ConnectionHandle, ServerName.s, NameLength1, UserName.s, NameLength2, Authentication.s, NameLength3) As "_SQLConnectW@28"
      SQLTables(StatementHandle, *CatalogName, NameLength1, *SchemaName, NameLength2, *TableName, NameLength3, *TableType, NameLength4) As "_SQLTablesW@36"
    CompilerElse
      SQLConnect(ConnectionHandle, ServerName.s, NameLength1, UserName.s, NameLength2, Authentication.s, NameLength3) As "SQLConnectW"
      SQLTables(StatementHandle, *CatalogName, NameLength1, *SchemaName, NameLength2, *TableName, NameLength3, *TableType, NameLength4) As "SQLTablesW"
    CompilerEndIf
  CompilerElse
    SQLConnect(ConnectionHandle, ServerName.s, NameLength1, UserName.s, NameLength2, Authentication.s, NameLength3)
    SQLTables(StatementHandle, *CatalogName, NameLength1, *SchemaName, NameLength2, *TableName, NameLength3, *TableType, NameLength4)
  CompilerEndIf 
EndImport
; -----------------------------------------------------------------------------




Procedure.s ODBC_FindDriver(SourceOrBaseName.s) ; Find ODBC driver name for Oracle, SQL Server, Excel, etc ... [ex: ODBC_FindDriver("Oracle")]
  Protected phKey.i, DriverIndex.i, DriverName.s, *Buffer, BufferSize.i, *BufferV, BufferVSize.i, ReturnValue.s
  SourceOrBaseName = UCase(SourceOrBaseName)
  Debug "SourceOrBaseName -> " + SourceOrBaseName
  BufferSize = 1024
  BufferVSize = 1024
  *Buffer = AllocateMemory(BufferSize)
  If *Buffer
    *BufferV = AllocateMemory(BufferVSize)
    If *BufferV
      If RegOpenKeyEx_(#HKEY_LOCAL_MACHINE, "SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers", 0, #KEY_READ, @phKey) = #ERROR_SUCCESS
        Result = RegEnumValue_(phKey, DriverIndex, *Buffer, @BufferSize, #Null, #Null, *BufferV, @BufferVSize)
        If Result = #ERROR_SUCCESS
          DriverIndex = DriverIndex + 1
          DriverName = PeekS(*Buffer) ;,-1, #PB_UTF8)

          If FindString(UCase(DriverName), SourceOrBaseName, 1)
            ReturnValue = DriverName
          Else
            Repeat
              BufferSize = 1024
              BufferVSize = 1024
              Result = RegEnumValue_(phKey, DriverIndex, *Buffer, @BufferSize, #Null, #Null, *BufferV, @BufferVSize)
              If Result = #ERROR_SUCCESS
                 DriverName = PeekS(*Buffer) ;,-1, #PB_UTF8)
                 If FindString(UCase(DriverName), SourceOrBaseName, 1)
                  ReturnValue = DriverName
                  Debug "ODBC_FindDriver = '" + DriverName + "'"
                EndIf
              EndIf
              DriverIndex = DriverIndex + 1
            Until Result = #ERROR_NO_MORE_ITEMS
          EndIf
        EndIf
        RegCloseKey_(phKey)
      EndIf
      FreeMemory(*BufferV)
    EndIf
    FreeMemory(*Buffer)
  EndIf
  ProcedureReturn ReturnValue
EndProcedure

Procedure.i ODBC_ConfigDataSource(Driver.s, ConnectString.s)
  Protected Result, Base, *mem.Character, *ptr.Character, Len, i
    Debug "Driver = " + Driver
    Debug "connectstring = " + ConnectString
 
  If Driver And ConnectString
    Len = Len(ConnectString)
    Debug "longueur : " + Str(Len)
   
    Base = AllocateMemory((Len + 2) << (SizeOf(Character) - 1))
    If Base
      Debug "Base OK " + Str(Base)
      *mem = Base
      *ptr = @ConnectString
      For i = 1 To Len
        If *ptr\c <> ';'
          *mem\c = *ptr\c
        EndIf
        *mem + SizeOf(Character)
        *ptr + SizeOf(Character)
      Next
      Debug "SQLConfigDataSource_ Driver " + Driver
      Debug "SQLConfigDataSource_ Base   " + Str(Base)
      Result = SQLConfigDataSource_(0, #ODBC_ADD_DSN, Driver, Base)
      FreeMemory(Base)
    EndIf
  EndIf
  ProcedureReturn Result
EndProcedure

Procedure.i ODBC_MakeConnection(Driver.s, Server.s, Database.s, Description.s, DSN.s)
  Protected Library.i, *Memory, Result.i, Attributes.s, AlreadyExists.i
  Static DatabaseInit.i
  If DatabaseInit = #False
    If UseODBCDatabase() = 0
      MessageRequester("ERREUR", "ODBC version 3 ou supérieure est nécessaire !", #MB_ICONERROR)
    Else
      DatabaseInit = #True
    EndIf
  EndIf
  If DatabaseInit
    If ExamineDatabaseDrivers()
      While NextDatabaseDriver()
        If DatabaseDriverName() = DSN
          AlreadyExists = #True
          Debug "DSN : " + DSN
        EndIf
      Wend
    EndIf
    If AlreadyExists
      Result = #True
      Debug "MakeConnection : Already Exists"
    Else
      Attributes = "Server=" + Server + ";Description=" + Description + ";DSN=" + DSN
       Debug "MakeConnection : Attribut " + Attributes
      If Database
        Attributes = Attributes + ";Database=" + Database
        Debug "MakeConnection : Attribut - Database = " + Attributes
      EndIf
      Result = ODBC_ConfigDataSource(Driver, Attributes)
      Debug "MakeConnection : ConfigDataSource = " + Str(Result)
    EndIf
  EndIf
  ProcedureReturn Result
EndProcedure

Procedure.i ODBC_DeleteConnection(Driver.s, DSN.s)
  Protected Library.i, Result.i
 
  DSN = "DSN=" + DSN
  Debug "DSN -> " + DSN
  Debug "Driver -> " + Driver
  Result = SQLConfigDataSource_(0, #ODBC_REMOVE_DSN, Driver, DSN)
  Debug "ODBC_DeleteConnection : SQLConfigDataSource = " + Str(Result)
  ProcedureReturn Result
EndProcedure
;

Description.s = "Purebasic-ODBC"
Driver.s = ODBC_FindDriver("SQL Server")


Top
 Profile  
Reply with quote  
 Post subject: Re: Creating an ODBC DSN is not OK with version 5.70B1
PostPosted: Mon Dec 10, 2018 8:06 pm 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 13496
Location: France
Fixed.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye