0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

.Net 6: DataSet の使い方

Posted at

こちらのプログラムを DataSet を使うように書き換えました。
.Net 6: C# で PostgreSQL の Read

Program.cs
// ----------------------------------------------------------------
/*
	Program.cs

					Aug/20/2022

*/
// ----------------------------------------------------------------
using System;
using System.Data;
using Npgsql;

// ----------------------------------------------------------------
public static class postgres_read
{
// ----------------------------------------------------------------
public static void Main (string[] args)
{
	Console.WriteLine ("*** 開始 ***");
	string server="localhost";
	string str_db="city";

	string user="scott";
	string password="tiger123";

	string str_connect = "Host=" + server + ";Username="
		+ user + ";Password="
		+ password + ";Database=" + str_db + ";";

	NpgsqlConnection conn = new NpgsqlConnection(str_connect);
	
	NpgsqlCommand cmd = new NpgsqlCommand("SELECT * FROM cities", conn);

	NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(cmd);

	DataSet dsx = new DataSet();
	adapter.Fill(dsx);

	Console.WriteLine(dsx.Tables[0].Rows.Count);

	for (int it=0; it< dsx.Tables[0].Rows.Count; it++)
		{
		var Row = dsx.Tables[0].Rows[it];

		string out_str = "";
		out_str += Row[0] + "\t";
		out_str += Row[1] + "\t";
		out_str += Row[2] + "\t";
		out_str += Row[3];

		Console.WriteLine(out_str);
		}

	Console.WriteLine ("*** 終了 ***");
}

// ----------------------------------------------------------------
}
// ----------------------------------------------------------------

次のバージョンで確認しました。

$ dotnet --version
6.0.400
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?