How to form connection between HSQLDB and C# .net?

后端 未结 2 891
误落风尘
误落风尘 2021-01-20 15:47

How to form connection between HSQLDB and C# .net ? I have already looked at SharpHSQL and H2Sharp but not able to connect the HSQLDB.

相关标签:
2条回答
  • 2021-01-20 15:48

    try like this: Make sure you've already add hsqldb.dll, IKVM.OpenJDK.Core.dll, IKVM.OpenJDK.Jdbc.dll as reference. If you don't have the IKVM library, you can download here.

    At your C#:

    using java.sql; //add this.
    

    for create a connection:

    private Connection GetConnection()
        {
            DriverManager.registerDriver(new org.hsqldb.jdbcDriver());
            Connection conn = DriverManager.getConnection("jdbc:hsqldb:hsql://[host]/[db name]", "[username]", "[password]");
            return conn;
        }
    

    how to use it:

    public void LoadSetting(String userId)
        {
            Connection conn = null;
            try
            {
                //Connect it!
                conn = GetConnection();
    
                string query = "SELECT A.* FROM table A";
    
                PreparedStatement ps = conn.prepareStatement(query);
    
                ResultSet rs = ps.executeQuery();
                while (rs.next())
                {
                   //Get Query Result
                   Console.WriteLn(rs.getString("COL1"));
                   Console.WriteLn(rs.getString("COL2"));
                }
             }
    
             //Close the Connection
             finally
             {
                if (conn != null && !conn.isClosed())
                {
                    conn.close();
                    conn = null;
                }
             }
         }
    

    Hope this help.
    Cheers...

    0 讨论(0)
  • 2021-01-20 16:09

    Basic steps:

    1. Download IKVM.NET and HyperSQL (=HSQLDB) driver.

    2. Convert HSQLDB Java driver to .NET DLL using IKVM.NET to create a hsqldb.dll

    3. Add compile time dependencies to your C# project:

    • hsqldb.dll
    • IKVM.OpenJDK.Core.dll
    • IKVM.OpenJDK.Jdbc.dll
    1. Add runtime dependencies when running your C# program:
    • IKVM.OpenJDK.Localedata.dll
    • IKVM.OpenJDK.Text.dll
    • IKVM.OpenJDK.Util.dll
    • IKVM.Runtime.dll
    1. Add connection string to your app.config :
    <configuration>
    
      <connectionStrings>
        <add name="HyperSQL"
            connectionString="jdbc:hsqldb:hsql://localhost:9999/xdb;user=SA;password=;" />
      </connectionStrings>
    
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
      </startup>
    </configuration>
    1. C# code:
    using System;
    using System.Configuration;
     
    namespace HyperSQL
    {
        class Program
        {
            readonly static string CONNECTION_STRING = ConfigurationManager.ConnectionStrings["HyperSQL"].ConnectionString;
            const string SQL = "SELECT * FROM customer";
     
            static void Main(string[] args)
            {
                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($"ID={rs.getInt("id")}");
                            Console.WriteLine($"NAME={rs.getString("name")}");
                            Console.WriteLine($"AGE={rs.getInt("age")}");
                            Console.WriteLine($"ADDRESS={rs.getString("address")}");
                            Console.WriteLine($"SALARY={rs.getInt("salary")}");
     
                            Console.WriteLine("------------------");
                        }
                    }
                }
     
                Console.ReadLine();
            }
        }
    }

    Here my detailed tutorial how to connect to HyperSQL from C#

    0 讨论(0)
提交回复
热议问题