简体   繁体   English

C#.net核心如何使用npgsql检索所有行

[英]C# .net core how can I retrieve all rows using npgsql

I am testing out the new .net Core RC2 with Npgsql because I have a postgres database. 我正在用Npgsql测试新的.net Core RC2,因为我有一个postgres数据库。 I am new to all of this but what I am trying to do is to retrieve 5 records out of a database and return it back in Json format. 我对所有这些都是新手,但是我想做的是从数据库中检索5条记录,然后以Json格式返回。 However, I am only able to output 1 record even though I can see that all 5 records are being returned. 但是,即使看到所有5条记录都已返回,我也只能输出1条记录。 What am I missing here.. 我在这里想念什么..

public JsonResult About()
{
    using (NpgsqlConnection conn = new NpgsqlConnection("myconnection"))
    {
        conn.Open();

        string city="";
        string state= "";
        NpgsqlCommand cmd = new NpgsqlCommand("select city,state from streams limit 5", conn);

        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                city = reader.GetString(reader.GetOrdinal("city"));
                state = reader.GetString(reader.GetOrdinal("state"));
            }
        }
        return Json(city);
    }
}

I'm guessing the value of city returned in the Json object would only contain the last city and state returned from the query. 我猜测Json对象中返回的city的值将仅包含查询返回的最后一个城市和州。 The while loop overwrites the city variable each iteration. while循环在每次迭代时都会覆盖city变量。

Storing each city and state in an array will allow for you to return all 5 cities. 将每个城市和州存储在一个阵列中将使您可以返回所有5个城市。

Code Update 代码更新

string city="";
string state= "";
NpgsqlCommand cmd = new NpgsqlCommand("select city,state from streams limit 5", conn);

var cities = new string[5]();
using (var reader = cmd.ExecuteReader())
{
    var i = 0;
    while (reader.Read())
    {
        city = reader.GetString(reader.GetOrdinal("city"));
        state = reader.GetString(reader.GetOrdinal("state"));

        cities[i++] = city;
    }
}

// Aggregate into one string
var citiesString = String.Join(", ", cities);

return Json(citiesString);

Array return 数组返回

On the other hand you can just return the array which will serialize into a javascript array using json. 另一方面,您可以只返回将使用json序列化为javascript数组的数组。

string city="";
string state= "";
NpgsqlCommand cmd = new NpgsqlCommand("select city,state from streams limit 5", conn);

var cities = new string[5]();
using (var reader = cmd.ExecuteReader())
{
    var i = 0;
    while (reader.Read())
    {
        city = reader.GetString(reader.GetOrdinal("city"));
        state = reader.GetString(reader.GetOrdinal("state"));

        cities[i++] = city;
    }
}
return Json(cities);

I'd recommend using Entity Framework, and just serialize the Model to json. 我建议使用实体框架,然后将模型序列化为json。 Checkout the Npgsql.EntityFrameworkCore.PostgreSQL -Pre nuget package. 检出Npgsql.EntityFrameworkCore.PostgreSQL -Pre nuget包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 .NET Core 3.1 的 C# 中检索域用户的所有本地组? - How can I retrieve all the local groups for a domain user in C# in .NET Core 3.1? C# 如何从数据库中检索所有记录 - C# how can i retrieve all record from the database 如何使用c#检索所有当前打开的内存映射文件的列表? - How can I retrieve a list of all currently open memory mapped files using c#? 我可以在 CentOS 上安装 .NET Core for Linux 和 Npgsql 吗? - Can I install .NET Core for Linux and Npgsql on CentOS? 如何在 C# ASP.NET Core 6.0 中从 Azure AD 检索用户拥有的所有组? - How to retrieve all the groups that a user has from Azure AD in C# ASP.NET Core 6.0? 如何使用Entity Framework Core和Npgsql清理所有字符串输入? - How do I sanitize all my string inputs using Entity Framework Core and Npgsql? 如何使用 C# 和 .NET 核心制作转码视频文件流 - How can I make a Transcoded Video Filestream using C# and .NET Core 如何使用 Npgsql 从 Postgresql 检索 UTC 日期时间 - How can you retrieve a UTC DateTime from Postgresql using Npgsql 如何使用C#检索本地机器SID? - How can I retrieve the local Machine SID using C#? 如何通过仅使用.NET Core SDK直接调用C#编译器来编译.NET Standard 2.0类库? - How can I compile a .NET Standard 2.0 class library by directly invoking the C# complier using only the .NET Core SDK?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM