标签: npgsql

为什么PostgreSQL会将查询中的值视为列名?

我正在使用带有C#的Npgsql与我的PostgreSQL数据库进行通信.我的数据库中使用的所有名称都是大小写混合,因此在查询中我确保在每个名称周围使用双引号.以下是我发送查询的方式:

// construct an insert query
string insertQuery = "insert into \"Update\" (\"Vehicle\",\"Property\",\"Value\") " + 
                     "values (" + vehicleNum.ToString() + ",\"" + propertyName + 
                     "\",\"" + propertyValue + "\")";

// execute the query
NpgsqlCommand insertCommand = new NpgsqlCommand(insertQuery, conn);
insertCommand.ExecuteScalar();
Run Code Online (Sandbox Code Playgroud)

通过插入断点并进行检查,我验证了字符串insertQuery在发送之前看起来是这样的:

insert into "Update" ("Vehicle","Property","Value") values (12345,"EngineSpeed","50")
Run Code Online (Sandbox Code Playgroud)

当我发送这个查询时,PostgreSQL给了我一个错误,它包含在一个Npgsql异常中,该异常指出: ERROR: 42703: column "EngineSpeed" does not exist

从我的查询中可以看出,EngineSpeed它不是列,它是列的值Property,因此具有该名称的列自然不可能存在.那么为什么PostgreSQL以这种方式处理我的查询,我该如何解决这个问题呢?我的查询是否以错误的方式构建?

c# postgresql npgsql

1
推荐指数
1
解决办法
2235
查看次数

使用PostgreSQL的Npgsql:使用UNCOMMITTED READ无法看到未提交的更改

我正在使用Npsql和PostgreSQL.我想在另一个事务中看到一个事务的未提交更改.

这是我创建连接和事务的方式:

// create connection
m_Connection = new NpgsqlConnection(connectionString);
m_Connection.Open();

//create transaction
m_Transaction = m_Connection.BeginTransaction(IsolationLevel.ReadUncommitted);
Run Code Online (Sandbox Code Playgroud)

在一个线程中,我插入一行,如下所示:

 NpgsqlCommand command = CreateCommand("INSERT INTO TABLEA  ....", parameters, commandTimeout)
 command.ExecuteNonQuery();
Run Code Online (Sandbox Code Playgroud)

并且在不提交或回滚交易的情况下处理其他内容.

在另一个线程中,我读了一行,如下所示:

 NpgsqlCommand command = CreateCommand("SELECT COUNT(*) FROM TABLEA", parameters, commandTimeout);
 command.ExecuteScalar();
Run Code Online (Sandbox Code Playgroud)

但不知怎的,我没有看到第一个INSERT的结果.我也没有在pgAdmin中看到插入的结果(即使在运行SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED之后).

我究竟做错了什么?任何帮助将不胜感激.

postgresql npgsql

1
推荐指数
1
解决办法
1826
查看次数

Npgsql,TransactionScope和准备好的事务

有人可以说我可以在TransactionScope中使用npgsql吗?

在此手册中提供的代码示例:http://npgsql.projects.postgresql.org/docs/manual/UserManual.html不起作用.它只是在服务器上创建两个准备好的事务.

我遇到此问题相同的问题:TransactionScope和Npgsql - 准备好的事务问题

在TransactionScope中使用npgsql有什么解决方案吗?

UPD:首先我的目标:我需要在我的软件中使用单个逻辑转换中的两个连接.对此最好的解决方案是TransactionScope.Npgsql声明支持在系统事务中进行登记.

然后我麻烦了:我使用文档中的代码,这段代码做了下面的事情:

  1. 创建交易范围
  2. 打开第一个连接
  3. 打开第二个连接
  4. 插入第一个连接
  5. 插入第二个连接
  6. 关闭第二个连接
  7. 关闭第一个连接
  8. 调用scope.Complete()
  9. 处理范围

我等了,9步后的数据将完全提交给db.实际上,在步骤6和7中,npgsql创建准备好的事务,并且在步骤9中什么都不做.最后我有两个预先准备好的交易,即阻止数据库.没有人可以提交或回滚它们.

通常如果我在范围内调用完成之前关闭连接(例如因为异常)npgsql创建阻止表的准备事务.我认为这不是严谨的行为.我等待范围处理数据完全提交或完全回滚后.没有任何准备好的交易.

您可以使用文档中的代码重复此错误.供参考Devart免费库工作正确.

postgresql transactions distributed-transactions prepared-statement npgsql

1
推荐指数
1
解决办法
4467
查看次数

连接到PostgreSQL

Npgsql用来处理PostgreSQL使用C#.为了连接数据库,我写道:

NpgsqlConnection conn = new NpgsqlConnection(connstring);
conn.Open();
Run Code Online (Sandbox Code Playgroud)

Open()是一种void方法.它不返回任何指示是否连接到数据库的值.我需要显示状态connectednot connected在我的客户端应用程序中.怎么做?

c# database postgresql npgsql

1
推荐指数
1
解决办法
5983
查看次数

如何在NpgsqlConnection中设置编码

我有一个PostgreSQL数据库,它使用字符编码WIN1252.

查询数据库时,某些记录在尝试读取数据时会产生错误,因为它正在尝试将其转换为UTF8.在包含某些非拉丁字符的某些外来名称上会发生这种情况.

错误是:

ERROR: 22P05: character with byte sequence 0x81 in encoding "WIN1252" has no equivalent in encoding "UTF8"
Run Code Online (Sandbox Code Playgroud)

它发生在我打电话Read()的时候NpgsqlDataReader.

我的连接定义为:

new NpgsqlConnection("Server=127.0.0.1;Port=5432;Database=xyz;User Id=****;Password=****;");
Run Code Online (Sandbox Code Playgroud)

如何使用C#读取此数据?

c# postgresql encoding npgsql

1
推荐指数
1
解决办法
2686
查看次数

如何从C#中的PostgreSQL获取所选行的值?

我正在使用带有C#和Npgsql库的PostgreSQL数据库。

现在,我可以选择表中的最后一行,但是我不知道如何为它分配C#变量。我知道我的选择有效,因为我之前已经成功编辑了最后一个条目。

您可以在下面找到我的代码。请注意,我没有粘贴其余方法,因为我认为它们无关紧要。

public void myMethod()
{
    this.OpenConn(); //opens the connection

    string sql = "SELECT id FROM information_schema.tables WHERE table_schema = 'public' AND table_name = 'customers' ORDER BY id DESC, LIMIT 1";

    using (NpgsqlCommand command = new NpgsqlCommand(sql, conn))
    {
        int id = 0; //instead of '0' I want it to be equal to the ID value from the row
        //something like "int id = sqlSelection.id;" -- this obviously doesn't work

        this.CloseConn(); //close the current connection
    }
}
Run Code Online (Sandbox Code Playgroud)

c# sql postgresql npgsql

1
推荐指数
1
解决办法
6401
查看次数

npgsql 2.1.3和EF 6:无法确定存储版本; 需要有效的存储连接或版本提示

我正在为我的sql生成器添加对EF 6的支持,用于PostgreSQL:PostgreSQL Migration Generator

我已经创建了一个测试项目来尝试它,但是当我创建一个新连接时return new NpgsqlConnection();,它会向我抛出这个异常:

An exception of type 'System.NotSupportedException' occurred in EntityFramework.dll but was not handled in user code

Additional information: Unable to determine the provider name for provider factory of type 'Npgsql.NpgsqlFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
Run Code Online (Sandbox Code Playgroud)

按照Npgsql官方网站上的说明,我使用这个app.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections> …
Run Code Online (Sandbox Code Playgroud)

c# postgresql entity-framework npgsql

1
推荐指数
1
解决办法
8089
查看次数

实体框架6 - Npgsql - 连接字符串错误

一个棘手的(令人沮丧的问题) - 也许你们大家可能很聪明地解决它:

问题

我希望能够使用Entity Frameworks读取/写入我的数据库.我有一个在Heroku(简单的脚手架)上运行的简单app rails.我想连接到这个数据库并操纵记录.好消息是我可以使用npgsql成功连接到该数据库.坏消息是我不能用实体框架来做.这是我收到的错误:

System.Data.Entity.Core.ProviderIncompatibleException:从数据库获取提供程序信息时发生错误.这可能是由实体框架使用不正确的连接字符串引起的.检查内部异常以获取详细信息,并确保连接字符串正确.---> System.Data.Entity.Core.ProviderIncompatibleException:提供程序未返回ProviderManifestToken字符串.---> System.IO.FileLoadException:无法加载文件或程序集'Npgsql,Version = 3.1.2.0,Culture = neutral,PublicKeyToken = 5d8b90d52f46fda7'或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(HRESULT异常:0x80131040)

这是堆栈跟踪:

   at Npgsql.NpgsqlServices.GetDbProviderManifestToken(DbConnection connection) 
   at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) 
   --- End of inner exception stack trace --- 
   at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) 
   at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) 
   --- End of inner exception stack trace --- 
   at System.Data.Entity.Utilities.DbProviderServicesExtensions.GetProviderManifestTokenChecked(DbProviderServices providerServices, DbConnection connection) 
   at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.<>c__DisplayClass1.<ResolveManifestToken>b__0(Tuple`3 k) 
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) 
   at System.Data.Entity.Infrastructure.DefaultManifestTokenResolver.ResolveManifestToken(DbConnection connection) 
   at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest) 
   at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) 
   at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) 
   at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) 
   at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 
   at …
Run Code Online (Sandbox Code Playgroud)

c# connection-string heroku npgsql entity-framework-6

1
推荐指数
1
解决办法
3084
查看次数

SELECT查询在C#方法中返回异常

我是 C# 和 Npgsql 的新手,我正在尝试在某个表中进行搜索,但出现异常。

public User Login(Account c)
{
    User usr = new User();
    using (NpgsqlConnection con = new NpgsqlConnection(strConnection))
    {
        try
        {
            con.Open();
            NpgsqlCommand command = new NpgsqlCommand();
            command.Connection = con;
            command.CommandText = "SELECT name FROM public.user WHERE c_id IN (SELECT id FROM public.account WHERE email=@Email AND password=@Password)";
            command.Parameters.AddWithValue("Email", c.Email);
            command.Parameters.AddWithValue("Password", c.Password);
            NpgsqlDataReader dr = command.ExecuteReader();

            if (dr.HasRows)
            {
                usr.Name = dr["name"].ToString();
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }    
    return usr;
}
Run Code Online (Sandbox Code Playgroud)

咆哮异常:

System.InvalidOperationException: '没有可用的行' …

c# sql npgsql

1
推荐指数
1
解决办法
1505
查看次数

如何使用Visual Studio 2017或.NET 4.5+设置PostgreSQL?

关于PostgreSQL的安装,有几篇文章和文章,但是它们有些混乱,并且为不同的版本提供了不同的方法。

这篇文章将为那些希望将PostgreSQLEntity Framework结合使用的人提供帮助-数据库优先方法

要求:VS 2017 PostgreSQL DB 11.1 EF 6+

.net postgresql entity-framework npgsql visual-studio

1
推荐指数
1
解决办法
2432
查看次数