简体   繁体   English

.net C# jdbc 驱动程序连接器异常

[英].net C# jdbc driver connector exception

I'm trying to connect to a hyperSQL database (HSQLDB) on my local machine from ac# application in visual studio.我正在尝试从 Visual Studio 中的 ac# 应用程序连接到本地机器上的 hyperSQL 数据库 (HSQLDB)。

I've ran through the steps to create the .net JDBC driver to build the dll, and also downloading the demo console application in the "download" section from the following url: http://nikolaiklimov.de/query-java-HyperSQL-database-with-csharp/我已经完成了创建 .net JDBC 驱动程序以构建 dll 的步骤,并从以下 URL 的“下载”部分下载了演示控制台应用程序: http : //nikolaiklimov.de/query-java-HyperSQL -带有csharp的数据库/

The demo application works!!演示应用程序有效!! i can connect to the database and query its contents .我可以连接到数据库并查询其内容。 Next I have converted the console app into a class library then call the class library to query the DB but this is where it falls over and I get initialization errors of System.TypeInitializationException.接下来,我将控制台应用程序转换为类库,然后调用类库来查询数据库,但这就是它失败的地方,我收到 System.TypeInitializationException 的初始化错误。 any idea why the project falls over after converting to a class library.知道为什么项目在转换为类库后会失败。 (if i convert the class library straight back a console app it works again). (如果我将类库直接转换回控制台应用程序,它会再次工作)。

The code and connection string are:代码和连接字符串是:

namespace HyperSQL
{
public static class sqlconnector
{
             readonly static string CONNECTION_STRING =   
  ConfigurationManager.ConnectionStrings["HyperSQL"].ConnectionString;
             const string SQL = "SELECT * FROM meeting";

    public static void getdata()
    {
        try
        {


        java.sql.DriverManager.registerDriver(new org.hsqldb.jdbcDriver());
        using (java.sql.Connection conn = java.sql.DriverManager.getConnection(CONNECTION_STRING))
        {
            java.sql.PreparedStatement ps = conn.prepareStatement(SQL);
            using (java.sql.ResultSet rs = ps.executeQuery())
            {
                while (rs.next())
                {
                    Console.WriteLine($"MEETING_NO={rs.getInt("MEETING_NO")}");

                    Console.WriteLine("------------------");
                }
            }
        }
        }
        catch (Exception ex)
        {

        }

        Console.ReadLine();
    }
}
}

jdbc:hsqldb:hsql://localhost:3458/elitedb;crypt_key=DADADADADAADDADAD;crypt_type=AES;shutdown=true;write_delay=false;user=****;password=****

Ive added a console app to my solution after the conversion to a class libary.在转换为类库后,我向我的解决方案添加了一个控制台应用程序。 The console app code is below:控制台应用程序代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                HyperSQL.sqlconnector.getdata();
            }
            catch (Exception ex)
            {

            }
        }
    }
}

Here is a link to download my solution created in visual studio.这是下载我在 Visual Studio 中创建的解决方案的链接。 It contains a console application that calls a function in a class library.它包含调用类库中的函数的控制台应用程序。 The class library (HyperSQL) itself is simply converted from a console application and can be converted back.类库 (HyperSQL) 本身只是从控制台应用程序转换而来,并且可以转换回来。 You will need a running instance of HyperSQL on your machine to be able to connect successfully.您需要在您的机器上运行 HyperSQL 实例才能成功连接。 http://eliteservicedev.azurewebsites.net/DemoHyperSQL.zip http://eliteservicedev.azurewebsites.net/DemoHyperSQL.zip

Have you seen the error listed in the original site you posted?您是否看到您发布的原始站点中列出的错误?

If your forget to add runtime dependencies to the build folder of your project then you can get these errors: Could not load file or assembly 'IKVM.OpenJDK.Util, Version=8.1.5717.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58' or one of its dependencies.如果您忘记将运行时依赖项添加到项目的构建文件夹,那么您可能会收到以下错误:无法加载文件或程序集 'IKVM.OpenJDK.Util, Version=8.1.5717.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58' or one它的依赖关系。 The system cannot find the file specified.该系统找不到指定的文件。 TypeInitializationException: The type initializer for 'org.hsqldb.jdbc.JDBCDriver' threw an exception: FileNotFoundException: Could not load file or assembly 'IKVM.Runtime, Version=8.1.5717.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58' or one of its dependencies. TypeInitializationException:“org.hsqldb.jdbc.JDBCDriver”的类型初始值设定项引发异常:FileNotFoundException:无法加载文件或程序集“IKVM.Runtime,版本=8.1.5717.0,Culture=neutral,PublicKeyToken=13235d27fcbfff58”或其中之一依赖关系。 'The type initializer for 'sun.util.locale.provider.LocaleProviderAdapter' threw an exception.' ''sun.util.locale.provider.LocaleProviderAdapter' 的类型初始值设定项抛出异常。 FileNotFoundException: Could not load file or assembly 'IKVM.OpenJDK.Text, Version=8.1.5717.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58' or one of its dependencies. FileNotFoundException:无法加载文件或程序集“IKVM.OpenJDK.Text,版本=8.1.5717.0,Culture=neutral,PublicKeyToken=13235d27fcbfff58”或其依赖项之一。 'The type initializer for 'org.hsqldb.HsqlDateTime' threw an exception.' “'org.hsqldb.HsqlDateTime' 的类型初始值设定项抛出异常。” InvalidCastException: Unable to cast object of type 'java.util.PropertyResourceBundle' to type 'sun.util.resources.OpenListResourceBundle'. InvalidCastException:无法将“java.util.PropertyResourceBundle”类型的对象转换为“sun.util.resources.OpenListResourceBundle”类型。

***** UPDATE With the source project i see an error, looking in the bin\\debug folder i see that non all the referenced file are copied to ConsoleApp1.exe bin folder: this is the compare result: ***** 更新对于源项目,我看到一个错误,在 bin\\debug 文件夹中我看到非所有引用的文件都被复制到 ConsoleApp1.exe bin 文件夹:这是比较结果:

Confronto in corso dei file consoleapp1.txt e DEMOCSHARPTHYPERSQL.TXT
***** consoleapp1.txt
ConsoleApp1.exe
ConsoleApp1.exe.config
ConsoleApp1.pdb
hsqldb.dll
HyperSQL.dll
***** DEMOCSHARPTHYPERSQL.TXT
hsqldb.dll
HyperSQL.dll
*****

***** consoleapp1.txt
IKVM.OpenJDK.Jdbc.dll
IKVM.OpenJDK.Text.dll
***** DEMOCSHARPTHYPERSQL.TXT
IKVM.OpenJDK.Jdbc.dll
IKVM.OpenJDK.Localedata.dll --> Missing in ConsoleApp1.exe folder
IKVM.OpenJDK.Text.dll
*****

If i manually copy the missing dll on ConsoleApp1.exe folder the program run correctly.如果我手动复制 ConsoleApp1.exe 文件夹中丢失的 dll,程序将正确运行。 I think that is a visual studio bug我认为这是一个视觉工作室的错误

Seems VS is not copying all IKVM DLLs to output folder due to not resolving they are needed.由于不需要解析它们,VS 似乎没有将所有 IKVM DLL 复制到输出文件夹。 Please see this SourceForge issue and this GitHub issue , the later one with my comments.请参阅此SourceForge 问题和此GitHub 问题,后者带有我的评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM