Can I create a .bat file to automate Data Sources adding in ODBC Data Source Administrator?

StackOverflow https://stackoverflow.com/questions/23552529

Вопрос

I need for my work, in order to save time, to create a Macro that will allow me to add 3 data sources in ODBC Data Source Administrator automatically (I currently have to do it manually more than 10 times a day for these 3 data sources).

Unfortunately I can't use a (simple) program like SuperMacro since it requires developing a package for all the users and installing it on everyone's profile etc. And it would take months before being accepted by sooooo many different committees. (Big companies...)

My ANSWER: creating a .bat file! (?)

Now I am facing another problem; it is apparently impossible to send keystrokes from within the .bat file. I am not quite sure since I saw a few different opinions on the matter.

Anyway, it appears (if I read correctly) that I can then ask from the .bat file to send the command to a .vbs (?, correct me if I am wrong) that will send the needed keystroke.

(I may be a total noob on the subject but isn't it possible for me to do everything from the .vbs file without having to use a .bat file?)

. . .

To sum up, I need from a native readable/run-able file in windows 7 (NOT .exe) to be able to :

- open ODBC;

- to send "Tab", "Enter", "Up" & "Down", "Shift+Tab" and "Space" keytrokes;

- to enter "text" in appropriate places : Enter Text.png

- to select elements (by name) from a list : SourceData Selection.png

And then finally to quit ODBC with my new 3 added Data Sources ;)

. . .

Can anyone be so kind and help me with it :) ?

Or is there any other way of doing this :/ ?

. . .

I can send/post the exact requirements or the working example of the .mcr I used (at home) with SuperMacro to do what I need to do.

Thanks a lot for your help!

Best Regards,

Clamp77

Это было полезно?

Решение

Look up odbcconf for more info.

The format is: 
odbcconf type "DriverName" "DSN=DSNName|SERVER=(local)"

Here is how I have used it:
odbcconf configsysdsn "SQL Server" "DSN=NewDSN|SERVER=(local)"

In other words adding this command to your batch file
odbcconf configsysdsn "SQL Server" "DSN=NewDSN|SERVER=(local)"
will create a single new ODBC System DSN connection
on the local machine
Using the SQL Server driver named NewDSN.
Your .bat to make three connections could be simply ... 

odbcconf configsysdsn "SQL Server" "DSN=Name001|SERVER=(local)"
odbcconf configsysdsn "SQL Server" "DSN=Name002|SERVER=(local)"
odbcconf configsysdsn "SQL Server" "DSN=Name003|SERVER=(local)"

Другие советы

Try to

  1. export the registry branch of a manually created datasource
  2. copy/edit to get the entries for your 3 new datasources
  3. regedit /s them into the registry

see

Update with wrt @HDSSNET's answer (+1):

Everyone,

I managed to solve the problem, using only a .vbs file :) It does the job in less than 10 secs.

Here is the Script for the first Source:

Set sh = WScript.CreateObject("WScript.Shell")
sh.Run "odbcad32.exe", 9
WScript.Sleep 200
sh.SendKeys "{TAB}"
sh.SendKeys "{DOWN}"
sh.SendKeys "{UP}"
sh.SendKeys "{ENTER}"
WScript.Sleep 200
sh.SendKeys "Driver do Microsoft Access"
sh.SendKeys "{ENTER}"
sh.SendKeys "WKS"
sh.SendKeys "{TAB}"
WScript.Sleep 200
sh.SendKeys "NSBBL_ODBC_Setting"
sh.SendKeys "{TAB}"
sh.SendKeys "{ENTER}"
WScript.Sleep 200
sh.SendKeys "{TAB}"
sh.SendKeys "{TAB}"
sh.SendKeys "{TAB}"
sh.SendKeys "{TAB}"
sh.SendKeys "c"
sh.SendKeys "+{TAB}"
sh.SendKeys "+{TAB}"
WScript.Sleep 200
sh.SendKeys "{UP}"
sh.SendKeys "{UP}"
sh.SendKeys "{UP}"
sh.SendKeys "{UP}"
sh.SendKeys "{UP}"
sh.SendKeys "{UP}"
WScript.Sleep 200
sh.SendKeys "PROGRAMS"
sh.SendKeys "{ENTER}"
sh.SendKeys "{DOWN}"
sh.SendKeys "NatStar"
sh.SendKeys "{ENTER}"
sh.SendKeys "{DOWN}"
sh.SendKeys "NSBBL"
sh.SendKeys "{ENTER}"
sh.SendKeys "{DOWN}"
sh.SendKeys "MDB"
sh.SendKeys "{ENTER}"
sh.SendKeys "+{TAB}"
sh.SendKeys "{DOWN}"
WScript.Sleep 200
sh.SendKeys "{ENTER}"
sh.SendKeys "{TAB}"
WScript.Sleep 200
sh.SendKeys "{TAB}"
WScript.Sleep 200
sh.SendKeys "{TAB}"
WScript.Sleep 200
sh.SendKeys "{TAB}"
WScript.Sleep 200
sh.SendKeys "{TAB}"
sh.SendKeys "{ENTER}"

I know it has not necessary lines, but no worries :), I added the 2 other Sources behind it and it works perfectly !!!

It runs natively on Windows and can take any command: Keystrokes, text, etc.

Thanks a lot for your answers.

Clamp77

Try this. I use it regularly to create new dsn on my system for SQL SERVER

@echo off

set cn=%computername%
set host=%cn%\WINCC
ODBCCONF.exe /a {CONFIGSYSDSN "SQL Server" "DSN=XY|Description=Descriptionname|SERVER=%host%|Trusted_Connection=Yes|Database=XY"}

echo  SYSTEM DSN created successfuly...
pause
@CLS
@Exit
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top