Results 1 to 9 of 9

Thread: making a system dsn w/ api

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Posts
    15

    making a system dsn w/ api

    How do you programmatically set up a system database connection through API

  2. #2
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    VB Code:
    1. Const ODBC_ADD_SYS_DSN = 4       'Add System data source
    2. Const ODBC_ADD_DSN = 1       'Add User data source
    3. Const ODBC_CONFIG_SYS_DSN = 5    'Configure (edit) data source
    4. Const ODBC_REMOVE_SYS_DSN = 6    'Remove data source
    5.  
    6. Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal _
    7.    hwndParent As Long, ByVal fRequest As Long, ByVal _
    8.    lpszDriver As String, ByVal lpszAttributes As String) As Long
    9.  
    10. Public Enum enumDSN_Type
    11.     eUserDSN = ODBC_ADD_DSN
    12.     eSystemDSN = ODBC_ADD_SYS_DSN
    13. End Enum
    14. Public Function CreateDSN(pDSNName As String, pDBPath As String, pDSNType As enumDSN_Type) As Boolean
    15.     Dim lngRetVal
    16.     Dim strDriver As String
    17.     Dim strAttributes As String
    18.    
    19.     CreateDSN = False
    20.      
    21.     strDriver = "Microsoft Access Driver (*.mdb)" & Chr(0)
    22.     strAttributes = "DSN=" & pDSNName & Chr(0)
    23.     strAttributes = Attributes & "Uid=Admin" & Chr(0) & "pwd=" & Chr(0)
    24.     strAttributes = Attributes & "DBQ=" & pDBPath & Chr(0)
    25.     lngRetVal = SQLConfigDataSource(0, pDSNType, strDriver, strAttributes)
    26.    
    27.     'If the Return Value 1 then
    28.     'DataSource is created succesefully
    29.     'otherwise it has falied
    30.     CreateDSN = lngRetVal
    31. End Function

    You can call this routine like this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Call CreateDSN("MyDSN", "C:\MyDB.mdb", eSystemDSN)
    3. End Sub

    This will create a SYstem DSN for Access database.
    By changing the driver type you can setup ODBC DSN for any type of database/server.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Posts
    15
    Ahh you are so awsome, thank you very much!!

  4. #4
    Member
    Join Date
    Dec 2001
    Posts
    41

    for remote database in sql server

    what do we need to change the attributes for sql database which resides in remote machine.

    I mean for client configuration we need to give the remote ip address in server textbox and network libraries we need to check tcp/ip
    to achieve this what we have to add in the attributes?

    Thanx in advance
    Anand

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Posts
    15
    All you have to add is the Server attribute. So in your attributes string you go...

    Attributes = Attributes & "Server=127.0.0.1" & Chr(0)
    Attributes = Attributes & "Database=The database which resides on sql server" & chr(0)

    Those two are the extra ones for doing remote..

  6. #6
    Member
    Join Date
    Dec 2001
    Posts
    41
    Hi, Thanx

    The second parameter in the following function what I should I replace in case of sqlserver


    Private Sub Command1_Click()
    Call CreateDSN("MyDSN", "C:\MyDB.mdb", eSystemDSN)
    End Sub
    Anand

  7. #7
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    We can change this function to accept both Access and SQL Server:
    VB Code:
    1. Const ODBC_ADD_SYS_DSN = 4       'Add System data source
    2. Const ODBC_ADD_DSN = 1       'Add User data source
    3. Const ODBC_CONFIG_SYS_DSN = 5    'Configure (edit) data source
    4. Const ODBC_REMOVE_SYS_DSN = 6    'Remove data source
    5.  
    6. Private Declare Function SQLConfigDataSource Lib "ODBCCP32.DLL" (ByVal _
    7.    hwndParent As Long, ByVal fRequest As Long, ByVal _
    8.    lpszDriver As String, ByVal lpszAttributes As String) As Long
    9.  
    10. Public Enum enumDSN_Type
    11.     eUserDSN = ODBC_ADD_DSN
    12.     eSystemDSN = ODBC_ADD_SYS_DSN
    13. End Enum
    14.  
    15. Public Enum enumDBType
    16.     eACCESS = 0
    17.     eSQL_SERVER = 1
    18. End Enum
    19.  
    20. Public Function CreateDSN(p_DSNName As String, p_DSNType As enumDSN_Type, _
    21.                     p_DBType As enumDBType, p_strDatabase As String, _
    22.                     Optional p_strServer As String = "", _
    23.                     Optional p_strUser As String = "", _
    24.                     Optional p_strPassword As String = "") As Boolean
    25.     Dim lngRetVal
    26.     Dim strDriver As String
    27.     Dim strAttributes As String
    28.    
    29.     CreateDSN = False
    30.      
    31.     Select Case p_DBType
    32.         Case eACCESS
    33.             strDriver = "Microsoft Access Driver (*.mdb)" & Chr$(0)
    34.             strAttributes = "DSN=" & p_DSNName & Chr$(0)
    35.             strAttributes = Attributes & "Uid=Admin" & Chr$(0) & "pwd=" & Chr$(0)
    36.             strAttributes = Attributes & "DBQ=" & pDBPath & Chr$(0)
    37.         Case eSQL_SERVER
    38.             strDriver = "SQL Server"
    39.             strAttributes = "SERVER=" & p_strServer & Chr$(0)
    40.             strAttributes = strAttributes & "DESCRIPTION=" & Chr$(0)
    41.             strAttributes = strAttributes & "DSN=" & p_DSNName & Chr$(0)
    42.             strAttributes = strAttributes & "DATABASE=" & p_strDatabase & Chr$(0)
    43.             strAttributes = strAttributes & "UID=" & p_strUser & Chr$(0)
    44.             strAttributes = strAttributes & "PWD=" & p_strPassword & Chr$(0)
    45.              
    46.     End Select
    47.  
    48.     lngRetVal = SQLConfigDataSource(0, p_DSNType, strDriver, strAttributes)
    49.    
    50.     'If the Return Value 1 then
    51.     'DataSource is created succesefully
    52.     'otherwise it has falied
    53.     CreateDSN = lngRetVal
    54. End Function
    Then you can call it like this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     Call CreateDSN("YourDSN", eSystemDSN, eSQL_SERVER, "YourDatabase", "ServerName", "sa")
    3. End Sub

  8. #8
    Member
    Join Date
    Dec 2001
    Posts
    41
    Thanx,
    it is working for local database.

    for rempote database what r the attributes to be add.

    suppose the remote machine ip =207.100.100.100
    server= server1
    database=data1
    uid=uid1
    pwd =pwd1

    I can create the dsn manually for remote database what I am doing is for in dsn creation wizard in client configuration I am giving as server 201.100.100.100

    before that I am giving server as server1 and network library tcp/ip checked.

    Now I need to create that with my vb program coz I can not go to all my clients place and create it manually.

    Please help me in this regard.

    Thanx in advance
    Anand

  9. #9
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744
    Just change the server name to use the IP instead.
    VB Code:
    1. Private Sub Command1_Click()
    2.     Call CreateDSN("YourDSN", eSystemDSN, eSQL_SERVER, "YourDatabase", "207.100.100.100", "UserName", "YourPassword)
    3. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width