H2 语法错误
我们的应用程序使用 H2Sharp 从 C#(框架 v4)访问 H2 数据库。最近我们遇到了以下问题:以下代码
H2DataAdapter adapter;
DataTable table;
//initialize values
H2Command updateCommand = new H2Command("UPDATE MYTABLE SET NAME=@NAME, DOUBLECOLUMN=@DOUBLECOLUMN WHERE ID = @ID)", connection);
updateCommand.Parameters.Add(new H2Parameter("@NAME", DbType.String, 100, "NAME"));
updateCommand.Parameters.Add(new H2Parameter("@DOUBLECOLUMN", DbType.Double, 8, "DOUBLECOLUMN"));
updateCommand.Parameters.Add(new H2Parameter("@ID", DbType.StringFixedLength, 50, "ID"));
adapter.UpdateCommand = updateCommand;
在使用“,”作为小数点分隔符而不是“。”的区域性下以以下形式使用时会导致语法错误:
DataRow row = somehow_get_the_row();
row["NAME"] = "name";
row["DOUBLECOLUMN"] = some_double_with_fractional_part;
adapter.Update(table);
如果用户输入,则代码可以正常工作是一个没有小数部分的双精度数(即 39 有效,但 39,5 无效)。我假设 39.5 也可以,但 UI 不允许 CurrentCulture.NumberFormat.NumberDecimalSeparator
中未包含的字符,因此无法输入“。”在这种情况下。
堆栈顶部(直到我们的代码)如下所示:
h2-1.3.160.dll!org.h2.jdbc.JdbcConnection.prepareStatement(string str) + 0x1f4 bytes
H2Sharp.dll!System.Data.H2.H2Command.CreateStatement() Line 335 + 0x35 bytes
H2Sharp.dll!System.Data.H2.H2Command.EnsureStatment() Line 368 + 0x8 bytes
H2Sharp.dll!System.Data.H2.H2Command.ExecuteNonQuery() Line 438 + 0x8 bytes
System.Data.dll!System.Data.Common.DbDataAdapter.UpdateRowExecute(System.Data.Common.RowUpdatedEventArgs rowUpdatedEvent, System.Data.IDbCommand dataCommand, System.Data.StatementType cmdIndex) + 0x4f bytes
System.Data.dll!System.Data.Common.DbDataAdapter.Update(System.Data.DataRow[] dataRows, System.Data.Common.DataTableMapping tableMapping) + 0x6bc bytes
System.Data.dll!System.Data.Common.DbDataAdapter.UpdateFromDataTable(System.Data.DataTable dataTable, System.Data.Common.DataTableMapping tableMapping) + 0x2a bytes
System.Data.dll!System.Data.Common.DbDataAdapter.Update(System.Data.DataTable dataTable) + 0xb2 bytes
错误消息是
org.h2.jdbc.JdbcSQLException occurred
Message=Syntax error in SQL statement "UPDATE MYTABLE SET NAME=?, DOUBLECOLUMN=? WHERE ID = ?)"
Source=h2-1.3.160
我假设此区域性中 double 的字符串表示形式中的“,”正在破坏 SQL 语句。
完成这项工作的正确方法是什么?数据库表是否应该包含 double 类型的列,但参数作为采用不变区域性格式化的字符串传递?在这种情况下H2能正确解析吗?
该代码在 Windows 7、x64 下运行,H2 数据库为 v1.3.160。
预先感谢您的任何帮助。
Our application uses H2Sharp to access a H2 database from C# (Framework v4). Recently we bumped into the following issue: The following code
H2DataAdapter adapter;
DataTable table;
//initialize values
H2Command updateCommand = new H2Command("UPDATE MYTABLE SET NAME=@NAME, DOUBLECOLUMN=@DOUBLECOLUMN WHERE ID = @ID)", connection);
updateCommand.Parameters.Add(new H2Parameter("@NAME", DbType.String, 100, "NAME"));
updateCommand.Parameters.Add(new H2Parameter("@DOUBLECOLUMN", DbType.Double, 8, "DOUBLECOLUMN"));
updateCommand.Parameters.Add(new H2Parameter("@ID", DbType.StringFixedLength, 50, "ID"));
adapter.UpdateCommand = updateCommand;
leads to a syntax error later on when used in the following form when used under a culture with "," as the decimal separator instead of ".":
DataRow row = somehow_get_the_row();
row["NAME"] = "name";
row["DOUBLECOLUMN"] = some_double_with_fractional_part;
adapter.Update(table);
The code works fine if user input is a double without fractional part (i.e. 39 works but 39,5 does not work). I assume 39.5 would also work but the UI does not allow characters that are not included in the CurrentCulture.NumberFormat.NumberDecimalSeparator
so it is not possible to enter "." in this case.
The top of the stack (until our code) is like this:
h2-1.3.160.dll!org.h2.jdbc.JdbcConnection.prepareStatement(string str) + 0x1f4 bytes
H2Sharp.dll!System.Data.H2.H2Command.CreateStatement() Line 335 + 0x35 bytes
H2Sharp.dll!System.Data.H2.H2Command.EnsureStatment() Line 368 + 0x8 bytes
H2Sharp.dll!System.Data.H2.H2Command.ExecuteNonQuery() Line 438 + 0x8 bytes
System.Data.dll!System.Data.Common.DbDataAdapter.UpdateRowExecute(System.Data.Common.RowUpdatedEventArgs rowUpdatedEvent, System.Data.IDbCommand dataCommand, System.Data.StatementType cmdIndex) + 0x4f bytes
System.Data.dll!System.Data.Common.DbDataAdapter.Update(System.Data.DataRow[] dataRows, System.Data.Common.DataTableMapping tableMapping) + 0x6bc bytes
System.Data.dll!System.Data.Common.DbDataAdapter.UpdateFromDataTable(System.Data.DataTable dataTable, System.Data.Common.DataTableMapping tableMapping) + 0x2a bytes
System.Data.dll!System.Data.Common.DbDataAdapter.Update(System.Data.DataTable dataTable) + 0xb2 bytes
and the error message is
org.h2.jdbc.JdbcSQLException occurred
Message=Syntax error in SQL statement "UPDATE MYTABLE SET NAME=?, DOUBLECOLUMN=? WHERE ID = ?)"
Source=h2-1.3.160
I assume the "," from the string representation of double in this culture is breaking the SQL statement.
What would be a proper way to get this work? Should the database table contain columns of type double but arguments passed as strings formatted with an invariant culture? Will H2 do the parsing correctly in this case?
The code is running under Windows 7, x64 and H2 Database is v1.3.160.
Thanks in advance for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎是更新语句最后的
)
:应该是
The problem seems to be the
)
at the very end of the update statement:should be