cscの作法 その435

# 概要
cscの作法、調べてみた。
練習問題、やってみた。

# 練習問題
SQLITEで小遣い帳を集計せよ。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/76f3b98d-d6fb-fa1e-d366-ce352a69e3de.png)

# サンプルコード

```
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Data.SQLite;

class form1: Form {
	TextBox bo0;
	TextBox bo1;
	TextBox bo2;
	TextBox bo3;
	DataGridView gd0;
	form1() {
		Text = "小遣い帳";
		ClientSize = new Size(700, 450);
		Label la0 = new Label();
		la0.Location = new Point(50, 10);
		la0.Text = "日付";
		bo0 = new TextBox();
		bo0.Location = new Point(150, 10);
		bo0.Text = "2023-08-30";
		Label la1 = new Label();
		la1.Location = new Point(50, 40);
		la1.Text = "内訳";
		bo1 = new TextBox();
		bo1.Location = new Point(150, 40);
		bo1.Text = "アイス";
		Label la2 = new Label();
		la2.Location = new Point(50, 70);
		la2.Text = "出入り";
		bo2 = new TextBox();
		bo2.Location = new Point(150, 70);
		bo2.Text = "-";
		Label la3 = new Label();
		la3.Location = new Point(50, 100);
		la3.Text = "金額";
		bo3 = new TextBox();
		bo3.Location = new Point(150, 100);
		bo3.Text = "100";
		Button btn1 = new Button();
		btn1.Location = new Point(150, 130);
		btn1.Text = "登録";
		btn1.Click += btn1_Click;
		Button btn2 = new Button();
		btn2.Location = new Point(350, 130);
		btn2.Text = "集計";
		btn2.Click += btn2_Click;
		gd0 = new DataGridView();
		gd0.Location = new Point(50, 160);
		gd0.Size = new Size(620, 250);
		Controls.AddRange(new Control[] {
			la0,
			bo0,
			la1,
			bo1,
			la2,
			bo2,
			la3,
			bo3,
			btn1,
			btn2,
			gd0
		});
		using (SQLiteConnection conn = new SQLiteConnection("Data Source=db0.sqlite"))
		{
			conn.Open();
			using (var command = conn.CreateCommand())
			{
				command.CommandText = "CREATE TABLE IF NOT EXISTS  kozu(id INTEGER PRIMARY KEY, adate TEXT, memo TEXT, inp INTEGER, oup INTEGER)";
				command.ExecuteNonQuery();
			}
			conn.Close();
		}
		using (SQLiteConnection conn = new SQLiteConnection("Data Source=db0.sqlite"))
		{
			conn.Open();
			using (DataSet dataSet = new DataSet())
			{
				String sql = "SELECT * From kozu ORDER BY adate ASC";
				SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sql, conn);
				dataAdapter.Fill(dataSet);
				gd0.DataSource = dataSet.Tables[0];
			}
			conn.Close();
		}
	}
	void btn1_Click(object sender, System.EventArgs e) {
		int inp = 0;
		int oup = 0;
		if (bo2.Text == "-")
		{
			oup = Convert.ToInt32(bo3.Text);
		}
		else
		{
			inp = Convert.ToInt32(bo3.Text);
		}
		using (SQLiteConnection conn = new SQLiteConnection("Data Source=db0.sqlite"))
		{
			conn.Open();
			using (DataSet dataSet = new DataSet())
			{
				String sql = String.Format("INSERT INTO kozu(adate, memo, inp, oup) VALUES('{0}', '{1}', '{2}', '{3}')", bo0.Text, bo1.Text, inp, oup);
				SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sql, conn);
				dataAdapter.Fill(dataSet);
				MessageBox.Show("登録しました。");
			}
			conn.Close();
		}
		using (SQLiteConnection conn = new SQLiteConnection("Data Source=db0.sqlite"))
		{
			conn.Open();
			using (DataSet dataSet = new DataSet())
			{
				String sql = "SELECT * From kozu ORDER BY adate ASC";
				SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sql, conn);
				dataAdapter.Fill(dataSet);
				gd0.DataSource = dataSet.Tables[0];
			}
			conn.Close();
		}
	}
	void btn2_Click(object sender, System.EventArgs e) {
		using (SQLiteConnection conn = new SQLiteConnection("Data Source=db0.sqlite"))
		{
			conn.Open();
			using (DataSet dataSet = new DataSet())
			{
				String sql = "SELECT * From kozu ORDER BY adate ASC";
				SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(sql, conn);
				dataAdapter.Fill(dataSet);
				dataSet.Tables[0].Columns.Add("残金", typeof(int));
				gd0.DataSource = dataSet.Tables[0];
			}
			conn.Close();
		}
		gd0.Columns[1].HeaderText = "日付";
		gd0.Columns[2].HeaderText = "内訳";
		gd0.Columns[3].HeaderText = "貰ったお金";
		gd0.Columns[4].HeaderText = "使ったお金";
		int rowCount = gd0.Rows.Count;
		int pre = 0;
		for (int i = 0; i < rowCount - 1; i++)
		{
			int inp = 0;
			int oup = 0;
			int zan = 0;
			String s2 = Convert.ToString(gd0[3, i].Value);
			if (s2 != "")
			{
				inp = Convert.ToInt32(s2);
			}
			String s3 = Convert.ToString(gd0[4, i].Value);
			if (s3 != "")
			{
				oup = Convert.ToInt32(s3);
			}
			String s4 = Convert.ToString(gd0[5, i].Value);
			if (s4 != "")
			{
				zan = Convert.ToInt32(s4);
			}
			pre = inp - oup + zan + pre;
			gd0[5, i].Value = pre;
		}
	}
	[STAThread]
	public static void Main() {
		Application.EnableVisualStyles();
		Application.SetCompatibleTextRenderingDefault(false);
		Application.Run(new form1());
	}
}




```

以上。