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

后端 未结 2 893
误落风尘
误落风尘 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...

提交回复
热议问题