C# .NET + PostgreSQL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# .NET + PostgreSQL
提问by akdom
I'm looking at working on a project which uses C#.NET (sitting on a windows box) as the primary language and PostgreSQL as the backend database (backend is sitting on a linux box). I've heard that ODBC.NET allows for easy integration of these two components.
我正在研究一个项目,该项目使用 C#.NET(位于 Windows 机器上)作为主要语言,PostgreSQL 作为后端数据库(后端位于 linux 机器上)。我听说 ODBC.NET 允许轻松集成这两个组件。
Has anyone had experience actually setting C# and PostgreSQL up to work together? If so, do you have any suggestions about how to go about it, issues you've found, etc.?
有没有人有实际设置 C# 和 PostgreSQL 一起工作的经验?如果是这样,您对如何进行操作、发现的问题等有任何建议吗?
采纳答案by jalcom
I'm working with C# and Postgres using Npgsql2component, and they work fast, I recommend you.
我正在使用Npgsql2组件使用 C# 和 Postgres ,它们工作得很快,我推荐你。
You can download from https://github.com/npgsql/Npgsql/releases
您可以从https://github.com/npgsql/Npgsql/releases下载
Note:If you want an application that works with any database you can use the DbProviderFactoryclass and make your queries using IDbConnection, IDbCommand, IDataReaderand/or IDbTransactioninterfaces.
注意:如果您想要一个适用于任何数据库的应用程序,您可以使用DbProviderFactory类并使用IDbConnection、IDbCommand、IDataReader和/或IDbTransaction接口进行查询。
回答by Andrei R?nea
Today most languages/platforms (Java, .NET, PHP, Perl etc.) can work with almost any DBMS (SQL Server, Firebird, MySQL, Oracle, PostgresSQL etc.) so I wouldn't worry for one second. Sure there might be glitches and small problems but no showstopper.
今天,大多数语言/平台(Java、.NET、PHP、Perl 等)几乎可以与任何 DBMS(SQL Server、Firebird、MySQL、Oracle、PostgresSQL 等)一起使用,所以我一秒钟都不会担心。当然,可能会有小故障和小问题,但没有停止。
As jalcom suggested you should program against a set of interfaces or at least a set of base classes (DbConnection, DbCommand and so on) to have an easily adaptable application.
正如 jalcom 建议的那样,您应该针对一组接口或至少一组基类(DbConnection、DbCommand 等)进行编程,以获得易于适应的应用程序。
回答by Kibbee
You shouldn't have too many problems. As others have mentioned, there's many .Net PostgreSQL data providers available. One thing you may want to look out for is that features like Linq will probably not be able to be used.
你不应该有太多的问题。正如其他人所提到的,有许多 .Net PostgreSQL 数据提供程序可用。您可能需要注意的一件事是,Linq 之类的功能可能无法使用。
回答by David Schmitt
There is a Linq provider for PostgreSQL at https://www.nuget.org/packages/linq2db.PostgreSQL/.
在https://www.nuget.org/packages/linq2db.PostgreSQL/ 上有一个 PostgreSQL 的 Linq 提供程序。
回答by Simon
We have developed several applications using visual studio 2005 with the devart ado.net data provider for PostgreSql (http://www.devart.com/pgsqlnet/).
我们使用visual studio 2005 和用于PostgreSql 的devart ado.net 数据提供程序(http://www.devart.com/pgsqlnet/)开发了几个应用程序。
One of the advantages of this provider is that it provides full Visual Studio support. The latest versions include all the new framework features like linq.
此提供程序的优点之一是它提供完整的 Visual Studio 支持。最新版本包括所有新的框架功能,如 linq。
回答by Stradas
Npgsql- .Net Provider for PostGreSQL - is an excellent driver. If you have used the more traditional ADO.NET framework you are really in luck here. I have code that connects to Oracle that looks almost identical to the PostGreSQL connections. Easier to transition off of Oracle and reuse brain cells.
Npgsql- PostGreSQL 的 .Net Provider - 是一个优秀的驱动程序。如果您使用过更传统的 ADO.NET 框架,那么您真的很幸运。我有连接到 Oracle 的代码,看起来几乎与 PostGreSQL 连接相同。更容易从 Oracle 过渡并重用脑细胞。
It supports all of the standard things you would want to do with calling SQL, but it also supports calling Functions(stored procedures). This includes the returning of reference cursors. The documentation is well written and provides useful examples without getting philosophical or arcane. Steal the code right out of the documentation and it will work instantly.
它支持您希望通过调用 SQL 执行的所有标准操作,但它还支持调用函数(存储过程)。这包括返回引用游标。该文档写得很好,并提供了有用的示例,但没有变得哲学或神秘。直接从文档中窃取代码,它会立即生效。
Francisco Figueiredo, Jr's and team have done a great job with this.
It is now available on Github.
https://github.com/franciscojunior/Npgsql2
Francisco Figueiredo, Jr's 和团队在这方面做得很好。
它现在在Github 上可用。
https://github.com/franciscojunior/Npgsql2
The better site for info is: http://npgsql.projects.postgresql.org/
更好的信息站点是:http: //npgsql.projects.postgresql.org/
Read the documentation! http://npgsql.projects.postgresql.org/docs/manual/UserManual.html
阅读文档! http://npgsql.projects.postgresql.org/docs/manual/UserManual.html
回答by user542319
Dont let a lack of Linq support stop you. A pattern I use is to always return my data into lists, and then linq away. I started doing this religiously when I found that the same (admittedly obscure) Linq expression in MySQL didnt bring back the same data as it did in Sql Server.
不要让缺乏 Linq 支持阻止您。我使用的一种模式是始终将我的数据返回到列表中,然后 linq 离开。当我发现 MySQL 中相同的(不可否认的)Linq 表达式没有像在 Sql Server 中那样带回相同的数据时,我开始虔诚地这样做。
回答by JRA
Just Go to Tool-->NuGet Package Manager-->Manager Nuget Package Manager
只需转到工具--> NuGet 包管理器--> 管理器 Nuget 包管理器
search for NpgSqland then select your project and click Install
搜索NpgSql,然后选择您的项目并单击安装
sample Code
示例代码
public void Demo()
{
NpgsqlConnection connection = new NpgsqlConnection();
connection = d_connection; // your connection string
connection.Open();
NpgsqlCommand cmd = new NpgsqlCommand();
try
{
cmd.Connection = connection;
cmd.CommandText = "select * from your table name";
cmd.CommandType = System.Data.CommandType.Text;
using (var dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
string answer= dataReader.IsDBNull(0) ? "" : dataReader.GetString(0);
}
dataReader.Dispose();
}
}
catch (Exception e)
{
}
finally
{
cmd.Dispose();
connection.Dispose();
}
}
Dont use upper case in postgreSql because its case sensitive.
不要在 postgreSql 中使用大写,因为它区分大小写。
回答by Maciej
Npgsql is excellent driver, but only issue Ive found so far is that Numeric value does not fit in a System.Decimal, so only option is correct each query or DB schema
Npgsql 是出色的驱动程序,但到目前为止我发现的唯一问题是数值不适合 System.Decimal,因此每个查询或 DB 模式的唯一选项是正确的
https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/438#issuecomment-486586272
https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/issues/438#issuecomment-486586272