c# - 无法访问 SQLite 数据库窗体窗口服务

标签 c# sqlite windows-services

我为 outlook 2010 开发了一个插件。在 outlook 启动时,插件将所有邮件主题存储在 SQLite 数据库中。

现在我必须创建一个也将访问相同数据库的 Windows 服务,并将其发布到基于休息的服务上。但是我收到一个错误。

Windows 服务无法访问该数据库。

 static void Main()
    {
        //try
        //{
            //=======Connection string=============
        string dbfile = @"D:\testdatabase.db";

        sql_con = new SQLiteConnection
            ("Data Source= " + dbfile + ";Version=3;New=False;Compress=True;");
            //=======Open Connection=============
            sql_con.Open();
            //=======Create dataset and store value in dataset===================
            sql_cmd = sql_con.CreateCommand();
            string CommandText = "select * from mailbackup";
            DB = new SQLiteDataAdapter(CommandText, sql_con);
            //DS.Reset();
            DB.Fill(DS);
            //=======Store dataset value in text file============
            StringBuilder str = new StringBuilder();
            int rows = DS.Tables[0].Rows.Count;
            for (int i = 0; i < rows;i++ )
            {
                str.AppendLine(DS.Tables[0].Rows[i].ItemArray[0].ToString());
                str.AppendLine(DS.Tables[0].Rows[i].ItemArray[1].ToString());
                str.AppendLine(DS.Tables[0].Rows[i].ItemArray[2].ToString());
                str.AppendLine(DS.Tables[0].Rows[i].ItemArray[3].ToString());
                str.AppendLine(DS.Tables[0].Rows[i].ItemArray[4].ToString());
                str.AppendLine(DS.Tables[0].Rows[i].ItemArray[5].ToString());
                str.AppendLine(DS.Tables[0].Rows[i].ItemArray[6].ToString());
                str.AppendLine(DS.Tables[0].Rows[i].ItemArray[7].ToString());
                str.AppendLine(DS.Tables[0].Rows[i].ItemArray[8].ToString());
                StreamWriter sw = null;
                sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + "\\LogFile.txt", true);
                sw.WriteLine(DateTime.Now.ToString() + ": " + str.ToString());
                sw.Flush();
                sw.Close();
            }
                //=======Close Connection=============
                sql_con.Close();
            //storedata();
        //}
        //catch(System.Exception ex)
        //{

        //}
    }

我遇到了一个错误

An unhandled exception of type 'Finisar.SQLite.SQLiteException' occurred in SQLite.NET.dll

Additional information: unsupported file format

当我们更改连接字符串时它工作正常

 sql_con = new SQLiteConnection
        ("Data Source= " + dbfile + ";Version=3;New=True;Compress=True;");

创建新数据库并打开连接,但在使用 sqlite 浏览器或 SQLiteStudio 创建表后,它开始抛出相同的错误。

最佳答案

在windows服务中运行什么样的应用程序并不重要。问题来自 SQLite 组件。

An unhandled exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll Additional information: Mixed mode assembly is built against version v2.0.50727 of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.

为了使用 CLR 2.0 混合模式程序集,您需要修改 App.Config 文件以包括:

<?xml version="1.0"?>
 <configuration>
  <startup useLegacyV2RuntimeActivationPolicy="true">
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>

关键是 useLegacyV2RuntimeActivationPolicy 标志。这会导致 CLR 使用最新版本 (4.0) 加载您的混合模式程序集。没有这个,它将无法工作。

请注意,这仅适用于混合模式 (C++/CLI) 程序集。您可以加载所有托管的 CLR 2 程序集,而无需在 app.config 中指定它。

无论如何,Considerations for server-side Automation of Office文章陈述如下:

Microsoft 目前不推荐也不支持来自任何无人值守、非交互式客户端应用程序或组件(包括 ASP、ASP.NET、DCOM 和 NT 服务)的 Microsoft Office 应用程序自动化,因为 Office在此环境中运行 Office 时,可能会表现出不稳定的行为和/或死锁。

如果您要构建在服务器端上下文中运行的解决方案,则应尝试使用已针对无人值守执行安全处理的组件。或者,您应该尝试找到至少允许部分代码在客户端运行的替代方案。如果您使用来自服务器端解决方案的 Office 应用程序,该应用程序将缺少许多成功运行所必需的功能。此外,您还需要承担整体解决方案稳定性的风险。

关于c# - 无法访问 SQLite 数据库窗体窗口服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28314997/

相关文章:

php - 拉维尔 5.1 : Enable SQLite foreign key constraints

java - JavaExe下Windows服务启动失败: Updated

c# - RabbitMQ 消费者恢复

c# - ASP.NET Core 3.0 - InProcess 与 OutOfProcess(HTTP 错误 500.30 - ANCM 进程内启动失败)

c# - 如何在 HeadlessChrome 中解决 "You are using an unsupported command-line flag: --ignore-certificate-errors, Stability and security will suffer"

c# - 如何防止第一次在 C# 中运行的事件

android - 我想将我的数据从 mysql 加载到 sqlite

c# 你的 SQL 语法有错误

java - 最接近的匹配值 sqlite

c++ - 什么是无效,更新方法在 VC++ 中做什么