access数据库和SQLite数据库速度的简单对比

先上一张结果图,用了4种方式,分别插入10000条数据,可以看到差别还是很大的,如果插入更多数据的话,估计速度还会差更大

这是源代码

string AccessConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Environment.CurrentDirectory + "\\TestDB.accdb";
        string SQLiteConn = "Data Source=" + Environment.CurrentDirectory + "\\Test.db";

        float II = 10000;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LblTotal.Text = "";
        }

        private void Btn1_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection(AccessConn);
            conn.Open();

            Lab1.Text = "开始:" + DateTime.Now.ToString("HH:mm:ss-fff");

            OleDbCommand Cmd = new OleDbCommand();
            Cmd.Connection = conn;
            OleDbTransaction tr=conn.BeginTransaction();
            Cmd.Transaction = tr;

            for(int i=1;i<=II;i++)
            {
               
                Cmd.CommandText = "Insert into Test(Sid,Sname) values(" + i + ",'" + i.ToString() + "')";
                Cmd.ExecuteNonQuery();
                Application.DoEvents();

                Lab3.Text = (i / II) * 100 + " %";
            }

            tr.Commit();

            Lab2.Text = "结束:" + DateTime.Now.ToString("HH:mm:ss-fff");

            tr.Dispose();
            Cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }

        private void Btn2_Click(object sender, EventArgs e)
        {
            ADODB.Recordset rs = new ADODB.Recordset();
            ADODB.Connection cn = new ADODB.Connection();
            cn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Environment.CurrentDirectory + "\\TestDB.accdb";
            cn.Open();

            object dummy;

            Lab4.Text = "开始:" + DateTime.Now.ToString("HH:mm:ss-fff");

            cn.BeginTrans();

            for (int i = 1; i <= II; i++)
            {

                string aa = "Insert into Test(Sid,Sname) values(" + i + ",'" + i.ToString() + "')";
                cn.Execute(aa, out dummy, -1);
                Application.DoEvents();

                Lab6.Text = (i / II) * 100 + " %";
            }

            cn.CommitTrans();

            Lab5.Text = "结束:" + DateTime.Now.ToString("HH:mm:ss-fff");

            cn.Close();

        }

        private void Btn3_Click(object sender, EventArgs e)
        {

            // 连接数据库
            SQLiteConnection conn = new SQLiteConnection(SQLiteConn);
            conn.Open();

            Lab7.Text = "开始:" + DateTime.Now.ToString("HH:mm:ss-fff");
              // 创建数据表
            SQLiteCommand cmd = new SQLiteCommand();
            cmd.Connection = conn;

            SQLiteTransaction trans = conn.BeginTransaction();
            try
            {
                for (int i = 1; i <= II; i++)
                {
                    cmd.CommandText = "insert into Test(Sid,Sname) values ("+i+",'"+i.ToString()+"')";
                    cmd.ExecuteNonQuery();
                    Application.DoEvents();

                    Lab9.Text = (i / II) * 100 + " %";
                }

                trans.Commit();
            }
            catch
            {
                trans.Rollback();
                throw;
            }

            Lab8.Text = "结束:" + DateTime.Now.ToString("HH:mm:ss-fff");
            conn.Close();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            DBEngine dbEngine = new DBEngine();
            Database db = dbEngine.OpenDatabase(Environment.CurrentDirectory + "\\TestDB.accdb");

            Recordset rs = db.OpenRecordset("Test");
            Field[] myFields = new Field[9];
            myFields[0] = rs.Fields["ID"];
            myFields[1] = rs.Fields["Sid"];
            myFields[2] = rs.Fields["Sname"];
            label3.Text = "开始:" + DateTime.Now.ToString("HH:mm:ss-fff");
            for (int i = 1; i <= II; i++)
            {
                rs.AddNew();

                myFields[1].Value = i;
                myFields[2].Value = i.ToString();

                rs.Update();

                label1.Text = (i / II) * 100 + " %";

            }

            label2.Text = "结束:" + DateTime.Now.ToString("HH:mm:ss-fff");
            rs.Close();
            db.Close();
        }

发布了1 篇原创文章 · 获赞 0 · 访问量 3368
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览