C# 使用Npgsql for Postgresql的C查询显示重复的结果和缺少的表数据

C# 使用Npgsql for Postgresql的C查询显示重复的结果和缺少的表数据,c#,database,postgresql,datasource,npgsql,C#,Database,Postgresql,Datasource,Npgsql,我正在检查PostgreSQL作为SQLServer的潜在替代品,我在PostgreSQL公共模式的测试数据库中创建了一个测试表,并向测试表添加了两行数据 现在的问题是,当使用NpgSQL.dll从C.net运行简单查询时,我会得到重复的结果,并且不会显示所有的表数据 以下是我使用的代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threa

我正在检查PostgreSQL作为SQLServer的潜在替代品,我在PostgreSQL公共模式的测试数据库中创建了一个测试表,并向测试表添加了两行数据

现在的问题是,当使用NpgSQL.dll从C.net运行简单查询时,我会得到重复的结果,并且不会显示所有的表数据

以下是我使用的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Npgsql;


namespace PlayingWithPostgres
{
    class Program
    {
        static void Main(string[] args)
        {
            // creating the connection string (Server, Port, User id, password, database)
            string conStr = "Server=127.0.0.1; Port=5432; User Id=postgres; Password=Sada1973; Database=CarsTestDB;";
            NpgsqlConnection conn = new NpgsqlConnection(conStr);
            string comStr = "Select * FROM \"CarsTable\";";
            NpgsqlCommand com = new NpgsqlCommand(comStr, conn);
            NpgsqlDataAdapter ad = new NpgsqlDataAdapter(com);
            DataTable dt = new DataTable();
            Console.WriteLine("Conection to server established successfuly \n");
            // check if connection is open or not
            if(conn != null && conn.State == ConnectionState.Open)
            {
                Console.WriteLine("Connection Open");
                conn.Close();
            }
            else
            {
                conn.Open();
            }

            // Fill data table with data and start reading
            ad.Fill(dt);
            NpgsqlDataReader dRead = com.ExecuteReader();

            try
            {
                Console.WriteLine("Contents of table in database: \n");
                while (dRead.Read())
                {
                    foreach(DataRow row in dt.Rows)
                    {
                        for (int i = 0; i < dt.Rows.Count; i++)
                        {
                            Console.Write("{0} \t \n", row[i].ToString());
                        }
                    }
                }
            }
            catch (NpgsqlException ne)
            {
                Console.WriteLine("Problem connecting to server, Error details {0}", ne.ToString());
            }
            finally
            {
                Console.WriteLine("Closing connections");
                dRead.Close();
                dRead = null;
                conn.Close();
                conn = null;
                com.Dispose();
                com = null;
            }
        }
    }
}

在最初的方法中,对于每一行,您将显示其列,直到索引=行计数的列。我相信这不是你想做的。您应该按每行显示每一列,如下所示:

而不是

foreach(DataRow row in dt.Rows)
{
    for (int i = 0; i < dt.Rows.Count; i++)
    {
        Console.Write("{0} \t \n", row[i].ToString());
    }
}
使用:


重复内容的问题是由使用WhiledRead.Read的循环和使用foreach的表的数据行上的循环引起的。这会在数据上有效地循环两次

如果要使用DataReader循环记录,则不需要DataRow和DataTable,而是使用DataReader的FieldCount属性和当前记录的DataReader索引器

// Not needed
// ad.Fill(dt);
NpgsqlDataReader dRead = com.ExecuteReader();

while (dRead.Read())
{
   for(int i = 0; i < dRead.FieldCount; i++)
       Console.Write("{0} \t \n", dRead[i].ToString());
}

找不到dataTable dt的ItemArray。抱歉,这是行。现在编辑。两种方法都起作用了,现在我显示了整个表数据,但它仍然是重复的。我的意思是每个记录显示两次。很抱歉,无法看到您的新代码。当然你只使用上面两种方法中的一种,对吧?当然我只使用了一种方法,但仍然得到了重复的结果,我对PostgreSQL是新手,一直在SQLServer上工作,这也是为什么我使用data table和data reader,但只在一个方法上运行查询,只是为了测试PostgreSQL。也许您可以在原始问题中添加新代码。我得到了它。您建议的第一个方法可以很好地删除重复项,非常感谢我的朋友,非常感谢。
// Not needed
// ad.Fill(dt);
NpgsqlDataReader dRead = com.ExecuteReader();

while (dRead.Read())
{
   for(int i = 0; i < dRead.FieldCount; i++)
       Console.Write("{0} \t \n", dRead[i].ToString());
}
ad.Fill(dt);
// Not needed
// NpgsqlDataReader dRead = com.ExecuteReader();
foreach(DataRow row in dt.Rows)
{
    for (int i = 0; i < dt.Columns.Count; i++)
    {
        Console.Write("{0} \t \n", row[i].ToString());
    }
}