Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Setup DSN programmatically on the fly

Setup DSN programmatically on the fly

Scheduled Pinned Locked Moved Visual Basic
helpdatabasevisual-studiosysadminquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KreativeKai
    wrote on last edited by
    #1

    Hi all, Hope someone can help with this... I wrote this code several years ago and it has worked great in XP. Our department is rolling out Windows 7 and the agency that uses the app I wrote are finally receiving their new Windows 7 systems. One problem... code doesn't work any longer. The code sets up a DSN entry on the fly. In XP, a standard user without any elevated rights could run the app and this code worked. Now in Windows 7 when installing, I have the end user setup as local administrator and the app fails to Create the DSN. If I go in and right click on the "Click-Once" setup.exe and select "Run as administrator" it works. Also if I create a shortcut to the setup.exe and change the advanced settings to "Run as administrator" it also works from that shortcut. If I use the icon created by the program from the "Start" menu or from the desktop, which doesn't have an option to "Run as administrator", even though the client is setup as local administrator, it kicks an error that it can't create the DSN. I can delete the DSN and see when it is created and when it can't and it coincides with the trials I've listed above. Is there something I can put in code that allows it to work no matter if administrator or if a standard user, like it did in XP? Some google posts talk about UAC causing the problem, but just say they recommend to run as administrator. I've searched google and code project, and most articles / posts seem to be from 2004 or around that time. We're using VS 2010... Is there a better way to creat the DSN's on the fly with VS 2010? :confused: Here is the code Public Declare Auto Function SQLConfigDataSource Lib "ODBCCP32.DLL" _ (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer Private Const ODBC_ADD_SYS_DSN As Integer = 4 Friend Shared Sub Create_DSN_For_Crystal_Reports() Try Dim attributes As New System.Text.StringBuilder() Dim returnCode As Integer attributes.Append("DSN=CCPROD") attributes.Append(Chr(0)) attributes.Append("Server=CCPROD") attributes.Append(Chr(0)) attributes.Append("Description=DSN added via code from Beneficiary System") attributes.Append(Chr(0)) attributes.Append("Database=HrPr") attributes.Append(Chr(0)) attributes.Append("AnsiNPW=Yes") attributes.Append(Chr(0)) attributes.Append("QuotedId=Yes") attributes.Append(Chr(0)) attributes.Append("Trusted_Connection=Yes") att

    D 1 Reply Last reply
    0
    • K KreativeKai

      Hi all, Hope someone can help with this... I wrote this code several years ago and it has worked great in XP. Our department is rolling out Windows 7 and the agency that uses the app I wrote are finally receiving their new Windows 7 systems. One problem... code doesn't work any longer. The code sets up a DSN entry on the fly. In XP, a standard user without any elevated rights could run the app and this code worked. Now in Windows 7 when installing, I have the end user setup as local administrator and the app fails to Create the DSN. If I go in and right click on the "Click-Once" setup.exe and select "Run as administrator" it works. Also if I create a shortcut to the setup.exe and change the advanced settings to "Run as administrator" it also works from that shortcut. If I use the icon created by the program from the "Start" menu or from the desktop, which doesn't have an option to "Run as administrator", even though the client is setup as local administrator, it kicks an error that it can't create the DSN. I can delete the DSN and see when it is created and when it can't and it coincides with the trials I've listed above. Is there something I can put in code that allows it to work no matter if administrator or if a standard user, like it did in XP? Some google posts talk about UAC causing the problem, but just say they recommend to run as administrator. I've searched google and code project, and most articles / posts seem to be from 2004 or around that time. We're using VS 2010... Is there a better way to creat the DSN's on the fly with VS 2010? :confused: Here is the code Public Declare Auto Function SQLConfigDataSource Lib "ODBCCP32.DLL" _ (ByVal hwndParent As Integer, ByVal fRequest As Integer, ByVal lpszDriver As String, ByVal lpszAttributes As String) As Integer Private Const ODBC_ADD_SYS_DSN As Integer = 4 Friend Shared Sub Create_DSN_For_Crystal_Reports() Try Dim attributes As New System.Text.StringBuilder() Dim returnCode As Integer attributes.Append("DSN=CCPROD") attributes.Append(Chr(0)) attributes.Append("Server=CCPROD") attributes.Append(Chr(0)) attributes.Append("Description=DSN added via code from Beneficiary System") attributes.Append(Chr(0)) attributes.Append("Database=HrPr") attributes.Append(Chr(0)) attributes.Append("AnsiNPW=Yes") attributes.Append(Chr(0)) attributes.Append("QuotedId=Yes") attributes.Append(Chr(0)) attributes.Append("Trusted_Connection=Yes") att

      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      I really have to ask why you're even bothering with a DSN?? Do you know what a DSN is?? It's a connection string in a text file. Since your code can use a connection string directly without the DSN, why are you even bothering with it?

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak

      K 1 Reply Last reply
      0
      • D Dave Kreskowiak

        I really have to ask why you're even bothering with a DSN?? Do you know what a DSN is?? It's a connection string in a text file. Since your code can use a connection string directly without the DSN, why are you even bothering with it?

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        K Offline
        K Offline
        KreativeKai
        wrote on last edited by
        #3

        The DSN is being setup for the Crystal reports that are initiated by the application.

        Lost in the vast sea of .NET

        D 1 Reply Last reply
        0
        • K KreativeKai

          The DSN is being setup for the Crystal reports that are initiated by the application.

          Lost in the vast sea of .NET

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          OK. Crystal Reports doesn't exactly require using a DSN either, but whatever. By default, users on Vista and 7 run as normal users, even if they are Admin accounts. UAC will usually prompt for admin creds if an app requires admin level priv's. Add a manifest to your application and modify it to require admin priv's when it runs. See this[^] for the full details. Warning! If UAC is turned on on Vista and 7, every time your app runs, it will ask for the users credentials or at least warn them that the app will be running as an admin.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          K 1 Reply Last reply
          0
          • D Dave Kreskowiak

            OK. Crystal Reports doesn't exactly require using a DSN either, but whatever. By default, users on Vista and 7 run as normal users, even if they are Admin accounts. UAC will usually prompt for admin creds if an app requires admin level priv's. Add a manifest to your application and modify it to require admin priv's when it runs. See this[^] for the full details. Warning! If UAC is turned on on Vista and 7, every time your app runs, it will ask for the users credentials or at least warn them that the app will be running as an admin.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            K Offline
            K Offline
            KreativeKai
            wrote on last edited by
            #5

            Thanks Dave! This worked very well in Windows 7 without breaking the app for my XP clients. I was looking at your profile. Congratulations on all the MVP awards over the years! I don't post much on Codeproject, just a couple questions and a few answers a year, but I believe you've probably answered most of my posts. I see you hit 9 years as of today, March 12th, Congratulations... Thanks again for your help! :)

            Lost in the vast sea of .NET

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups