建立MDB檔讀取

建立MDB檔讀取

 

使用 ADO 以及 ADOX 之前,要加入 的參考

Microsoft ADO Ext. 2.8 for DDL and Security
Microsoft ActioveX Data Objects 2.8 Library


C:\Program Files\Common Files\System\ado\msado15.dll (ADODB)
C:\Program Files\Common Files\System\ado\msadox.dll (ADOX)

0

 

 

using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using ADOX;                              //引用  Microsoft ADO Ext. 2.8 for DDL and Security
                                         //引用  Microsoft ActioveX Data Objects 2.8 Library

namespace Access_build
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BT_Add_Click(object sender, EventArgs e)
        {
            if (textBox1.TextLength != 0)
            {
                string conn = "";
                string Pwd = "";
                string fileName = textBox1.Text;

                conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + @"c:\" +fileName + ".mdb";

                //   conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Jet OLEDB:Database Password=" + Pwd + ";Jet OLEDB:Engine Type=5";

                //開始建立資料庫
                ADOX.Catalog catalog = new Catalog();
                try
                {
                    catalog.Create(conn);
                }
                catch
                { }
                //連結資料庫宣告
                ADODB.Connection cn = new ADODB.Connection();
                cn.Open(conn, null, null, -1);
                catalog.ActiveConnection = cn;

                //新建表單
                ADOX.Table table = new ADOX.Table();
                table.Name = "booklist";

                ADOX.Column column = new ADOX.Column();
                column.ParentCatalog = catalog;
                column.Type = ADOX.DataTypeEnum.adInteger; //設定型別
                column.Name = "ID";
                column.DefinedSize = 9;
                column.Properties["AutoIncrement"].Value = true; //自動增加
                table.Columns.Append(column, DataTypeEnum.adInteger, 0);

                //設定PK
                table.Keys.Append("PrimaryKey", ADOX.KeyTypeEnum.adKeyPrimary, "ID", "", "");

                table.Columns.Append("bookName", DataTypeEnum.adVarWChar, 50);
                table.Columns.Append("bookPage", DataTypeEnum.adInteger, 9);
                table.Columns.Append("bookAuthor", DataTypeEnum.adVarWChar, 50);

                try
                {
                    catalog.Tables.Append(table);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                finally
                {
                    table = null;
                    catalog = null;
                    Application.DoEvents();
                    cn.Close();
                }

                MessageBox.Show("新增完畢!", "系統提示", MessageBoxButtons.OK);
            }
        }

        private void BT_geturl_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = Application.StartupPath;
            ofd.Filter = "All file (*.*)|*.*";

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                label1.Text = ofd.FileName;

                //if (checkBox1.Checked)
                //{
                //    ViewData(label1.Text, textBox1.Text.Trim());
                //}
                //else
                {
                    ViewData(label1.Text, null);
                }
            }
        }

        private void ViewData(string fileName, string pwd)
        {
            string strConnection;
            if (pwd == null)
            {
                strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Persist Security Info=False";
            }
            else
            {
                strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileName + ";Jet OLEDB:Database password=" + pwd;
            }

            OleDbConnection conn = new OleDbConnection(strConnection);
            string strSql = "select * From booklist";
            da = new OleDbDataAdapter(strSql, conn);
            ds = new DataSet();
            try
            {
                da.Fill(ds, "booklist");
                cb = new OleDbCommandBuilder(da);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

      

        private OleDbDataAdapter da;
        private DataSet ds;
        private OleDbCommandBuilder cb;

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (ds != null)
            {
                this.Validate();
                da.Update(ds, "booklist");
                ds.AcceptChanges();
                ViewData(label1.Text, null);
            }
        }
    }
}

 

 

1.新增

1_thumb[1]

 

 

 

2. 選擇檔位置

2

 

 

3.呈現

3

 

 

參考文獻

http://support.microsoft.com/kb/317881/zh-tw
http://www.cnblogs.com/wk986/archive/2010/09/11/1823948.html
http://maolaoda.blogspot.tw/2008/06/adox-table.html

 

 

 

 

大家一起加入blogads 賺零用錢!!