{"id":967,"date":"2017-02-21T22:44:10","date_gmt":"2017-02-21T13:44:10","guid":{"rendered":"http:\/\/okamurax.com\/?p=967"},"modified":"2017-02-22T06:13:57","modified_gmt":"2017-02-21T21:13:57","slug":"c-access%e3%83%87%e3%83%bc%e3%82%bf%e3%83%99%e3%83%bc%e3%82%b9%e6%af%94%e8%bc%83","status":"publish","type":"post","link":"https:\/\/appbay.org\/?p=967","title":{"rendered":"C# Access(accdb) \u6bd4\u8f03"},"content":{"rendered":"<p>\u696d\u8005\u88fd\u4f5c\u306eAccess\u30d5\u30a1\u30a4\u30eb+\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3067\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30d7\u30ed\u30b0\u30e9\u30e0\u5074\u3067\u64cd\u4f5c\u3057\u305f\u7d50\u679c\u304c<br \/>\nAccess\u5185\u90e8\u3067\u3069\u3046\u5909\u66f4\u3055\u308c\u308b\u304b\u8abf\u3079\u3088\u3046\u3068\u601d\u3063\u305f\u304cAccess\u30d5\u30a1\u30a4\u30eb\u304c\u5927\u304d\u3059\u304e\u305f\u3002<\/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\n\/* Microsoft Access \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9 \u30a8\u30f3\u30b8\u30f3 2010 \u518d\u9812\u5e03\u53ef\u80fd\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8 (AccessDatabaseEngine.exe)\r\n\u30d5\u30a1\u30a4\u30eb\u304caccdb\u3001ACE.OLEDB.12.0\u3092\u5229\u7528\u3059\u308b\u5834\u5408\u3001\r\nPC\u304c32bit\u306a\u3089\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u306a\u3044\u3068\u30d7\u30ed\u30d0\u30a4\u30c0\u304c\u898b\u3064\u304b\u3089\u306a\u3044\u3002*\/\r\n\r\nnamespace \u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u5dee\u5206\u30c1\u30a7\u30c3\u30af\r\n{\r\n    public partial class Form1 : Form\r\n    {\r\n        UserDataSet uds1 = null;\r\n        UserDataSet uds2 = null;\r\n\r\n        public Form1()\r\n        {\r\n            InitializeComponent();\r\n\r\n            comboBox1.Enabled = false;\r\n            comboBox2.Enabled = false;\r\n\r\n            comboBox1.SelectedIndexChanged += new EventHandler(ComboxChange1);\r\n            comboBox2.SelectedIndexChanged += new EventHandler(ComboxChange2);\r\n        }\r\n        private void ComboxChange1(object sender, EventArgs e)\r\n        {\r\n            if (uds1 == null) return;\r\n            dataGridView1.DataSource = uds1.AccessDataSet.Tables[comboBox1.Text];\r\n        }\r\n        private void ComboxChange2(object sender, EventArgs e)\r\n        {\r\n            if (uds2 == null) return;\r\n            dataGridView2.DataSource = uds2.AccessDataSet.Tables[comboBox2.Text];\r\n        }\r\n\r\n        private void button1_Click(object sender, EventArgs e)\r\n        {\r\n            OpenFileDialog ofd = new OpenFileDialog();\r\n            if (ofd.ShowDialog() == DialogResult.OK)\r\n            {\r\n                if (listBox1.Items.Count &lt;= 1)\r\n                {\r\n                    listBox1.Items.Add(ofd.FileName);\r\n                }\r\n                else\r\n                {\r\n                    listBox1.Items[0] = listBox1.Items[1];\r\n                    listBox1.Items.RemoveAt(1);\r\n                    listBox1.Items.Add(ofd.FileName);\r\n                }\r\n            }\r\n        }\r\n\r\n        private void button2_Click(object sender, EventArgs e)\r\n        {\r\n            comboBox1.Items.Clear();\r\n            comboBox2.Items.Clear();\r\n\r\n            if (listBox1.Items.Count == 1)\r\n            {\r\n                this.uds1 = new UserDataSet(listBox1.Items[0].ToString());\r\n\r\n                for (int i = 0; i &lt; uds1.AccessDataSet.Tables.Count; i++)\r\n                {\r\n                    comboBox1.Items.Add(uds1.AccessDataSet.Tables[i].TableName);\r\n                }\r\n                comboBox1.Enabled = true;\r\n            }\r\n\r\n\r\n            if (listBox1.Items.Count == 2)\r\n            {\r\n                this.uds2 = new UserDataSet(listBox1.Items[1].ToString());\r\n\r\n                for (int i = 0; i &lt; uds2.AccessDataSet.Tables.Count; i++)\r\n                {\r\n                    comboBox2.Items.Add(uds2.AccessDataSet.Tables[i].TableName);\r\n                }\r\n                comboBox2.Enabled = true;\r\n            }\r\n        }\r\n    }\r\n\r\n    public class UserDataSet\r\n    {\r\n        public DataSet AccessDataSet = new DataSet();\r\n\r\n        public UserDataSet(string filePath)\r\n        {\r\n            using (System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(\r\n                @\"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\" + filePath + \";JET OLEDB:Database Password=xxx\"))\r\n            {\r\n                try\r\n                {\r\n                    con.Open(); \/\/fill\u306e\u5834\u5408\u4e0d\u8981\u3060\u304cGetSchema\u3067\u5fc5\u8981\u3002\r\n\r\n                    DataTable dt = con.GetSchema(\"Tables\");\r\n\r\n                    foreach (DataRow r in dt.Rows)\r\n                    {\r\n                        string tableName = r[\"TABLE_NAME\"].ToString(); \/\/System.Diagnostics.Debug.Print(r[\"TABLE_NAME\"].ToString());\r\n\r\n                        if (tableName.Substring(0, 4) != \"MSys\")\r\n                        {\r\n                            using (System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter())\r\n                            {\r\n                                System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(\"select * from \" + tableName, con);\r\n                                da.SelectCommand = cmd;\r\n\r\n                                DataTable newTable = new DataTable(tableName);\r\n                                AccessDataSet.Tables.Add(newTable);\r\n                                da.Fill(AccessDataSet.Tables[tableName]);\r\n                            }\r\n                        }\r\n                    }\r\n                }\r\n                catch(Exception e)\r\n                {\r\n                    MessageBox.Show(e.Message);\r\n                    return;\r\n                }\r\n                finally\r\n                {\r\n                    con.Close();\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u696d\u8005\u88fd\u4f5c\u306eAccess\u30d5\u30a1\u30a4\u30eb+\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3067\u3001\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30d7\u30ed\u30b0\u30e9\u30e0\u5074\u3067\u64cd\u4f5c\u3057\u305f\u7d50\u679c\u304c Access\u5185\u90e8\u3067\u3069\u3046\u5909\u66f4\u3055\u308c\u308b\u304b\u8abf\u3079\u3088\u3046\u3068\u601d\u3063\u305f\u304cAccess\u30d5\u30a1\u30a4\u30eb\u304c\u5927\u304d\u3059\u304e\u305f\u3002 using System; using S &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/appbay.org\/?p=967\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;C# Access(accdb) \u6bd4\u8f03&#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-967","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\/967","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=967"}],"version-history":[{"count":5,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/967\/revisions"}],"predecessor-version":[{"id":5109,"href":"https:\/\/appbay.org\/index.php?rest_route=\/wp\/v2\/posts\/967\/revisions\/5109"}],"wp:attachment":[{"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appbay.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}