成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

perl如何連接SQL Server數(shù)據(jù)庫(kù)

數(shù)據(jù)庫(kù)
本文將提供一些perl連接Microsoft SQL Server數(shù)據(jù)庫(kù)的實(shí)例。perl腳本運(yùn)行在Windows和Linux平臺(tái)。

  本文將提供一些perl連接Microsoft SQL Server數(shù)據(jù)庫(kù)的實(shí)例。perl腳本運(yùn)行在Windows和Linux平臺(tái)。

  Windows平臺(tái)

  如果在Windows平臺(tái)下運(yùn)行perl腳本,建議使用依賴DBI的兩個(gè)模塊包,提供標(biāo)準(zhǔn)的數(shù)據(jù)庫(kù)接口模塊。 DBD::ODBC DBD::ADO

  使用DBD::ODBC

  如果選用DBD::ODBC,下面的實(shí)例代碼將展示如何連接到SQL Server數(shù)據(jù)庫(kù): 

  1. use DBI;  
  2.   
  3. # DBD::ODBC  
  4.   
  5. my $dsn = 'DBI:ODBC:Driver={SQL Server}';  
  6. my $host = '10.0.0.1,1433';  
  7. my $database = 'my_database';  
  8. my $user = 'sa';  
  9. my $auth = ‘s3cr3t';  
  10.   
  11. Connect via DBD::ODBC by specifying the DSN dynamically.  
  12. my $dbh = DBI->connect("$dsn;Server=$host;Database=$database",  
  13.  $user,  
  14.  $auth,  
  15.  { RaiseError => 1, AutoCommit => 1}  
  16.  ) || die "Database connection not made: $DBI::errstr";  
  17.   
  18. #Prepare a SQL statement my $sql = "SELECT id, name, phone_number FROM employees ";  
  19. my $sth = $dbh->prepare( $sql );  
  20.   
  21. #Execute the statement  
  22. $sth->execute();  
  23.   
  24. my( $id, $name, $phone_number );  
  25.   
  26. # Bind the results to the local variables  
  27. $sth->bind_columns( undef, \$id, \$name, \$phone_number );  
  28.   
  29. #Retrieve values from the result set  
  30. while( $sth->fetch() ) {  
  31.  print "$id, $name, $phone_number\n";  
  32. }  
  33.   
  34. #Close the connection  
  35. $sth->finish();  
  36. $dbh->disconnect(); 

 

  你還可以使用預(yù)先設(shè)置的一個(gè)系統(tǒng)DSN來(lái)連接。要建立一個(gè)系統(tǒng)DSN,可以這樣訪問(wèn)控制面板->管理工具->數(shù)據(jù)源。 使用系統(tǒng)DSN連接,需要更改連接字符串。如下所示:

 

  1. Connect via DBD::ODBC using a System DSN  
  2. my $dbh = DBI->connect("dbi:ODBC:my_system_dsn",  
  3.  $user,  
  4.  $auth,  
  5.  {  
  6.  RaiseError => 1,  
  7.  AutoCommit => 1  
  8.  }  
  9.  ) || die "Database connection not made: $DBI::errstr"

 

  使用DBD::ADO

  如果選擇DBD::ADO模塊,下面的實(shí)例展示如何連接到SQL Server數(shù)據(jù)庫(kù)。

 

  1. use DBI;  
  2.   
  3. my $host = '10.0.0.1,1433';  
  4. my $database = 'my_database';  
  5. my $user = 'sa';  
  6. my $auth = ‘s3cr3t';  
  7.   
  8. # DBD::ADO  
  9. $dsn = "Provider=sqloledb;Trusted Connection=yes;";  
  10. $dsn .= "Server=$host;Database=$database";  
  11. my $dbh = DBI->connect("dbi:ADO:$dsn",  
  12.  $user,  
  13.  $auth,  
  14.  { RaiseError => 1, AutoCommit => 1}  
  15.  ) || die "Database connection not made: $DBI::errstr";  
  16.   
  17. #Prepare a SQL statement  
  18. my $sql = "SELECT id, name, phone_number FROM employees "; my $sth = $dbh->prepare( $sql );  
  19.   
  20. #Execute the statement  
  21. $sth->execute();  
  22.   
  23. my( $id, $name, $phone_number );  
  24.   
  25. # Bind the results to the local variables  
  26. $sth->bind_columns( undef, \$id, \$name, \$phone_number );  
  27.   
  28. #Retrieve values from the result set  
  29. while( $sth->fetch() ) {  
  30.  print "$id, $name, $phone_number\n";  
  31. }  
  32.   
  33. #Close the connection  
  34. $sth->finish();  
  35. $dbh->disconnect(); 

 

  Linux平臺(tái)

  如果是在Linux平臺(tái)下運(yùn)行perl腳本,連接SQL Server數(shù)據(jù)庫(kù)需要使用到DBD::Sybase包。

  安裝SQL Server支持庫(kù)

  Sybase DBD包依賴FreeTDS驅(qū)動(dòng)程序。 FreeTDS下載地址:www.freetds.org 安裝FreeTDS驅(qū)動(dòng)的說(shuō)明文檔參見(jiàn):http://www.freetds.org/userguide/config.htm 該驅(qū)動(dòng)沒(méi)有使用到ODBC.

  配置數(shù)據(jù)源

  修改freetds.conf文件包括SQL Server數(shù)據(jù)庫(kù)信息,如下所示: [SS_MY_DB] host = 10.0.0.1 # or host name port = 1433 tds version = 7.0

  安裝Sybase DBD模塊

  該模塊文檔參見(jiàn):http://search.cpan.org/~mewp/DBD-Sybase/Sybase.pm 此外,需要將sybase環(huán)境變量應(yīng)設(shè)置為FreeTDS安裝路徑,export SYBASE=/usr/local/freetds

  使用Sybase DBI和SQL Server DSN實(shí)例

 

  1. load the DBI module  
  2. use DBI;  
  3. use DBD::Sybase;  
  4.   
  5. my $database="my_database";  
  6. my $user="sa";  
  7. my $auth="s3cr3t";  
  8.   
  9. BEGIN  
  10. {  
  11.  $ENV{SYBASE} = "/usr/local";  
  12. }  
  13.   
  14. Connect to the SQL Server Database  
  15. my $dbh = DBI->connect("dbi:Sybase:server=ss_my_db;database=$database",  
  16.  $user,  
  17.  $auth  
  18.  {RaiseError => 1, AutoCommit => 1}  
  19.  ) || die "Database connection not made: $DBI::errstr";  
  20.   
  21. #Prepare a SQL statement  
  22. my $sql = "SELECT id, name, phone_number FROM employees";  
  23. my $sth = $dbh->prepare( $sql );  
  24.   
  25. #Execute the statement  
  26. $sth->execute();  
  27.   
  28. my( $id, $name, $phone_number );  
  29.   
  30. # Bind the results to the local variables  
  31. $sth->bind_columns( undef, \$id, \$name, \$phone_number );  
  32.   
  33. #Retrieve values from the result set  
  34. while( $sth->fetch() ) {  print "$name, $title, $phone\n";  
  35. }  
  36.   
  37. #Close the connection  
  38. $sth->finish();  
  39. undef $sth; # This fixes a segfault bug with certain versions of DBD::Sybase  
  40. $dbh->disconnect(); 

 

責(zé)任編輯:honglu 來(lái)源: 內(nèi)存溢出
相關(guān)推薦

2010-07-15 17:28:50

SQL Server

2011-08-09 09:31:39

SQL Server數(shù)connectionS

2009-07-07 17:42:28

2009-08-03 14:17:18

C#連接AccessC#連接SQL Ser

2010-11-02 11:49:18

SQL SERVER連

2009-06-03 10:51:59

連接SQL數(shù)據(jù)庫(kù)Adobe Dream

2011-05-20 13:11:22

ADO.NET

2009-11-12 11:23:35

ADO.NET SQL

2010-10-26 15:54:02

連接oracle數(shù)據(jù)庫(kù)

2010-07-01 15:02:29

SQL Server數(shù)

2010-09-13 15:55:17

SQL Server數(shù)

2010-11-08 16:04:06

SQL SERVER連

2010-11-10 09:44:31

SQL Server端

2021-05-17 06:57:34

SQLServer數(shù)據(jù)庫(kù)

2010-07-08 11:05:14

SQL Server數(shù)

2011-07-28 11:44:46

SQL Server數(shù)合并表格數(shù)據(jù)

2009-04-22 09:42:07

SQL Server監(jiān)視鏡像

2011-03-28 15:28:03

SQL Server 數(shù)據(jù)庫(kù)

2010-06-18 10:20:22

SQL Server

2011-04-02 10:47:51

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 久久青视频 | 免费在线看黄 | 五月天天丁香婷婷在线中 | 亚洲国产黄 | 在线播放国产一区二区三区 | 国产美女一区二区三区 | 99精品欧美一区二区三区综合在线 | 国产欧美精品一区二区色综合朱莉 | 北条麻妃一区二区三区在线观看 | 久久免费精品 | 久久青| 亚洲精品日韩综合观看成人91 | 高清av电影 | 亚洲国产专区 | 午夜久久久久久久久久一区二区 | 精品久久久久久久久久久 | 久久夜夜| 日韩av福利在线观看 | 视频一区二区国产 | 国产一区二区在线免费播放 | 国产精品v | 羞羞视频免费在线观看 | 羞羞午夜 | 精品视频在线观看 | 免费激情网站 | 中文字幕在线观看一区二区 | 国产乱码精品一区二区三区忘忧草 | 亚洲综合大片69999 | 一级黄色片免费在线观看 | 欧美精品久久久 | 国产黄色在线 | 国产精品激情在线 | 一级大片网站 | 美女久久视频 | 亚洲三区在线观看 | 中文字幕不卡 | av毛片| 欧美精品福利视频 | 亚洲日日操| 夜夜艹 | 色婷婷综合网站 |