cscの作法 その173

# 概要
cscの作法、調べてみた。
mdbをつないで、table作って、データ追加して、表示してみた。

# 写真

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/18104/c53ae28f-9cc8-700e-dbcd-b36bb89cb688.png)

# コンパイル手順

```
csc acs0.cs /platform:x86

```

# サンプルコード

```
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data.OleDb;
using System.Data;
using System.Collections;
using System.ComponentModel;
using System.Text;

namespace app
{
	public class form2: Form {
		public form2() {
			Text = "DataGridView";
			ClientSize = new Size(700, 400);
			DataGridView gd0 = new DataGridView();
			gd0.Dock = DockStyle.Fill;
			this.Controls.Add(gd0);
			OleDbConnection con = new OleDbConnection();
			con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydb.mdb;";
			con.Open();
			OleDbDataAdapter oda = new OleDbDataAdapter("SELECT * FROM 売上テーブル;", con);
			DataTable dt = new DataTable();
			oda.Fill(dt);
			gd0.DataSource = dt;
			con.Close();
		}
	}
	public class Form1 : Form {
		private Button button1;
		private Button button2;
		private Button button3;
		private Button button4;
		public Form1() {
			this.button1 = new Button();
			this.button2 = new Button();
			this.button3 = new Button();
			this.button4 = new Button();
			this.button1.Location = new System.Drawing.Point(30, 30);
			this.button1.TabIndex = 0;
			this.button1.Text = "create";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			this.button2.Location = new System.Drawing.Point(30, 80);
			this.button2.TabIndex = 0;
			this.button2.Text = "make";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			this.button3.Location = new System.Drawing.Point(30, 130);
			this.button3.TabIndex = 0;
			this.button3.Text = "view";
			this.button3.Click += new System.EventHandler(this.button3_Click);
			this.button4.Location = new System.Drawing.Point(30, 180);
			this.button4.TabIndex = 0;
			this.button4.Text = "del";
			this.button4.Click += new System.EventHandler(this.button4_Click);
			this.ClientSize = new System.Drawing.Size(300, 300);
			this.Controls.AddRange(new Control[] {
				this.button1,
				this.button2,
				this.button3,
				this.button4,
			});
			this.Text = "access";
		}
		private void button4_Click(object sender, System.EventArgs e) {
			OleDbConnection con = new OleDbConnection();
			con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydb.mdb;";
			con.Open();
			string sSQL = "DELETE FROM 売上テーブル WHERE 売上<100";
			OleDbCommand cmd = new OleDbCommand(sSQL, con);
			cmd.ExecuteNonQuery();
			Console.WriteLine("ok");
		}
		private void button1_Click(object sender, System.EventArgs e) {
			OleDbConnection con = new OleDbConnection();
			con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydb.mdb;";
			con.Open();
			string sSQL = "CREATE TABLE 売上テーブル(販売コード AUTOINCREMENT CONSTRAINT PrimaryKey PRIMARY KEY, 支店 TEXT(255), 販売日 DATETIME, 商品 TEXT(255), 個数 INTEGER, 売上 CURRENCY);";
			OleDbCommand cmd = new OleDbCommand(sSQL, con);
			cmd.ExecuteNonQuery();
			Console.WriteLine("ok");
			string sSQL2 =  "CREATE INDEX 販売日 ON 売上テーブル (販売日) WITH DISALLOW NULL;";
			OleDbCommand cmd2 = new OleDbCommand(sSQL2, con);
			cmd2.ExecuteNonQuery();
			Console.WriteLine("ok");
		}
		private void button2_Click(object sender, System.EventArgs e) {
			OleDbConnection con = new OleDbConnection();
			con.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=mydb.mdb;";
			con.Open();
			Random rnd = new Random();
			string[] varSites = new string[] {"新宿", "渋谷", "池袋", "上野", "品川"};
			string[] varProducts = new string[] {"烏龍茶", "オレンジジュース", "グレープジュース", "コーヒー", "紅茶"};
			int[] varPrices = new int[] {150, 250, 250, 180, 180};
			for (int i = 10; i < 31; i++)
			{
				for (int j = 0; j < rnd.Next(3, 6); j++)
				{
					int intProdId = rnd.Next(4);
					int ko = rnd.Next(2, 12);
					string sSQL = String.Format("INSERT INTO 売上テーブル(支店, 販売日, 商品, 個数, 売上) VALUES('{0}',#2022-05-{1}#,'{2}',{3},{4})", varSites[rnd.Next(4)], i, varProducts[intProdId], ko, varPrices[intProdId] * ko);
					OleDbCommand cmd = new OleDbCommand(sSQL, con);
					cmd.ExecuteNonQuery();
				}
			}
			Console.WriteLine("ok");
		}
		private void button3_Click(object sender, System.EventArgs e) {
			form2 dialog = new form2();
			dialog.Owner = this;
			dialog.Show();
		}
		[STAThread]
		static void Main() {
			Application.Run(new Form1());
		}
	}
}





```

以上。