{"id":1304,"date":"2017-05-08T16:37:32","date_gmt":"2017-05-08T07:37:32","guid":{"rendered":"http:\/\/okamurax.com\/?p=1304"},"modified":"2017-05-08T16:37:32","modified_gmt":"2017-05-08T07:37:32","slug":"c-%e5%a4%96%e4%bd%9c%e5%b7%a5%e7%a8%8b-%e4%bd%9c%e3%82%8a%e3%81%8b%e3%81%91","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=1304","title":{"rendered":"C# \u5916\u4f5c\u5de5\u7a0b \u4f5c\u308a\u304b\u3051"},"content":{"rendered":"<p>\u4eca\u306e\u65b9\u5411\u6027\u3067\u4f5c\u3063\u3066\u3044\u3063\u3066\u3082\u3001\u30a8\u30af\u30bb\u30eb\u7ba1\u7406\u3068\u6bd4\u3079\u3066\u660e\u78ba\u306a\u5229\u70b9\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u305f\u3081\u3002<\/p>\n<p>Form1.cs<\/p>\n<pre class=\"lang:c# decode:true  \">using System;\r\nusing System.Collections.Generic;\r\nusing System.ComponentModel;\r\nusing System.Data;\r\nusing System.Drawing;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing System.Windows.Forms;\r\n\r\nusing System.Data.OleDb;\r\n\r\nnamespace \u5916\u4f5c\u5de5\u7a0b\u30c1\u30e3\u30fc\u30c8\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        OleDbConnection con;\r\n        OleDbDataAdapter da;\r\n        DataTable dt;\r\n\r\n        private string BuildQuery()\r\n        {\r\n            string query = \"select \" + this.textBox4.Text + \" from tbl\";\r\n            string join = \" where\";\r\n\r\n            if(this.textBox1.Text != \"\")\r\n            {\r\n                query += join + \" \u7d0d\u671f &gt;= #\" + this.textBox1.Text + \"#\";\r\n                join = \" and\";\r\n            }\r\n\r\n            if (this.textBox2.Text != \"\")\r\n            {\r\n                query += join + \" \u7d0d\u671f &lt;= #\" + this.textBox2.Text + \"#\";\r\n                join = \" and\";\r\n            }\r\n\r\n            if (this.textBox3.Text != \"\")\r\n            {\r\n                query += join + \" \" + this.comboBox3.Text + \" like '%\" + this.textBox3.Text + \"%'\";\r\n            }\r\n\r\n            if(this.comboBox2.Text != \"\")\r\n            {\r\n                query += \" order by \" + this.comboBox2.Text;\r\n            }\r\n            return query;\r\n        }\r\n\r\n        private bool ConnectDatabase()\r\n        {\r\n            try\r\n            {\r\n                con = new OleDbConnection(@\"provider=microsoft.jet.oledb.4.0;data source=\" + Application.StartupPath + @\"\\dat.mdb\");\r\n                con.Open();\r\n            }\r\n            catch(OleDbException e)\r\n            {\r\n                MessageBox.Show(e.Message);\r\n                return false;\r\n            }\r\n            finally\r\n            {\r\n                con.Close();\r\n            }\r\n            return true;\r\n        }\r\n\r\n        public void DataSourceReset(string query)\r\n        {\r\n            da = new OleDbDataAdapter(query, con);\r\n            dt = new DataTable();\r\n            dataGridView1.Columns.Clear();\r\n\r\n            try\r\n            {\r\n                da.Fill(dt);\r\n            }\r\n            catch(OleDbException)\r\n            {\r\n                MessageBox.Show(\"\u5217\u304c\u6b63\u3057\u304f\u53d6\u5f97\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u8868\u793a\u5217\u306e\u8a2d\u5b9a\u304c\u6b63\u3057\u304f\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\");\r\n            }\r\n\r\n            dataGridView1.DataSource = dt;\r\n\r\n            this.comboBox2.Items.Clear();\r\n            this.comboBox3.Items.Clear();\r\n\r\n            List&lt;string&gt; colsName = new List&lt;string&gt;();\r\n\r\n            foreach (DataColumn col in dt.Columns)\r\n            {\r\n                colsName.Add(col.ColumnName);\r\n            }\r\n\r\n            this.comboBox2.Items.AddRange(colsName.ToArray());\r\n            this.comboBox3.Items.AddRange(colsName.ToArray());\r\n        }\r\n\r\n        public void InitControl()\r\n        {\r\n            this.radioButton1.Checked = true;\r\n\r\n            this.textBox1.Leave += new EventHandler(FormatDate);\r\n            this.textBox2.Leave += new EventHandler(FormatDate);\r\n\r\n            this.comboBox1.Items.AddRange(new string[] { \"\u6708\", \"\u9031\", \"\u65e5\" });\r\n            this.comboBox1.Text = \"\u65e5\";\r\n\r\n            this.button1.Click += new EventHandler(ClickSearch);\r\n            this.button2.Click += new EventHandler(ClickUpdate);\r\n            this.button3.Click += new EventHandler(SearchFormClear);\r\n            this.button4.Click += new EventHandler(UpdateSettings);\r\n\r\n            this.dataGridView1.DataError += new DataGridViewDataErrorEventHandler(DataGridViewError);\r\n        }\r\n\r\n        private void SearchFormClear(object sender, EventArgs e)\r\n        {\r\n            this.comboBox2.Text = \"\";\r\n            this.comboBox3.Text = \"\";\r\n\r\n            this.textBox1.Text = \"\";\r\n            this.textBox2.Text = \"\";\r\n            this.textBox3.Text = \"\";\r\n\r\n            DataSourceReset(BuildQuery());\r\n        }\r\n\r\n        private void FormatDate(object sender, EventArgs e)\r\n        {\r\n            DateTime dt;\r\n\r\n            try\r\n            {\r\n                dt = DateTime.Parse(((TextBox)sender).Text);\r\n            }\r\n            catch(FormatException)\r\n            {\r\n                ((TextBox)sender).Text = \"\";\r\n                return;\r\n            }\r\n            ((TextBox)sender).Text = dt.ToString(\"yyyy\/MM\/dd\");\r\n        }\r\n\r\n        private void DataGridViewError(object sender, DataGridViewDataErrorEventArgs e)\r\n        {\r\n            MessageBox.Show(\"\u5165\u529b\u304c\u6b63\u3057\u304f\u3042\u308a\u307e\u305b\u3093\u3002\");\r\n            e.Cancel = false;\r\n        }\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            Settings settings = new Settings(this);\r\n            if (!settings.InitCheck) return;\r\n            if (!ConnectDatabase()) return;\r\n\r\n            DataSourceReset(BuildQuery());\r\n            InitControl();\r\n        }\r\n\r\n        private void ClickSearch(object sender, EventArgs e)\r\n        {\r\n            if (this.textBox1.Text != \"\")\r\n            {\r\n                try\r\n                {\r\n                    DateTime dt = DateTime.Parse(this.textBox1.Text);\r\n                }\r\n                catch (FormatException)\r\n                {\r\n                    MessageBox.Show(\"\u958b\u59cb\u65e5(\u7d0d\u671f)\u304c\u6b63\u3057\u3044\u65e5\u4ed8\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\");\r\n                    return;\r\n                }\r\n            }\r\n\r\n            if (this.textBox2.Text != \"\")\r\n            {\r\n                try\r\n                {\r\n                    DateTime\u3000dt = DateTime.Parse(this.textBox2.Text);\r\n                }\r\n                catch (FormatException)\r\n                {\r\n                    MessageBox.Show(\"\u7d42\u4e86\u65e5(\u7d0d\u671f)\u304c\u6b63\u3057\u3044\u65e5\u4ed8\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002\");\r\n                    return;\r\n                }\r\n            }\r\n\r\n            if (this.textBox3.Text != \"\"\u3000&amp;&amp; this.comboBox3.Text == \"\")\r\n            {\r\n                MessageBox.Show(\"\u691c\u7d22\u5bfe\u8c61\u3092\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044\u3002\");\r\n                return;\r\n            }\r\n            DataSourceReset(BuildQuery());\r\n        }\r\n\r\n        private void ClickUpdate(object sender, EventArgs e)\r\n        {\r\n            try\r\n            {\r\n                con.Open();\r\n                OleDbCommandBuilder cb = new OleDbCommandBuilder(da);\r\n                da.Update(dt);\r\n            }\r\n            catch(InvalidOperationException)\r\n            {\r\n                MessageBox.Show(\"\u6b63\u3057\u304f\u66f4\u65b0\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002\u30c6\u30fc\u30d6\u30eb\u306bID\u3092\u8868\u793a\u3055\u305b\u3066\u3044\u306a\u3044\u53ef\u80fd\u6027\u304c\u3042\u308a\u307e\u3059\u3002\");\r\n                return;\r\n            }\r\n            finally\r\n            {\r\n                con.Close();\r\n            }\r\n            \r\n            dt.AcceptChanges();\r\n\r\n            DataSourceReset(BuildQuery());\r\n            MessageBox.Show(\"\u66f4\u65b0\u3057\u307e\u3057\u305f\u3002\");\r\n        }\r\n\r\n        private void UpdateSettings(object sender, EventArgs e)\r\n        {\r\n            if (this.textBox4.Text == \"\") this.textBox4.Text = \"*\";\r\n\r\n            try\r\n            {\r\n                con.Open();\r\n                OleDbCommand dc = new OleDbCommand(\"update settings set select_string = '\" + this.textBox4.Text + \"' where id = 1\",con);\r\n                dc.ExecuteNonQuery();\r\n            }\r\n            catch(InvalidOperationException)\r\n            {\r\n                MessageBox.Show(\"\u6b63\u3057\u304f\u66f4\u65b0\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002\");\r\n                return;\r\n            }\r\n            finally\r\n            {\r\n                con.Close();\r\n            }\r\n            DataSourceReset(BuildQuery());\r\n            MessageBox.Show(\"\u66f4\u65b0\u3057\u307e\u3057\u305f\u3002\");\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Settings.cs<\/p>\n<pre class=\"lang:c# decode:true\">using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.Threading.Tasks;\r\nusing System.Windows.Forms;\r\nusing System.Data.OleDb;\r\n\r\nnamespace \u5916\u4f5c\u5de5\u7a0b\u30c1\u30e3\u30fc\u30c8\r\n{\r\n    class Settings\r\n    {\r\n        public bool InitCheck;\r\n        public Settings(Form f)\r\n        {\r\n            using (OleDbConnection con = new OleDbConnection(@\"provider=microsoft.jet.oledb.4.0;data source=\" + Application.StartupPath + @\"\\dat.mdb\"))\r\n            {\r\n                try\r\n                {\r\n                    con.Open();\r\n\r\n                    OleDbCommand dc = new OleDbCommand(\"select * from settings\", con);\r\n                    using(OleDbDataReader dr = dc.ExecuteReader())\r\n                    {\r\n                        dr.Read();\r\n                        Control[] c = f.Controls.Find(\"textBox4\", true);\r\n                        c[0].Text = dr[\"select_string\"].ToString();\r\n                    }\r\n                }\r\n                catch (OleDbException)\r\n                {\r\n                    MessageBox.Show(\"\u8a2d\u5b9a\u304c\u6b63\u3057\u304f\u53d6\u5f97\u3067\u304d\u307e\u305b\u3093\u3067\u3057\u305f\u3002\");\r\n                    InitCheck = false;\r\n                    return;\r\n                }\r\n                finally\r\n                {\r\n                    con.Close();\r\n                }\r\n            }\r\n            InitCheck = true;\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u306e\u65b9\u5411\u6027\u3067\u4f5c\u3063\u3066\u3044\u3063\u3066\u3082\u3001\u30a8\u30af\u30bb\u30eb\u7ba1\u7406\u3068\u6bd4\u3079\u3066\u660e\u78ba\u306a\u5229\u70b9\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u305f\u3081\u3002 Form1.cs using System; using System.Collections.Generic; using System. &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=1304\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# \u5916\u4f5c\u5de5\u7a0b \u4f5c\u308a\u304b\u3051&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[9],"class_list":["post-1304","post","type-post","status-publish","format-standard","hentry","category-1","tag-c-net"],"_links":{"self":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/1304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1304"}],"version-history":[{"count":1,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/1304\/revisions"}],"predecessor-version":[{"id":1305,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/1304\/revisions\/1305"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}