Perl DBI SQL SERVER连接问题

时间:2013-11-25 19:42:57

标签: sql-server perl dbi

Perl非常新。我在尝试让DBI与SQL Server 2008 DB进行通信时遇到问题。

当我尝试使用ODBC或直接连接到SQL Servereveb时出现以下错误。

我是Perl的新手,有人可以帮助...谢谢

install_driver(MSSQL)失败:无法在C:/Perl/lib/DBD/MSSQL.pm第79行通过软件包“Class :: DBI :: MSSQL”找到对象方法“set_sql”。编译失败,要求在(评分19)C:/Perl/site/lib/DBI.pm:744第3行。

use strict; 
use warnings;
use diagnostics;
use Class::DBI::Loader;   

use DBI;

use File::Glob ':glob';


my $DBUserName                     = "*******";
my $DBPassword                     = "*******";
my $DBName                         = "dbi:MSSQL:uat-dbserver1";

my $dbh                         = "";
my $sqlStatement                 = "";
my $sqlCmd                         = "";

my @EasySetTableNames            = ();



$dbh = DBI->connect( $DBName, $DBUserName, $DBPassword,
    { PrintError => 0, AutoCommit => 0})
        || die "Database connection creation failed: $DBI::errstr\n";

$sqlStatement = "SELECT * from tableA ";
$sqlCmd = $dbh->prepare($sqlStatement);
$sqlCmd->execute();
@EasySetTableNames = @{$dbh->selectcol_arrayref($sqlStatement)};
print "hi";

并通过ODBC

#!/usr/bin/perl -w
use strict;

use DBI;

# Replace datasource_name with the name of your data source.
# Replace database_username and database_password
# with the SQL Server database username and password.
my $data_source = "dbi:MSSQL:test";
my $user = "test";
my $password = "test";

# Connect to the data source and get a handle for that connection.
my $dbh = DBI->connect($data_source, $user, $password)
    or die "Can't connect to $data_source: $DBI::errstr";

# This query generates a result set with one record in it.
my $sql = "SELECT 1 AS test_col";

# Prepare the statement.
my $sth = $dbh->prepare($sql)
    or die "Can't prepare statement: $DBI::errstr";

# Execute the statement.
$sth->execute();

# Print the column name.
print "$sth->{NAME}->[0]\n";

# Fetch and display the result set value.
while ( my @row = $sth->fetchrow_array ) {
   print "@row\n";
}

# Disconnect the database from the database handle.
$dbh->disconnect;

您可以提供任何帮助。

2 个答案:

答案 0 :(得分:1)

我通常在dbi中使用ODBC驱动程序,这就是我经常访问sql server的方法(2008 r2)

    #!/export/appl/pkgs/perl/5.8.4-fiq/bin/perl
    #!/usr/bin/perl
    #!/bin/sh

    use strict;
    use DBI;
    use Time::localtime;
    use Data::Dumper;

    my $dsn = 'DBI:ODBC:Driver={SQL Server}';
    my $host = 'xxx\yyy';
    my $database = 'testing';
    my $user = 'user';
    my $auth = 'password';


    my $dbh = DBI->connect("$dsn;Server=$host;Database=$database", $user, $auth) or die "Database connection not made: $DBI::errstr";

    my $sql = "EXECUTE database.schema.sproc";
    my $stmt = $dbh->prepare($sql);
    $stmt->execute();   
    $stmt->finish();
    $dbh->disconnect;

答案 1 :(得分:1)

我能够使用OLE32连接到SQL Server,这是一个代码示例... "Cursor type changed" error on Perl OLE32 MSSQL dateadd function results