首页 > 解决方案 > .net C# jdbc 驱动程序连接器异常

问题描述

我正在尝试从 Visual Studio 中的 ac# 应用程序连接到本地计算机上的 hyperSQL 数据库 (HSQLDB)。

我已经完成了创建 .net JDBC 驱动程序以构建 dll 的步骤,并从以下 URL 下载了“下载”部分中的演示控制台应用程序:http: //nikolaiklimov.de/query-java-HyperSQL -database-with-csharp/

演示应用程序有效!我可以连接到数据库并查询其内容。接下来,我将控制台应用程序转换为类库,然后调用类库来查询数据库,但这就是它失败的地方,我得到 System.TypeInitializationException 的初始化错误。知道为什么项目在转换为类库后会崩溃。(如果我将类库直接转换回控制台应用程序,它会再次工作)。

代码和连接字符串是:

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=****

转换为类库后,我在我的解决方案中添加了一个控制台应用程序。控制台应用程序代码如下:

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)
            {

            }
        }
    }
}

这是下载我在 Visual Studio 中创建的解决方案的链接。它包含一个调用类库中的函数的控制台应用程序。类库 (HyperSQL) 本身只是从控制台应用程序转换而来,并且可以转换回来。您需要在您的机器上运行 HyperSQL 实例才能成功连接。 http://eliteservicedev.azurewebsites.net/DemoHyperSQL.zip

标签: c#.netjdbchsqldb

解决方案


Seems VS is not copying all IKVM DLLs to output folder due to not resolving they are needed. Please see this SourceForge issue and this GitHub issue, the later one with my comments.


推荐阅读