Hier ein Beipiel aus dem Forum, was ich modifiziert habe:
Code: Alles auswählen
;-TOP
; Kommentar : Excel Tabelle lesen über SQL
; Version : v1.01
; Author : Michael Kastner
; Datei : FcDatabase.pb
; Erstellt : 28.09.2006
; Geändert :
; ***************************************************************************************
#ODBC_ADD_DSN = 1; // Add data source
#ODBC_CONFIG_DSN = 2; // Configure (edit) Data source
#ODBC_REMOVE_DSN = 3; // Remove data source
#ODBC_ADD_SYS_DSN = 4; // add a system DSN
#ODBC_CONFIG_SYS_DSN = 5; // Configure a system DSN
#ODBC_REMOVE_SYS_DSN = 6; // remove a system DSN
#ODBC_REMOVE_DEFAULT_DSN = 7; // remove the default DSN
; ***************************************************************************************
Procedure.s AddDSN(driver.s, databasename.s, user.s, pass.s)
name.s = GetFilePart(databasename.s)
name = Left(name.s, Len(name.s) - 4)
name = "Pure" + name
Select UCase(driver)
Case "ACCESS"
strDriver.s = "Microsoft Access Driver (*.mdb)"
Case "EXCEL"
strDriver.s = "Microsoft Excel Driver (*.xls)"
Default
strDriver.s = driver
EndSelect
strAttributes.s = "Server=APServer;Description=" + name
strAttributes.s + ";DSN=" + name
strAttributes.s + ";DBQ=" + databasename
strAttributes.s + ";UID=" + user
strAttributes.s + ";PWD=" + pass + ";"
MyMemory = AllocateMemory(Len(strAttributes)) ; Allocate the memory you need here
CopyMemory(@strAttributes, MyMemory, Len(strAttributes)) ; Copy the database information string into the memory space
For L = 1 To Len(strAttributes) ; Check the string in the memory space now
If PeekB(MyMemory + L - 1) = Asc(";") ; If you find a semicolon anywhere in the string
PokeB(MyMemory + L - 1, 0) ; Replace it with an empty character as the driver doesn't use it
EndIf ; End the current check
Next L ; Check the next byte
result = SQLConfigDataSource_(0, #ODBC_ADD_DSN, strDriver, MyMemory) ; Call the function you need from the ODBC library with the right details
FreeMemory(MyMemory) ; Free the memory now
If result
ProcedureReturn name
Else
ProcedureReturn ""
EndIf
EndProcedure
; ***************************************************************************************
Procedure.l RemoveDSN(driver.s, databasename.s, user.s, pass.s)
name.s = GetFilePart(databasename.s)
name = Left(name.s, Len(name.s) - 4)
name = "Pure" + name
Select UCase(driver)
Case "ACCESS"
strDriver.s = "Microsoft Access Driver (*.mdb)"
Case "EXCEL"
strDriver.s = "Microsoft Excel Driver (*.xls)"
Default
strDriver.s = driver
EndSelect
strAttributes.s = "DSN=" + name + ";"
MyMemory = AllocateMemory(Len(strAttributes)) ; Allocate the memory you need here
CopyMemory(@strAttributes, MyMemory, Len(strAttributes)) ; Copy the database information string into the memory space
For L = 1 To Len(strAttributes) ; Check the string in the memory space now
If PeekB(MyMemory + L - 1) = Asc(";") ; If you find a semicolon anywhere in the string
PokeB(MyMemory + L - 1, 0) ; Replace it with an empty character as the driver doesn't use it
EndIf ; End the current check
Next L ; Check the next byte
result = SQLConfigDataSource_(0, #ODBC_REMOVE_DSN, strDriver, MyMemory) ; Call the function you need from the ODBC library with the right details
FreeMemory(MyMemory) ; Free the memory now
ProcedureReturn result
EndProcedure
; ***************************************************************************************
;- Test
#Datenbank1 = 1
InitDatabase()
Path.s = "C:\test\Telefon.xls"
; DSN anlegen
Base.s = AddDSN("Excel", Path,"","")
If Base
Debug "DSN=" + Base
If OpenDatabase(#Datenbank1, Base, "", "")
sql1.s="create table adressen (Vorname CHAR(40),Name char(40))"
If DatabaseQuery(#Datenbank1, sql1)
Else
MessageRequester("","Erstellen der Tabelle fehlgeschlagen")
EndIf
sql1.s="insert into adressen(Vorname,Name) VALUES('Dysti','Purebasic')"
DatabaseQuery(#Datenbank1, sql1)
sql1.s = "Select * From [adressen$]"
If DatabaseQuery(#Datenbank1, sql1) ; Ermittelt alle Einträge in der 'employee' Tabelle
Spalten = DatabaseColumns(#Datenbank1) - 1
temp.s = ""
For Spalte = 0 To Spalten
temp + DatabaseColumnName(#Datenbank1, Spalte) + ";"
Next
Debug temp
While NextDatabaseRow(#Datenbank1) ; alle Einträge durchlaufen
temp.s = ""
For Spalte = 0 To Spalten
temp + GetDatabaseString(#Datenbank1, Spalte) + ";"
Next
Debug temp
Wend
EndIf
Else
MessageRequester("","Konnte die Datenbank nicht öffnen")
EndIf
CloseDatabase(#Datenbank1)
EndIf
; DSN am Ende entfernen
RemoveDSN("Excel", path, "", "")