zoukankan      html  css  js  c++  java
  • 使用SqlSugarCore在.Net5可能会出现Sqlite字符串连接异常问题。

    为什么说这是一个可能出现的bug,因为这个bug很奇怪我试了创建了很多次项目去执行相同的代码却只有一个会先这种情况。

    SqlSugarCore版本:5.0.2.8

    程序版本:.net5--winform程序。

    错误

    SqlSugar.SqlSugarException:“English Message : Connection open error . The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.
    Chinese Message : 连接数据库过程中发生错误,检查服务器是否正常连接字符串是否正确,实在找不到原因请先Google错误信息:The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception..”

    代码

          var connectionString = new SqliteConnectionStringBuilder()
                {
                    //Mode = SqliteOpenMode.ReadWriteCreate,
                    DataSource = @"d:
    efund.db"
    
                }.ToString();
    
            SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = connectionString,//连接符字串
                    DbType = SqlSugar.DbType.Sqlite,
                    IsAutoCloseConnection = true,
                    ConfigId = "sqlite",
                    IsShardSameThread = true
                });
    
                db.Ado.ExecuteCommand("select 1");

    思路

    在出现这个错误之后我第一次检查的就是sqlite的连接字符串。在对比了很多次之后我排除了是连接字符串的问题。

    因为我已相同的连接字符串另外一个.net5的程序中跑的时候是没有问题的。

    因为SqlSugarCore的sqlite依赖包是Microsoft.Data.Sqlite。

    我试着用原生的SqliteConnection去尝试连接数据库。

    代码:

    var connectionString = new SqliteConnectionStringBuilder()
                {
                    //Mode = SqliteOpenMode.ReadWriteCreate,
                    DataSource = @"d:
    efund.db"
    
                }.ToString();
    
    
                using (SqliteConnection connection = new SqliteConnection(connectionString))
                {
                    connection.Open();
    
                    SqliteCommand command = new SqliteCommand("select 1", connection);
    
                    var reader = command.ExecuteReader();
                }

     出现错误。

    错误1:

    The type initializer for 'Microsoft.Data.Sqlite.SqliteConnection' threw an exception.”

    错误2

    MissingMethodException: Method not found: 'Int32 SQLitePCL.ISQLite3Provider.sqlite3_win32_set_directory(Int32, System.String)'.

    重点是错误2,然后去网上找了一圈也没有解决方案,然后群友说可能是程序包版本问题。

    SQLitePCLRaw.bundle_green  根据错误来看可能是这个依赖包有问题。

    我只得重新安装这个包的最新版本。

    然后运行程序,发现可以连接字符串。

    但是这个依赖包在安装SqlSugarCore的时候已经安装了,安装的也是最新的包,但是却会报错,真是百思不得其解啊。

    结语

    遇到问题还是得要有正确的思路要不然真的难。

  • 相关阅读:
    Reflective implementation of ToString using customer attribute
    [tips]SQL 2005 AND 2008
    443 Chapter8. Failover clustering not completed
    444.Counters of SQL Server 2005
    443 Chapter4.Designing Database Server Security Policies
    [From MSDN]Event ID 2295 — IIS W3SVC Module Configuration
    443.Chapter3
    XT711(大陆行货)刷机与优化指南
    关于app2sd、a2sd、data2sd、a2sd+的区别的解释
    Canvas.Top和Canvas.Left属性
  • 原文地址:https://www.cnblogs.com/aqgy12138/p/14689349.html
Copyright © 2011-2022 走看看