Archive: DSN Creation


DSN Creation
  Hi.. i am distributing a software which uses a database and would like to know the procedure of creating a User DSN using NSIS. With the kind of features that NSIS has, I am sure this can be achieved somehow...Could somebody pls guide me in this regards.

Thanx
Ramesh


Well, I am pretty sure my dictionary was wrong when it gave me "Deep Space Network" as the defenition for DSN... Could we please explain what DSN is?

A few ideas even though I have no idea what you want :D :
1) Create an extension DLL that creates this user DSN.
2) Call some commands of the database interface using Exec/ExecWait.
3) Using SendMessage send it WM_CREATE or something :)
4) Use FileOpen/FileRead/FileWrite/FileClose to create it.


:rolleyes: that's a tough one for a starter like me. Any more ideas? :D


Hi Ramesh,

You can setup a access DSN using....

WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "DBQ" "c:\mydatabase.mdb"
WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "Description" "Description"
WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "Driver" "C:\\WINNT\\System32\\odbcjt32.dll"
WriteRegDword HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "DriverId" "00000019"
WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "FIL" "MS Access;"
WriteRegDword HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "SafeTransactions" "00000000"
WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "UID" ""

WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME\Engines\Jet" "ImplicitCommitSync" "Yes"
WriteRegDword HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME\Engines\Jet" "MaxBufferSize" "00000200"
WriteRegDword HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME\Engines\Jet" "PageTimeout" "00000005"
WriteRegDword HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME\Engines\Jet" "Threads" "00000003"
WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME\Engines\Jet" "UserCommitSync" "Yes"
WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\ODBC Data Sources" "DSNNAME" "Microsoft Access Driver (*.mdb)"


or an SQL one using .....

WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "Driver" "C:\\WINNT\\System32\\SQLSRV32.dll"
WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "Server" "Server Name"
WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "Database" "db on sql server"
WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODBC.INI\DSNNAME" "LastUser" "sa"
WriteRegStr HKEY_LOCAL_MACHINE "Software\ODBC\ODB.INI\ODBC Data Sources" "DSNNAME" "SQL Server"


hope this helps
ps you can look in your registry to work out other types


Well, if you can tell me what DSN is I will able to be a bit more specific :)


A DSN is an ODBC data source


btw, there is a bit more natural way:

!define ODBC_ADD_DSN 1

>!define ODBC_ADD_SYS_DSN 4
system::call 'odbccp32.dll::SQLConfigDataSourceA(i 0, i ${ODBC_ADD_DSN}, t "driver", t "attributes") i'
MSDN contains full desctiption of SQLConfigDataSource.

Originally posted by kichik
Well, I am pretty sure my dictionary was wrong when it gave me "Deep Space Network" as the defenition for DSN... Could we please explain what DSN is?(...)
Originally posted by kichik
Well, if you can tell me what DSN is I will able to be a bit more specific :)
Originally posted by Mark_Ive
A DSN is an ODBC data source
As far as I can tell (not being a DSN expert), is that a DSN is an adress to a data source, for example, a MySQL database. Just like an IP-adress for websites. (I might be wrong, then please correct me, brainsucker, Mark_Ive or vrtata) :)

In Windows, there is an "ODBC Data Sources" control panel that allows you to add addressing to databases, so I could setup a MySQL database with a username/password and the connect info and then within my app simply say "Connect to MyDB" where "MyDB" is the "DSN" name for my database. I can do this for anything, Excel files, MS SQL databases, Access, etc. Just makes accessing the database a little easier.

I believe what Mark_Ive said is correct...all that data is stored in the registry so you can simply add the entries there and it should take care of it.


Using System.dll to make calls to odbccp32.dll::SQLConfigDataSourceA
  Brainsucker,

You mentioned that a more natural way to install an ODBC connection was to use system::call 'odbccp32.dll::SQLConfigDataSourceA(...)'
but this doesn't seem to work. I can get it to work from a 5 line c++ program (same parameters to call) but not from NSIS. Just wondering if you had any insight into this - otherwise I'm banging on the registry.

Thanks in advance.

Artificial


ODBC problems
  Unfortunately I'm having all sorts of problems trying to set up a working DSN. I was able to set up a working SQL Server ODBC DSN using nsODBC. But if I try to set up two ODBC links in the one installation, then only the first one will work. I then reverted to the direct registry method, which seems to create dsns that work, but they're not showing up on the list under Administrative Tools. But when attempting to create a dsn with the same name an alert comes up saying that the dsn already exists. I also looked into the system call mentioned above, but I didn't have any luck with that either (I'm not sure how to give all the connection properties, do the null delimiters work?)

Can anybody shed any light on any of the above? Something that I would have thought would be quite simple has already taken up a lot of my time and I'm still not able to get it right.


I've been hacking up quick installs for MS Access databases for a while now, and here's the code I'm using.

I know this isn't too different from Mark_Ive's earlier post, but it may clear up a couple of additional questions.

At the start of the script, this define just saves a lot of copying and pasting.

The ODBC_CONNECTION_NAME we're adding here is the DSN - this is what will appear in the ODBC Administrator and all of your apps.

!define ODBC_CONNECTION_NAME "BaB"


Later on, I add the following. Obviously your "Driver", "Description", and "DBQ" settings will change with each different Access connection. For databases other than Access, just take a look in your registry and steal the appropriate settings.

The first line in the code below is what causes the DSN to appear in the ODBC Administrator. I noticed this was in the earlier post, but maybe this was missing from yours gold0r?

Also, replace HKLM below with HKCU to create a "User DSN" instead of a "System DSN".

  ; Write the ODBC database connection for Windows

WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources" "${ODBC_CONNECTION_NAME}" "Microsoft Access Driver (*.mdb)"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "FIL" "MS Access;"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "Driver" "$SYSDIR\odbcjt32.dll"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "Description" "GBIS Back and Beyond Demo Dataset"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "UID" ""
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "DBQ" "$INSTDIR\BackAndBeyond.mdb"
WriteRegDWord HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "SafeTransactions" "00000000"
WriteRegDWord HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "DriverID" "00000019"
WriteRegDWord HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}\Engines\Jet" "Threads" "00000003"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}\Engines\Jet" "ImplicitCommitSync" ""
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}\Engines\Jet" "UserCommitSync" "Yes"
WriteRegDWord HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}\Engines\Jet" "PageTimeout" "00000005"
WriteRegDWord HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}\Engines\Jet" "MaxBufferSize" "00000800"


Finally, in the cleanup:

  DeleteRegKey "HKLM" "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}"
DeleteRegValue "HKLM" "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources" "${ODBC_CONNECTION_NAME}"


Your mileage may vary, but I've been using this method quite successfully on all of the NT-based boxes here at the office.

I'm not sure whether this works with 9x kernels, but I can't think of any reason offhand why it wouldn't.

And the same again for SQL Server:

In the defines we'll set the DSN name again:

!define ODBC_CONNECTION_NAME "MSPubs"


If anyone's screwing around with SQL Express 2005, you may need to axe the "Database" section and use "localhost\pubs" (or whatever) in the "Server" section instead. But you can find all that out by checking your own registry anyway.

  WriteRegStr   HKLM "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources" "${ODBC_CONNECTION_NAME}" "SQL Server"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "Description" "MS Demo Database"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "Driver" "$SYSDIR\SQLSRV32.dll"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "LastUser" "bob"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "Server" "localhost"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "Database" "pubs"


And in the uninstaller:

  DeleteRegKey "HKLM" "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}"
DeleteRegValue "HKLM" "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources" "${ODBC_CONNECTION_NAME}"

Hi all, I have some problems with my DSN creation. I used the examples above to try to create a DSN and I do not have any errors when compiling. But when I try to launch my program, I get prompted a message saying that DSN could not be found. I have posted my script below. I hope it explains my problem better. I am trying to create a DSN for Firebird database. Thanks!


I'm so sorry. I posted the wrong script above and have removed it. The right one is below. Thanks once again!


Hi I would really appreciate it if someone helped me out here. I only need this last part to work for my installer. Once this DSN creation part is done I will no longer bug anyone on the forum ;) . Thank you!


Hi, I still have not been able to solve my problem which I stated above. To make thigs easier for all I am gonna paste my script in this post. If anyone ses any fault please tell me. Btw, Friday (tomorrow) is my last day for my attachment so I hope someone would reply me by then.

Here's the code:

WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources" "${ODBC_CONNECTION_NAME}" "Firebird Server"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "FIL" "Firebird;"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "Driver" "$SYSDIR\IB6ODBC.dll"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "Description" "Easy Soft IB6ODBC"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "UID" ""
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "DBQ" "$INSTDIR\QSystem_ANSI.gdb"
WriteRegDWord HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "SafeTransactions" "00000000"
WriteRegDWord HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}" "DriverID" "00000019"
WriteRegDWord HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}\Engines\Jet" "Threads" "00000003"
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}\Engines\Jet" "ImplicitCommitSync" ""
WriteRegStr HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}\Engines\Jet" "UserCommitSync" "Yes"
WriteRegDWord HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}\Engines\Jet" "PageTimeout" "00000005"
WriteRegDWord HKLM "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}\Engines\Jet" "MaxBufferSize" "00000800"


; For the delete section of the DSN
DeleteRegKey "HKLM" "SOFTWARE\ODBC\ODBC.INI\${ODBC_CONNECTION_NAME}"
DeleteRegValue "HKLM" "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources" "${ODBC_CONNECTION_NAME}"

Thanks in advance!


Ok I know I must be pissing a lot of people in this forum now, and I apologise for that. I just want to know must I have a ReadRegStr command before I start to write the registry strings for the dsn?

Thanks.


I suggest you try brainsucker's method. If there is an API function for this, why use the registry directly?


Hi All,
I just want to make a small note here... the solution of writing to registry given by Domgrief works absolutely fine, but just a small correction in the Driver ID dWord value should be "0x00000019" instead of "00000019" though this does not give any error it works fine it allows to connect to the DB as well, but if you go and check in the ODBC Administrator via control panel you wont see any database file there... after putting this string it works fine.

thanks
arif


The other DWORD values should have 0x in front of them too. As you can imagine, "00000019" will be read by NSIS as decimal 1 and not 25 (0x00000019).

Stu