How do I connect to a MSSQL database using Perl's DBI module in Windows?

前端 未结 4 871
野的像风
野的像风 2020-12-03 22:02

How do I connect to a MSSQL database using Perl\'s DBI module in Windows?

相关标签:
4条回答
  • 2020-12-03 22:21

    Use DBD::ODBC. If you just create a data source with the Control Panel -> System Management -> ODBC Data Sources -> System Data Source or User Data Source (those are the names as I remember them, but my XP isn't in English, so I can't check), then all you have to do is use the name of that data source in the DBI connect string.

    my $dbh = DBI->connect("dbi:ODBC:$dsn", $user, $pwd, \%attr);
    

    The difference between User and System data source is that the latter is usable by any user.

    See also: HOW TO: Create a System Data Source Name in Windows XP

    0 讨论(0)
  • 2020-12-03 22:31

    Checking Perlmonks, I see the suggestion to actually use the Sybase DBI driver for connecting to MS SQL. Which makes sense, given that MS SQL has its origins in the Sybase code. ODBC works, too, of course.

    0 讨论(0)
  • 2020-12-03 22:36

    Couldn't find this anywhere reliable. Use Perl code similar to

    use DBI;
    my $dbs = "dbi:ODBC:DRIVER={SQL Server};SERVER={ServerName}";
    my ($username, $password) = ('username', 'password');
    
    my $dbh = DBI->connect($dbs, $username, $password);
    
    if (defined($dbh))
    {
        #write code here
        $dbh->disconnect;
    }
    else
    {
        print "Error connecting to database: Error $DBI::err - $DBI::errstr\n";
    }
    
    0 讨论(0)
  • 2020-12-03 22:38

    Using OLEDB with Integrated Security (Windows Authentication):

    DBI:ADO:Provider=SQLOLEDB.1;Integrated Security=SSPI;Data Source=localhost;Initial Catalog=$dbName;

    0 讨论(0)
提交回复
热议问题