18. december 2001 - 16:20Der er
4 kommentarer og 1 løsning
Oprette System DSN i runtime
Hej forum.
Jeg sidder og skal lave en installationspakke, og vil gerne have, at den under installationen opretter de DSN\'er der skal til for at skabe forbindelse til databasen.
MS Visual Studio kommer med en DLL der hedder ODBCTool.dll, som har en enkelt klasse med funktionen CreateDSN. Den virker nem at benytte, men virker umiddelbart ikke. Endvidere kan jeg ikke finde noget dokumentation nogen steder.
Har nogen en metode til at oprette et DSN? Jeg skal kunne angive både brugernavn og password samt hvorvidt DSN\'et skal placeres i henholdsvis UserDSN eller SystemDSN. Eller har nogen med held haft held til at benytte ODBCTool.CreateDSN?
Her er en anden metode, men én hvor jeg ikke kan få indstillet de parametre jeg vil:
********************* Private Const ODBC_ADD_DSN = 1 \' Add data source Private Const ODBC_CONFIG_DSN = 2 \' Configure (edit) data source Private Const ODBC_REMOVE_DSN = 3 \' Remove data source Private Const vbAPINull As Long = 0 \' NULL Pointer
\'Function Declare #If Win32 Then
Private Declare Function SQLConfigDataSource Lib \"ODBCCP32.DLL\" _ (ByVal hwndParent As Long, ByVal fRequest As Long, _ ByVal lpszDriver As String, ByVal lpszAttributes As String) _ As Long #Else Private Declare Function SQLConfigDataSource Lib \"ODBCINST.DLL\" _ (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal _ lpszDriver As String, ByVal lpszAttributes As String) As Integer #End If
Private Sub Command2_Click() #If Win32 Then Dim intRet As Long #Else Dim intRet As Integer #End If Dim strDriver As String Dim strAttributes As String
\'Set the driver to SQL Server because it is most common. strDriver = \"SQL Server\" \'Set the attributes delimited by null. \'See driver documentation for a complete \'list of supported attributes. strAttributes = \"SERVER=ebdb02\" & Chr$(0) strAttributes = strAttributes & \"DESCRIPTION=Temp DSN\" & Chr$(0) strAttributes = strAttributes & \"DSN=DSN_TEMP\" & Chr$(0) strAttributes = strAttributes & \"DATABASE=Diverse\" & Chr$(0) \'To show dialog, use Form1.Hwnd instead of vbAPINull. intRet = SQLConfigDataSource(vbAPINull, ODBC_ADD_DSN, _ strDriver, strAttributes) If intRet Then MsgBox \"DSN Created\" Else MsgBox \"Create Failed\" End If End End Sub
I would think that the easiest way is to EXPORT the REGISTRY settings fot the DSN from this you get a .REG file. Then from VB call the ,REG file using
SHELLEXEC
Place this in a module: Public Declare Function ShellExecute Lib \"shell32.dll\" Alias \"ShellExecuteA\" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Public Const SW_HIDE = 0 Public Const SW_SHOW = 5
Place this in a function: Dim result As Integer
result = ShellExecute(0, \"Open\", \"C:\\temp\\Your.reg\", \"\", \"\", SW_HIDE)
Jeg har selv fundet ud af at tilrette koden fra den kommentar jeg lagde 18/12 2001 16:26:23.
Hvis man vil lave et System DSN, er det konstanten:
Private Const ODBC_ADD_DSN = 1
- der skal rettes til 4. 1 laver et User DSN. Det gode ved metoden er som jeg ser det, at den melder tilbage hvorvidt DSN\'et er blevet oprettet eller ej.
Mads
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.