Also, dieses hier kommt in ein MODUL!!!
Option Strict Off
Option Explicit On
Module Module1
' API-Deklaration
Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal _
hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, _
ByVal lpszAttributes As String) As Integer
' Konstanten
Private Const ODBC_ADD_DSN As Short = 1
Private Const ODBC_ADD_SYS_DSN As Short = 4
' DSN-Verbindung erstellen
Public Function AddDSN(ByVal sDSNName As String, ByVal sPath As String, _
Optional ByVal sDescription As String = "", Optional ByVal sDriverName As _
String = "Microsoft Access Driver (*.mdb)", Optional ByVal sUser As String = _
"", Optional ByVal sPwd As String = "", Optional ByVal bSystemDSN As Boolean _
= False) As Boolean
Dim nResult As Integer
Dim nRequest As Integer
Dim sAttr As String
' Benutzer-DSN oder System-DSN?
nRequest = IIf(bSystemDSN, ODBC_ADD_SYS_DSN, ODBC_ADD_DSN)
Dim sServer As String
If InStr(1, sDriverName, "SQL Server", CompareMethod.Text) > 0 Then
' SQL-Server
' Prüfen, ob Serverpfad angegeben...
If InStr(sPath, "\") > 0 Then
sServer = Left(sPath, InStr(sPath, "\") - 1)
sPath = Mid(sPath, InStr(sPath, "\") + 1)
Else
' Falls nicht, lokalen SQL-Server verwenden
sServer = "(local)"
End If
sAttr = "DSN=" & sDSNName & Chr(0) & "SERVER=" & sServer & Chr(0) & _
"DATABASE=" & sPath & Chr(0) & "TRUSTED_CONNECTION=True" & Chr(0) & _
"DESCRIPTION=" & sDescription & Chr(0)
Else
' Verbindungs-Attribute
sAttr = "DSN=" & sDSNName & Chr(0) & "DBQ=" & sPath & Chr(0) & "UID=" & _
sUser & Chr(0) & "PWD=" & sPwd & Chr(0) & "DESCRIPTION=" & sDescription & Chr( _
0)
End If
' DSN-Verbindung einrichten
nResult = SQLConfigDataSource(0, nRequest, sDriverName & Chr(0), sAttr)
AddDSN = (nResult = 1)
End Function
End Module Der Aufruf der Funktion erfolgt dann z. b. mittels eines Buttons von einer Form:
Option Strict Off
Option Explicit On
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs _
As System.EventArgs) Handles Command1.Click
Dim bResult As Boolean
bResult = AddDSN("MySQLDSN", "SERVER\KUNDEN", , "SQL Server")
If Not bResult Then MsgBox("DSN konnte nicht erstellt werden!")
End Sub
End ClassEALA FREYA FRESENA |