18
23

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 5 years have passed since last update.

【C#】DataTableについてのメモ

Last updated at Posted at 2018-05-02

DataTableとは

DataTableとは、データベースのように表形式で格納されているデータの事です。
列を指定し、行毎にデータを指定して行く事で、テーブルのようにデータを格納します。

DataTable作成、データの追加の仕方

DataTableの作成方法

// DataTableを宣言
DataTable dt = new DataTable("テーブル名");

// 列を追加
dt.Columns.Add("カラム名", typeof("型"));

DataTableへデータを挿入

// DataRowを宣言
DataRow dr;

// 行を作成
dr = dt.NewRow();
dr["カラム名"] = "値";

// 行を表に追加
dt.Rows.Add(dr);

DataTableの実践的な使い方

using System.Data;

DataTable dt = new DataTable("testTable");
DataRow dr;

dt.Columns.Add("id", typeof(int));
dt.Columns.Add("password", typeof(String));
dt.Columns.Add("userName", typeof(String));

dr = dt.NewRow();
dr["id"] = 001;
dr["password"] = "test";
dr["userName"] = "田中太郎";
// ※以下のような書き方も可能。ただし、列と値、その位置は揃える事。
// ⇒『td.Rows.Add(001, "test", "田中太郎");』

dt.Rows.Add(dr);
18
23
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
18
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?