Sqlite   发布时间:2019-11-06  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用System.Data.SQLite在C#app中缓慢打开SQLite连接大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑3:

我想我的问题暂时解决了……我将我的服务和测试应用程序更改为SYSTEM帐户而不是Networkservice帐户.更改用户帐户的好处是否会持续存在,或者只是暂时性的,还有待观察.

原始问题:

我注意到我的小型224kB sqlite数据库在我的C#应用​​程序中打开非常慢,从一些小的ms到1.5秒或更长时间.下面是我的代码,以及我今天下午添加的所有额外调试语句.我把它缩小到调用cnn.open();如下面的日志所示:

2014-03-27 15:05:39,864 DEBUG - CreaTing sqliteConnection...
2014-03-27 15:05:39,927 DEBUG - sqliteConnection Created!
2014-03-27 15:05:39,927 DEBUG - sqliteConnection Opening...
2014-03-27 15:05:41,627 DEBUG - sqliteConnection Opened!
2014-03-27 15:05:41,627 DEBUG - sqliteCommand CreaTing...
2014-03-27 15:05:41,627 DEBUG - sqliteCommand Created!
2014-03-27 15:05:41,627 DEBUG - sqliteCommand execuTing reader...
2014-03-27 15:05:41,658 DEBUG - sqliteCommand executed reader!
2014-03-27 15:05:41,658 DEBUG - DataTable Loading...
2014-03-27 15:05:41,767 DEBUG - DataTable Loaded!

如您所见,在这种情况下,打开连接需要1.7秒.我试过重复这个,无法预测后续连接是否会立即打开,或者像这样延迟.

虑过使用某种形式的连接池,但是对于单实例单线程应用程序来说,这是值得的吗?现在,我正在创建一个sqliteDatabase类的实例,并为每个查询调用以下函数.

public DataTable GetDataTable(String sql)
{
    DataTable dt = new DataTable();
    try
    {
        Logging.LogDebug("CreaTing sqliteConnection...");
        using (sqliteConnection cnn = new sqliteConnection(dbConnection))
        {
            Logging.LogDebug("sqliteConnection Created!");
            Logging.LogDebug("sqliteConnection Opening...");
            cnn.open();
            Logging.LogDebug("sqliteConnection Opened!");
            Logging.LogDebug("sqliteCommand CreaTing...");
            using (sqliteCommand mycommand = new sqliteCommand(cnn))
            {
                Logging.LogDebug("sqliteCommand Created!");
                mycommand.CommandText = sql;
                Logging.LogDebug("sqliteCommand execuTing reader...");
                using (sqliteDataReader reader = mycommand.ExecuteReader())
                {
                    Logging.LogDebug("sqliteCommand executed reader!");
                    Logging.LogDebug("DataTable Loading...");
                    dt.Load(reader);
                    Logging.LogDebug("DataTable Loaded!");
                    reader.Close();
                }
            }
            cnn.Close();
        }
    }
    catch (Exception E)
    {
        throw new Exception(e.messagE);
    }
    return dt;
}

编辑:

当然,dbConnection是连接字符串,由以下函数设置. inputFile只是要打开的文件名的字符串路径.

public sqliteDatabase(String inputFilE)
{
    dbConnection = String.Format("Data source={0}",inputFilE);
}

在这一点上,我认为sql是无关紧要的,因为当cnn.open()停止时,它没有达到这一点.

编辑2:

好的,我已经做了一些测试.在本地运行测试,它在~5秒内完成1000次迭代循环,每次调用cnn.open()大约5ms.从我在本地PC上执行的相同Windows安装程序运行测试,它在大约25分钟内完成,每次调用cnn.open()平均为1468ms.

做了一个小测试程序,它只从服务程序调用TestOpenConn()函数(在Windows服务中运行的相同的代码),针对位于测试目录中的文件副本运行.在服务器或我的本地PC上运行它会产生可接受的性能(服务器上每次呼叫1.95ms,本地PC上每次呼叫4ms):

namespace EGC_Timing_Test
{
    class Program
    {
        static void Main(String[] args)
        {
            Logging.Init("log4net.xml","test.log");
            var db = new sqliteDatabase("config.sqlite");
            db.TestOpenConn();
        }
    }
}

这是测试功能:

public void TestOpenConn()
{
    // TODO: Remove this after tesTing loop of opening / closing sqlite DB repeatedly:
    const int iterations = 1000;
    Logging.LogDebug(String.Format("Running TestOpenConn for {0} opens...",iterations));
    var startTime = datetiR_31_11845@e.Now;
    for (var i = 0; i < iterations; i++)
    {
        using (sqliteConnection cnn = new sqliteConnection(dbConnection))
        {
            Logging.LogDebug(String.Format("sqliteConnection Opening,iteration {0} of {1}...",i,iterations));
            var startTimeInner = datetiR_31_11845@e.Now;
            cnn.open();
            var endTimeInner = datetiR_31_11845@e.Now;
            var diffTimeInner = endTimeInner - startTimeInner;
            Logging.LogDebug(String.Format("sqliteConnection Opened in {0}ms!",diffTimeInner.@R_954_10586@lMilliseconds));
            cnn.Close();
        }
    }
    var endTime = datetiR_31_11845@e.Now;
    var diffTime = endTime - startTime;
    Logging.LogDebug(String.Format("Done running TestOpenConn for {0} opens!",iterations));
    Logging.LogInfo(String.Format("{0} iterations @R_954_10586@l:\t{1}",iterations,diffTimE));
    Logging.LogInfo(String.Format("{0} iterations average:\t{1}ms",diffTime.@R_954_10586@lMilliseconds/iterations));
}

解决方法

我想我的问题暂时解决了……我将我的服务和测试应用程序更改为SYSTEM帐户而不是Networkservice帐户.更改用户帐户的好处是否会持续存在,还有待观察.

大佬总结

以上是大佬教程为你收集整理的使用System.Data.SQLite在C#app中缓慢打开SQLite连接全部内容,希望文章能够帮你解决使用System.Data.SQLite在C#app中缓慢打开SQLite连接所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。