cscの作法 その434

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

# 練習問題
SQLITEで小遣い帳にinsertせよ。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/e29248a5-9579-dea7-b34d-cc091737f14b.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(600, 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;
		gd0 = new DataGridView();
		gd0.Location = new Point(50, 160);
		gd0.Size = new Size(520, 250);
		Controls.AddRange(new Control[] {
			la0,
			bo0,
			la1,
			bo1,
			la2,
			bo2,
			la3,
			bo3,
			btn1,
			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();
		}
	}
	[STAThread]
	public static void Main() {
		Application.EnableVisualStyles();
		Application.SetCompatibleTextRenderingDefault(false);
		Application.Run(new form1());
	}
}




```

以上。