当前位置:   article > 正文

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

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();
        }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/681647
推荐阅读
相关标签
  

闽ICP备14008679号