我正在将 PostgreSQL pgadmin4 (4.16v) 与 ASP.NET 应用程序一起使用。我创建了一个如下定义的过程:
CREATE OR REPLACE PROCEDURE public.usp_bind(
)
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
select district_id,district_name from district_master order by district_name;
END;
$BODY$;
Run Code Online (Sandbox Code Playgroud)
从asp.net应用程序我调用了上面的过程,代码如下:
NpgsqlConnection conn = new NpgsqlConnection();
NpgsqlDataAdapter da = new NpgsqlDataAdapter();
NpgsqlCommand cmd = new NpgsqlCommand();
DataSet ds = new DataSet();
public string dbconstr = dbConnectstr.Connectionstring();
public DataSet getDatafunction(string procedure_, [Optional] string flag_)
{
using (conn = new NpgsqlConnection(dbconstr))
{
//conn.Open();
using (da = new NpgsqlDataAdapter())
{
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.CommandText = "CALL …Run Code Online (Sandbox Code Playgroud) 根据下面的链接,PostgreSql 支持多种ssl 模式连接,但其 ADO.net 驱动程序npgsql 不支持其 JDBC 驱动程序支持的 verify-ca 和 verify-full 模式。
这有什么原因吗?是否可以以其他方式使用这些模式?
PostgreSql 文档 https://www.postgresql.org/docs/9.1/libpq-ssl.html
Npgsql 驱动程序文档 https://www.npgsql.org/doc/connection-string-parameters.html
JDBC 驱动程序文档 https://jdbc.postgresql.org/documentation/head/ssl-client.html
这是预期的行为吗?如果是这样,有什么解决方法吗?我正在使用 Net Core 5.0 和 npgsql 5.0
public async Task<List<long>> GetGuiaIdsWithFilters(DateTime createddate)
{
IQueryable<Guia> guiaToReturn = _context.Guia;
createddate = createddate.ToUniversalTime();
return await guiaToReturn.Where(g => g.StatusHistory.Any(s.Fecha.Date == createddate.Date)).Select(x => x.Id).ToListAsync();
}
Run Code Online (Sandbox Code Playgroud)
运行此查询将返回以下错误,我将其隔离为提供程序无法提取日期部分:s.Fecha.Date (DateTimeOffset.Date)。我阅读了文档,DateTimeOffset将在PostgreSQL中存储为timestamptz,但我没有发现常见功能(例如获取日期部分)有任何限制。
无法翻译 LINQ 表达式 [...]。以可翻译的形式重写查询
我可以执行以下解决方法,该方法工作正常,但它似乎是一个效率低得多的查询:
public async Task<List<long>> GetGuiaIdsWithFilters(DateTime createddate)
{
IQueryable<Guia> guiaToReturn = _context.Guia;
var FromDate = createddate.ToUniversalTime().AddDays(-1);
var ToDate = createddate.ToUniversalTime.AddDays(1);
return await guiaToReturn.Where(g => g.StatusHistory.Any(s => s.Fecha > FromDate && s.Fecha < ToDate)).Select(x => x.Id).ToListAsync();
}
Run Code Online (Sandbox Code Playgroud) 有时我真的开始怀疑源代码中发生了什么:我正在尝试使用npgsql 2.0.11.0连接到PostGres 9.0,我确定我已经做到了,但是现在,我的程序在进入时抛出了NotSupportedException异常下列 :
ISessionFactory sf = Fluently.Configure()
.Database(PostgreSQLConfiguration.PostgreSQL82
.ConnectionString(c => c
.Host("localhost")
.Port(5432)
.Database("cw")
.Username("cw")
.Password("mypass")))
.Mappings(x => x.FluentMappings.AddFromAssemblyOf<MyMapping>())
.BuildSessionFactory();
Run Code Online (Sandbox Code Playgroud)
Stacktrace看起来很整洁:仅一行。
at NHibernate.Dialect.Dialect.GetDataBaseSchema(DbConnection connection) in d:\CSharp\NH\nhibernate\src\NHibernate\Dialect\Dialect.cs:Line 718.
Run Code Online (Sandbox Code Playgroud)
我尝试将其转录为以下内容:
ISessionFactory sf = Fluently.Configure()
.Database(PostgreSQLConfiguration.PostgreSQL82
.ConnectionString(c => c.Is("Server=localhost;Port=5432;Database=cw;User Id=cw;Password=myPass;")))
.Mappings(x => x.FluentMappings.AddFromAssemblyOf<CardTemplateMapping>())
.BuildSessionFactory();
Run Code Online (Sandbox Code Playgroud)
尽管如此,结果还是一样。任何人都有类似的问题,或者甚至更好的解决方法?
我在.NET 4.0下使用Npgsql 2.0.11来修改PostgreSQL 9.0数据库.该程序在一个事务中对数据库进行了许多修改.
在提交之前,我运行SELECT语句,有时会失败(例如,超时).我吞下了异常并继续进行交易.没有错误,所以看起来好像一切正常,但实际上数据库根本没有修改!
我的猜测是失败的SELECT回滚整个事务.我可以阻止这种情况(即,事务仍然提交)或者至少检测到这种情况并抛出异常,因此用户知道提交失败了吗?
我知道在这个特定情况下我可以在事务外部移动SELECT,但我更关心的是解决这个问题.提交不提交是一个非常严重的问题,我想确保它不会被检测到.
我使用visual studio,postgresql数据库和ado.net实体数据模型.在连接字符串中,我无法设置MultipleActiveResultSets=True.
通常当我连接到sql server时MultipleActiveResultSets=True,它工作正常.但我不能设置与postgresql数据库相同.
当我使用它时,我得到以下错误
已经有一个与此命令关联的打开DataReader,必须先关闭它.
我该如何解决这个问题.
我在C#.NET 4.0应用程序中使用Npgsql 2.0.11.94连接到PostgreSql数据库.我在他们的网站上根据示例形成了连接字符串,当我调用NpgsqlConnection对象连接时,抛出此异常:
A first chance exception of type 'Npgsql.NpgsqlException' occurred in Npgsql.dll
Additional information: ERROR: 22023: 3 is outside the valid range for parameter "extra_float_digits" (-15 .. 2)
Run Code Online (Sandbox Code Playgroud)
抛出异常后,代码将正确执行.也就是说,确实建立了与数据库的连接,并且查询返回了正确的数据.有谁知道它为什么抛出这个例外?这是我连接到db的代码:
string strConnection = "Server=192.168.253.20;Port=5432;User Id=alex;Password=asdf;Database=mydatabase;";
NpgsqlConnection conn = null;
try
{
conn = new NpgsqlConnection(strConnection);
conn.Open();
}
catch (Exception e)
{
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激.
谢谢,亚历克斯
该代码不起作用。谁能指导我在哪里可以找到使用C#快速创建Postgresql数据库和表的示例?
const string connStr = "Server=localhost;Port=5432;
User Id=postgres;Password=enter;Database=postgres";
var m_conn = new NpgsqlConnection(connStr);
// creating a database in Postgresql
m_createdb_cmd = new NpgsqlCommand("CREATE DATABASE IF NOT EXISTS \"testDb\" " +
"WITH OWNER = \"postgres\" " +
"ENCODING = 'UTF8' " +
"CONNECTION LIMIT = -1;", m_conn);
// creating a table in Postgresql
m_createtbl_cmd = new NpgsqlCommand(
"CREATE TABLE MyTable(CompanyName VARCHAR(150))";
m_conn.Open();
m_createdb_cmd.ExecuteNonQuery();
m_createtbl_cmd.Connection = m_conn;
m_conn.Close();
Run Code Online (Sandbox Code Playgroud)
数据库已创建,但是在创建表时出现静默失败。
所以我在安装npgsql.dll和mono.security.dll时遇到问题。我一直在努力解决这个问题,请帮助。这是我已完成的步骤...
在第1步中下载的zip文件中,找不到任何地方的Npgsql.dll和Mono.Security.dll ...
我还尝试下载实际上已经包含这些文件的其他zip文件,但是,当我尝试在命令(gacutil / i C:... \ Npgsql.dll)中安装它们时,我一直保持以下错误:
“将程序集添加到缓存失败:???????????”
我查看过www.npgsql.org上的资料,但找不到如何解决我的问题......
表,PostgreSQL
[City], [State]
"Austin", "TX"
"Houston", "TX"
"Los Angeles", "CA"
"San Diego", "CA"
"San Fransisco";"CA"
"St.Louis", "MO"
Run Code Online (Sandbox Code Playgroud)
函数(存储过程),PostgreSQL
-- Procedure that returns a single result set (cursor)
CREATE OR REPLACE FUNCTION show_cities() RETURNS refcursor AS $$
DECLARE
ref refcursor;
BEGIN
OPEN ref FOR SELECT city, state FROM cities;
RETURN ref;
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
代码,C#
using (NpgsqlConnection conn = new NpgsqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString))
{
conn.Open();
using (NpgsqlTransaction tran = conn.BeginTransaction())
{
using (var command = new NpgsqlCommand("show_cities", conn))
{ …Run Code Online (Sandbox Code Playgroud)