{"id":5118,"date":"2022-06-09T18:10:41","date_gmt":"2022-06-09T09:10:41","guid":{"rendered":"https:\/\/okamurax.com\/?p=5118"},"modified":"2022-06-09T20:06:25","modified_gmt":"2022-06-09T11:06:25","slug":"c-accessaccdb-%e6%8e%a5%e7%b6%9a","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=5118","title":{"rendered":"C# Access(accdb) \u63a5\u7d9a"},"content":{"rendered":"<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.Security.Cryptography;\r\nusing System.Data.OleDb;\r\n\r\nnamespace PersonalManager\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        private string connectionString = @\"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\" + Application.StartupPath + @\"\\dat.accdb;\" + \"Jet OLEDB:Database Password=xxx\";\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            button1.Click += (s, e) =&gt;\r\n            {\r\n                ExecuteNonQuery(\"create table employee(\" +\r\n                    \"id autoincrement primary key,\" +\r\n                    \"name text\" +\r\n                    \")\");\r\n\r\n                ExecuteNonQuery(\"insert into employee (name) values ('taro')\");\r\n\r\n                DataTable dt = ExecuteQuery(\"select * from employee\");\r\n\r\n                List&lt;Model&gt; models = new List&lt;Model&gt;();\r\n                for (int r = 0; r &lt; dt.Rows.Count; r++)\r\n                {\r\n                    models.Add(new Model()\r\n                    {\r\n                        id = dt.Rows[r][\"id\"].ToString(),\r\n                        name = dt.Rows[r][\"name\"].ToString()\r\n                    });\r\n                }\r\n                dataGridView1.DataSource = models;\r\n            };\r\n        }\r\n\r\n        private void ExecuteNonQuery(string commandText)\r\n        {\r\n            using (OleDbConnection con = new OleDbConnection(connectionString))\r\n            {\r\n                con.Open();\r\n                using (OleDbCommand cmd = new OleDbCommand())\r\n                {\r\n                    try\r\n                    {\r\n                        cmd.Connection = con;\r\n                        cmd.CommandText = commandText;\r\n                        cmd.ExecuteNonQuery();\r\n                    }\r\n                    catch (Exception e)\r\n                    {\r\n                        MessageBox.Show(e.Message);\r\n                    }\r\n                }\r\n            }\r\n        }\r\n\r\n        private void ExecuteNonQuery(List&lt;string&gt; commandTextList)\r\n        {\r\n            using (OleDbConnection con = new OleDbConnection(connectionString))\r\n            {\r\n                con.Open();\r\n                using (OleDbCommand cmd = new OleDbCommand())\r\n                {\r\n                    cmd.Transaction = con.BeginTransaction();\r\n\r\n                    try\r\n                    {\r\n                        foreach (string sql in commandTextList)\r\n                        {\r\n                            cmd.Connection = con;\r\n                            cmd.CommandText = sql;\r\n                            cmd.ExecuteNonQuery();\r\n                        }\r\n                        cmd.Transaction.Commit();\r\n                    }\r\n                    catch (Exception e)\r\n                    {\r\n                        MessageBox.Show(e.Message);\r\n                        cmd.Transaction.Rollback();\r\n                    }\r\n                }\r\n            }\r\n        }\r\n\r\n        private DataTable ExecuteQuery(string commandText)\r\n        {\r\n            using (OleDbConnection con = new OleDbConnection(connectionString))\r\n            {\r\n                try\r\n                {\r\n                    con.Open();\r\n                    using (OleDbCommand cmd = new OleDbCommand())\r\n                    {\r\n                        cmd.Connection = con;\r\n                        cmd.CommandText = commandText;\r\n\r\n                        using (OleDbDataReader reader = cmd.ExecuteReader())\r\n                        {\r\n                            DataTable dt = new DataTable();\r\n                            dt.Load(reader);\r\n                            return dt;\r\n                        }\r\n                    }\r\n                }\r\n                catch (Exception e)\r\n                {\r\n                    MessageBox.Show(e.Message);\r\n                    return new DataTable();\r\n                }\r\n            }\r\n        }\r\n    }\r\n\r\n    class Model\r\n    {\r\n        public string id { get; set; }\r\n        public string name { get; set; }\r\n\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.D &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=5118\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Access(accdb) \u63a5\u7d9a&#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-5118","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\/5118","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=5118"}],"version-history":[{"count":2,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/5118\/revisions"}],"predecessor-version":[{"id":5122,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/5118\/revisions\/5122"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}