How to form connection between HSQLDB and C# .net ? I have already looked at SharpHSQL and H2Sharp but not able to connect the HSQLDB.
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...
Basic steps:
Download IKVM.NET and HyperSQL (=HSQLDB) driver.
Convert HSQLDB Java driver to .NET DLL using IKVM.NET to create a hsqldb.dll
Add compile time dependencies to your C# project:
<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>
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#