JPro
JPro

Reputation: 6546

Connect to SQL Server from batch script

I am supplying a batch script to create a DSN connection in the user computer before they start to use my application. I am using this in a .bat file.

ODBCConf ConfigSysDSN "SQL Server" "DSN=CONNAME|SERVER=PCNAME\INSTANCENAME

But I want to make sure they will be able to connect to the database, considering the fact that proper drivers may not be installed in their systems. So, is there any way to check the connection from the same batch file and inform the user if something unable to connect to the database?

thanks.

Upvotes: 0

Views: 3484

Answers (2)

Alex K.
Alex K.

Reputation: 175748

You can use a script;

cscript test.js //nologo
if errorlevel == 1 @echo "FAILED"

where test.js is

var exitcode = 0;
try {
    var C = new ActiveXObject("ADODB.Connection");
    C.Open("DSN=THEDSN;Uid=???;Pwd=???");
    WScript.Echo("Connected!")
} catch (e) {
    WScript.Echo("Failed to connect: " + (e.message || "No details"))
    exitcode = 1;
} finally {
    (C.State == 1) && C.Close();
    C = null;
    WScript.Quit(exitcode);
}

Upvotes: 2

Preet Sangha
Preet Sangha

Reputation: 65476

You can use OSQL.EXE to make a connection to the database.

Upvotes: 0

Related Questions