[英]How can I specify the port number with DBD::ODBC?
I currently use the following code to connect to a database in my Perl script: 我目前在Perl脚本中使用以下代码连接到数据库:
my $dsn = 'dbi:ODBC:MYDATABASE';
my $database = 'uat_env';
my $user = 'user';
my $auth = 'password';
my $dbh = DBI->connect($dsn, $user, $auth, {
RaiseError => 1,
AutoCommit => 1
}) or die("Couldn't connect to database");
$dbh->do('use '.$database);
Now the port has changed from 1433 to 40450. 现在,端口已从1433更改为40450。
I am having trouble changing the port in the DSN. 我在更改DSN中的端口时遇到问题。 I thought this change would work but I am receiving a "DSN not found" error:
我以为此更改会起作用,但是我收到“找不到DSN”错误:
my $dsn = 'dbi:ODBC:MYDATABASE;Port=40450';
Any idea why this isn't working? 知道为什么这行不通吗?
There are two formats for a DBI data source string for ODBC. ODBC的DBI数据源字符串有两种格式。 You can say either
你可以说
dbi:ODBC:DSN=MYDATABASE
or you can abbreviate that to 或者您可以将其缩写为
dbi:ODBC:MYDATABASE
which is what you have. 这就是你所拥有的。 If you use just the DSN then you can't add any more parameters, so your
dbi:ODBC:MYDATABASE;Port=40450
is looking for DSN MYDATABASE;Port=40450
which clearly doesn't exist 如果仅使用DSN,则无法再添加任何参数,因此
dbi:ODBC:MYDATABASE;Port=40450
正在查找DSN MYDATABASE;Port=40450
,这显然不存在
The proper way to do this is to set up a new DSN which has a copy of all the parameters of MYDATABASE
, but with a different port name 正确的方法是设置一个新的DSN,该DSN包含
MYDATABASE
所有参数的副本,但端口名称不同
At a guess , I would say you may be able to write 大概猜 ,你也许会写
dbi:ODBC:DSN=MYDATABASE;Port=40450
but I can't be sure and I have no way of testing 但我不确定,我无法测试
If your requirements are simple then you can supply all of the parameters instead of a DSN, like this 如果您的要求很简单,那么您可以提供所有参数而不是DSN,就像这样
dbi:ODBC:Driver={SQL Server};Server=11.22.33.44;Port=40450
but you will have to supply the correct driver if you aren't using a SQL Server ODBC connection, and other parameters may be necessary 但是如果您不使用SQL Server ODBC连接,则必须提供正确的驱动程序,并且可能需要其他参数
You should start by examining the values in the MYDATABASE
DSN and go from there 您应该首先检查
MYDATABASE
DSN中的值,然后从那里开始
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.