如何导入npgsql模块? [英] How to import the npgsql module?

查看:121
本文介绍了如何导入npgsql模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从PowerShell脚本中连接到PostgreSQL数据库。但是如何在没有Visual Studio的情况下安装npgsql?没有任何缺点!

I need to connect to a PostgreSQL database from within PowerShell scripts. But how to install npgsql WITHOUT VisualStudio? There is no nuget!

因此,我尝试使用最新的MSI文件( Npgsql-3.0.5.msi )。

So I tried to install the driver in the GAC using the newest MSI file (Npgsql-3.0.5.msi).

使用 gacutil.exe 显示已安装:


Npgsql,版本= 3.0.5.0,文化=中性,PublicKeyToken = 5d8b90d52f46fda7,processorArchitecture = MSIL

Npgsql, Version=3.0.5.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7, processorArchitecture=MSIL

但是PowerShell对此一无所知! Get-Module -ListAvailable 应该显示它,但:

But PowerShell does not know anything about it! Get-Module -ListAvailable should show it but:

PS C:\WINDOWS\system32> (gmo -l n*).Name

NetAdapter  
NetConnection  
NetEventPacketCapture  
NetLbfo  
NetNat  
NetQos  
NetSecurity  
NetSwitchTeam  
NetTCPIP  
NetWNV  
NetworkConnectivityStatus  
NetworkLoadBalancingClusters  
NetworkTransition  
NFS  
PS C:\WINDOWS\system32> _

没有模块 Npgsql

我正在搜索 Npgsql.dll ,它是那里:

I was searching for Npgsql.dll and it is there:

PS  C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Npgsql\v4.0_3.0.5.0__5d8b90d52f46fda7> dir

Verzeichnis: C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Npgsql\v4.0_3.0.5.0__5d8b90d52f46fda7


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        08.01.2016     08:10     446464 Npgsql.dll


PS C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Npgsql\v4.0_3.0.5.0__5d8b90d52f46fda7> _

由于无法识别的模块 npgsql 我的PowerShell代码不起作用:

Because of the not recognised module npgsql my PowerShell code does not work:

function getDBConnection ($MyDBServer, $MyDBPort, $MyDatabase, $MyUid, $MyPwd) {
  $DBConnectionString = "Provider=PostgreSQL OLE DB Provider;Data Source=$MyDBServer;location=$MyDatabase;User ID=$MyUid;password=$MyPwd;timeout=1000;"
  $DBConn = New-Object System.Data.OleDb.OleDbConnection;
  $DBConn.ConnectionString = $DBConnectionString
  try {
    $DBConn.Open
  } catch {
    "Failed to connect! Error: "+ $_.Exception.Message
  }

  return $DBConn
}

function closeDBConnection ($DBConn) {
  $DBConn.Close
}

$query = "select * from test_table1"
$MyDBConnection = getDBConnection "dbserver" 5432 "databasename" "user" "pass"

try {
  $cmd = New-Object System.Data.OleDb.OleDbCommand($query, $MyDBConnection)
} catch {
  "Failed to create command object! Error: " + $_.Exception.Message
}
...

$ DBConn .Open 不会失败,但是 ConnectionState 属性在<$之后仍然保持 Closed c $ c> Open 调用。

The $DBConn.Open doesn't fail, but the property ConnectionState remains Closed after the Open call.

OleDbCommand 实例的创建崩溃并带有德语错误消息:

The creation of an instance of OleDbCommand crashes with a german error message:


无法创建命令对象!错误: OleDbCommand和folgende Argumenteanzahl kann keineÜberladunggefunden werden: 2。

Failed to create command object! Error: Für "OleDbCommand" und die folgende Argumenteanzahl kann keine Überladung gefunden werden: "2".

这意味着没有重载具有两个参数的方法。

It means that there are no overloaded methods with two parameters.

问题:


  • 我需要安装/配置什么才能在PowerShell中看到 Npgsql

  • 如何加载/导入<$ PowerShell脚本或模块中的c $ c> Npgsql 模块?

  • What do I need to install/configure so that Npgsql is visible in PowerShell?
  • How do I load/import the Npgsql module in a PowerShell script or module?

推荐答案

如有疑问,请阅读文档 Get-Module -ListAvailable 仅列出 $ env:PSModulePath

When in doubt, read the documentation. Get-Module -ListAvailable only lists modules from the directories listed in $env:PSModulePath.


-ListAvailable

获取所有已安装的模块。 Get-Module 获取环境变量 PSModulePath 中列出的路径中的模块。如果没有此参数,则 Get-Module 仅获取同时在 PSModulePath 环境变量中列出且在当前会话中加载的模块。 ListAvailable 不会返回有关在 PSModulePath 环境变量中找不到的模块的信息,即使这些模块已在当前会话中加载。

Gets all installed modules. Get-Module gets modules in paths listed in the PSModulePath environment variable. Without this parameter, Get-Module gets only the modules that are both listed in the PSModulePath environment variable, and that are loaded in the current session. ListAvailable does not return information about modules that are not found in the PSModulePath environment variable, even if those modules are loaded in the current session.

但是,您只需在脚本中导入具有完整路径的任何模块即可:

However, you can simply import any module with its full path in your script:

Import-Module 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Npgsql\v4.0_3.0.5.0__5d8b90d52f46fda7\Npgsql.dll'

允许在路径中使用通配符:

Using wildcards in the path is allowed:

Import-Module 'C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Npgsql\*\Npgsql.dll'

如果要能够按名称加载模块,请在列出的目录之一中创建一个子文件夹 Npgsql $ env:PSModulePath 中,然后将DLL放入该子文件夹。

If you want to be able to load the module by name create a subfolder Npgsql in one of the directories listed in $env:PSModulePath and put the DLL into that subfolder.

这篇关于如何导入npgsql模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆