Hi,
I tried that before but it doesn't change anything. Interestingly if I exclude the UID and PWD and just use
$conn->Open("DSN=$dsn");
I get
Cannot connect to MyDSN : OLE exception from "Microsoft OLE DB Provide
+r for ODBC Drivers" :
[Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user '
+(null)'. Reason: Not associated with a trusted SQL Server connection.
Win32::OLE(0.1502) error 0x80004005: "Unspecified error"
in METHOD/PROPERTYGET "Open" at adodb.pl line 12.
What am I supposed to have when calling the open routine? Are there other parameters that I should/can use?
I've set up the System DSN with the correct username/password - the ODBC control panel can connect successfully.
Maybe this is more of a Windows question than a Perl one. Any ideas?
Andrew
UPDATE : apparently this is something to do with the server being set up for Windows authentication only, and not Mixed Mode authentication. But why can my ODBC control panel connect OK, but not my Perl script? | [reply] [d/l] [select] |
Well, is it necessary to use a DSN connection? With ADO it's possible to use DSN-less connections. Here's a connection string I use regularly (needs MDAC 2.6+).
my $ConnStr = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Se
+curity Info=False;Data Source=hostname;Initial Catalog=Database;";
my $AdoConn = new Win32::OLE("ADODB.Connection");
$AdoConn->Open($ConnStr);
If you need to specify a user, here is the syntax:
my $ConnStr = "Provider=SQLOLEDB.1;User Id=user; Password=password; Da
+ta Source=hostname; Initial Catalog=Database;";
--
The ability to destroy planets is insignificant next to the power of the Deacon Effect.
| [reply] [d/l] [select] |
my $connect_str = "Provider=SQLOLEDB.1;" .
"User Id=$uid;Password=$pwd;" .
"Data Source=$svr;" .
"Initial Catalog=$db";
my $conn = new Win32::OLE('ADODB.Connection')
or die "Cannot make ADODB connection : " . Win32::OLE->LastError;
$conn->Open($connect_str)
or die "Cannot connect to $db on $svr : " . Win32::OLE->LastError;
$conn->Close;
...but I'm still getting the same problem...
Cannot connect to $db on $svr : 0 at adodb.pl line 22
I tried using Win32::ODBC to connect, which I got working ok, but this method doesn't support stored procedures with output parameters, which is what I really need to do. I'm looking into DBI too.
Anyone got any ideas how to do this?
| [reply] [d/l] [select] |
Thanks for your help, but I got it working a different way - using DBI.
For anyone who's interested - it seems difficult to find info on accessing MS SQL Server from Perl. I used DBI and DBD::ODBC - I was able to get a connection going with Win32::ODBC and RDBAL but wasn't sure about running stored procedures with these methods.
Here's the code...note you need to install DBI, DBD::ODBC and set up a system DNS to connect to database from your ODBC data sources control panel - there may be a way to get a DSN-less connection though. Also, you must initialise any parameter being bound to a placeholder and used as an output parameter to the stored procedure to some valid value of the expected datatype. If you're expecting a string then initalise to ' ' or other string value. If you're getting some datatype conversion error ("error converting data type ... to ..."), then this could be the problem.
use DBI;
use DBI qw(:sql_types);
my $MSSQL_uid = 'myuid';
my $MSSQL_pwd = 'mypwd';
my $MSSQL_svr = 'mysqlsvr';
my $MSSQL_db = 'mydb';
my $MSSQL_dsn = "dbi:ODBC:$MSSQL_db";
my $sth; # statement handle
# connect to DB
my $dbh = DBI->connect($MSSQL_dsn, $MSSQL_uid, $MSSQL_pwd)
or die "Couldn't connect to $MSSQL_svr : $DBI::errstr\n";
print "\n\nConnected to $MSSQL_dsn DS\n\n";
# execute stored procedures and get output parameter value
my $param1_int = 0; # note you must initalise this to something
my $exe_string = "EXECUTE my_stored_procedure" .
"?, " .
"'$param2_str', " .
"'$param3_str', " .
"$param4_int";
$sth = $dbh->prepare($exe_string)
or die "\nCouldn't prepare statement : $DBI::errstr\n";
$sth->bind_param_inout(1, \$param1_int, 50)
or die "\nCouldn't bind param inout : $DBI::errstr\n";
$sth->execute
or die "\nCouldn't execute stored procedure : $DBI::errstr\n";
print "Output parameter value is now $param1_int\n";
# disconnect from DB
$dbh->disconnect
or die "\nError disconnecting : $DBI::errstr\n";
Andrew | [reply] [d/l] |
What error do you get using the code I suggested ? poj
| [reply] |
I got the same error again with your code.
Andrew
| [reply] |