从C#使用H2数据库 [英] Working with H2 database from C#

查看:143
本文介绍了从C#使用H2数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

借助下面提到的URL,我可以从C#应用程序连接到H2数据库

I am able to connect to H2 database from my C# application with the help of below mentioned URL

http://www.h2database.com/html/advanced.html#microsoft_dot_net

org.h2.Driver.load();
Connection conn = DriverManager.getConnection("jdbc:h2:~/test", "sa", "sa");
Statement stat = conn.createStatement();

//Working
ResultSet rs = stat.executeQuery("SELECT 'Hello World'");
while (rs.next())
{
    Console.WriteLine(rs.getString(1));
}

//Not Working
ResultSet rs1 = stat.executeQuery("SELECT * FROM TEST");
while (rs1.next())
{
    Console.WriteLine(rs1.getString(1));
}

我将代码"SELECT'Hello World'"更改为"SELECT * FROM TEST",其中"TEST"是表名.我正在获取表未找到错误.但是该表存在于H2数据库中.

I have changed the code "SELECT 'Hello World'" to "SELECT * FROM TEST" where "TEST" is the table name. I am getting table not found error. But the table is present in H2 database.

编辑@尼克·布尔-非常感谢您的帮助.由于某些原因,无法通过c#代码访问在"H2控制台"中创建的表.我已经尝试过使用您的逻辑,从C#代码创建表,它起作用了!!但是它没有显示在H2控制台中.

EDIT @ Nick Bull- Thank you so much for your help. Due to some reason the table created in "H2 console" is not accessible through c# code. I have tried with your logic, creating the table from C# code, it worked!!. But it is not showing in H2 console.

还有一件事,"executeQuery"对我来说创建新表不起作用,我使用了"executeUpdate"(对某人可能有帮助)

One more thing, "executeQuery" did not work for me to create the new table, I used "executeUpdate" (for someone it may help)

推荐答案

我在Java上经常使用h2.我认为,在c#中创建表后可以访问c#中的表的原因是因为您正在查看的数据库与您在控制台中查看的数据库是不同的数据库.也许重新检查您的连接字符串(在c#和控制台中).

I've used h2 a lot with Java. I think that the reason you can access the table in c# when you've created it in c# is because the database you are looking at is a different one to the one you're looking at in the console. Maybe recheck your connection string (in both c# and the console).

如果h2驱动程序默认不存在数据库,则会创建数据库.

Databases are created if they don't exist by default by the h2 driver.

这篇关于从C#使用H2数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆