今の方向性で作っていっても、エクセル管理と比べて明確な利点が見つからないため。
Form1.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; namespace 外作工程チャート { public partial class Form1 : Form { OleDbConnection con; OleDbDataAdapter da; DataTable dt; private string BuildQuery() { string query = "select " + this.textBox4.Text + " from tbl"; string join = " where"; if(this.textBox1.Text != "") { query += join + " 納期 >= #" + this.textBox1.Text + "#"; join = " and"; } if (this.textBox2.Text != "") { query += join + " 納期 <= #" + this.textBox2.Text + "#"; join = " and"; } if (this.textBox3.Text != "") { query += join + " " + this.comboBox3.Text + " like '%" + this.textBox3.Text + "%'"; } if(this.comboBox2.Text != "") { query += " order by " + this.comboBox2.Text; } return query; } private bool ConnectDatabase() { try { con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + @"\dat.mdb"); con.Open(); } catch(OleDbException e) { MessageBox.Show(e.Message); return false; } finally { con.Close(); } return true; } public void DataSourceReset(string query) { da = new OleDbDataAdapter(query, con); dt = new DataTable(); dataGridView1.Columns.Clear(); try { da.Fill(dt); } catch(OleDbException) { MessageBox.Show("列が正しく取得できませんでした。表示列の設定が正しくない可能性があります。"); } dataGridView1.DataSource = dt; this.comboBox2.Items.Clear(); this.comboBox3.Items.Clear(); List<string> colsName = new List<string>(); foreach (DataColumn col in dt.Columns) { colsName.Add(col.ColumnName); } this.comboBox2.Items.AddRange(colsName.ToArray()); this.comboBox3.Items.AddRange(colsName.ToArray()); } public void InitControl() { this.radioButton1.Checked = true; this.textBox1.Leave += new EventHandler(FormatDate); this.textBox2.Leave += new EventHandler(FormatDate); this.comboBox1.Items.AddRange(new string[] { "月", "週", "日" }); this.comboBox1.Text = "日"; this.button1.Click += new EventHandler(ClickSearch); this.button2.Click += new EventHandler(ClickUpdate); this.button3.Click += new EventHandler(SearchFormClear); this.button4.Click += new EventHandler(UpdateSettings); this.dataGridView1.DataError += new DataGridViewDataErrorEventHandler(DataGridViewError); } private void SearchFormClear(object sender, EventArgs e) { this.comboBox2.Text = ""; this.comboBox3.Text = ""; this.textBox1.Text = ""; this.textBox2.Text = ""; this.textBox3.Text = ""; DataSourceReset(BuildQuery()); } private void FormatDate(object sender, EventArgs e) { DateTime dt; try { dt = DateTime.Parse(((TextBox)sender).Text); } catch(FormatException) { ((TextBox)sender).Text = ""; return; } ((TextBox)sender).Text = dt.ToString("yyyy/MM/dd"); } private void DataGridViewError(object sender, DataGridViewDataErrorEventArgs e) { MessageBox.Show("入力が正しくありません。"); e.Cancel = false; } public Form1() { InitializeComponent(); Settings settings = new Settings(this); if (!settings.InitCheck) return; if (!ConnectDatabase()) return; DataSourceReset(BuildQuery()); InitControl(); } private void ClickSearch(object sender, EventArgs e) { if (this.textBox1.Text != "") { try { DateTime dt = DateTime.Parse(this.textBox1.Text); } catch (FormatException) { MessageBox.Show("開始日(納期)が正しい日付ではありません。"); return; } } if (this.textBox2.Text != "") { try { DateTime dt = DateTime.Parse(this.textBox2.Text); } catch (FormatException) { MessageBox.Show("終了日(納期)が正しい日付ではありません。"); return; } } if (this.textBox3.Text != "" && this.comboBox3.Text == "") { MessageBox.Show("検索対象を選択してください。"); return; } DataSourceReset(BuildQuery()); } private void ClickUpdate(object sender, EventArgs e) { try { con.Open(); OleDbCommandBuilder cb = new OleDbCommandBuilder(da); da.Update(dt); } catch(InvalidOperationException) { MessageBox.Show("正しく更新できませんでした。テーブルにIDを表示させていない可能性があります。"); return; } finally { con.Close(); } dt.AcceptChanges(); DataSourceReset(BuildQuery()); MessageBox.Show("更新しました。"); } private void UpdateSettings(object sender, EventArgs e) { if (this.textBox4.Text == "") this.textBox4.Text = "*"; try { con.Open(); OleDbCommand dc = new OleDbCommand("update settings set select_string = '" + this.textBox4.Text + "' where id = 1",con); dc.ExecuteNonQuery(); } catch(InvalidOperationException) { MessageBox.Show("正しく更新できませんでした。"); return; } finally { con.Close(); } DataSourceReset(BuildQuery()); MessageBox.Show("更新しました。"); } } } |
Settings.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.OleDb; namespace 外作工程チャート { class Settings { public bool InitCheck; public Settings(Form f) { using (OleDbConnection con = new OleDbConnection(@"provider=microsoft.jet.oledb.4.0;data source=" + Application.StartupPath + @"\dat.mdb")) { try { con.Open(); OleDbCommand dc = new OleDbCommand("select * from settings", con); using(OleDbDataReader dr = dc.ExecuteReader()) { dr.Read(); Control[] c = f.Controls.Find("textBox4", true); c[0].Text = dr["select_string"].ToString(); } } catch (OleDbException) { MessageBox.Show("設定が正しく取得できませんでした。"); InitCheck = false; return; } finally { con.Close(); } } InitCheck = true; } } } |