使用 Sybase 和 DataAdapter
我正在.NET 2.0 中编写一个小型Windows 窗体应用程序。
我有一个 OdbcDataAdapter 具有像这样指定的插入和更新语句。 select 语句也在该代码的前面定义。
OdbcCommand insertCommand = _connection.CreateCommand();
insertCommand.CommandText = "INSERT INTO myTable (ReportID) VALUES (?ReportId)";
_dataAdapter.InsertCommand = insertCommand;
_dataAdapter.InsertCommand.Parameters.Add(new OdbcParameter { ParameterName = "?ReportID", SourceColumn = "ReportID" });
OdbcCommand updateCommand = _connection.CreateCommand();
updateCommand.CommandText = @"
UPDATE
myTable
SET
ReportID = ?ReportID
WHERE ID = ?ID";
_dataAdapter.UpdateCommand = updateCommand;
_dataAdapter.UpdateCommand.Parameters.Add(new OdbcParameter { ParameterName = "?ID", SourceColumn = "ID"});
_dataAdapter.UpdateCommand.Parameters.Add(new OdbcParameter { ParameterName = "?ReportID", SourceColumn = "ReportID"});
无论如何,问题就在这里。当我调用 _dataAdapter.Update(_table) 时,它适用于插入,但在更新时抛出错误。它显示:错误 [22018][SYBASE][ODBC Sybase 驱动程序][SQL Server]不允许从数据类型“NUMERIC”到“VARCHAR”的隐式转换。使用 CONVERT 函数运行此查询。
检查变量,没有理由发生这种情况。 ReportID 是一个 varchar,但由于某些奇怪的原因,在更新时被视为数字。为什么会发生这种情况?
更新
刚刚发现这是由于我设置参数的顺序造成的。我想无论如何我都会发布这个,以防其他人陷入困境。
I'm writing a small Windows form application in .NET 2.0.
I have an OdbcDataAdapter has the Insert and Update statements specified like so. The select statement is also defined earlier in this code.
OdbcCommand insertCommand = _connection.CreateCommand();
insertCommand.CommandText = "INSERT INTO myTable (ReportID) VALUES (?ReportId)";
_dataAdapter.InsertCommand = insertCommand;
_dataAdapter.InsertCommand.Parameters.Add(new OdbcParameter { ParameterName = "?ReportID", SourceColumn = "ReportID" });
OdbcCommand updateCommand = _connection.CreateCommand();
updateCommand.CommandText = @"
UPDATE
myTable
SET
ReportID = ?ReportID
WHERE ID = ?ID";
_dataAdapter.UpdateCommand = updateCommand;
_dataAdapter.UpdateCommand.Parameters.Add(new OdbcParameter { ParameterName = "?ID", SourceColumn = "ID"});
_dataAdapter.UpdateCommand.Parameters.Add(new OdbcParameter { ParameterName = "?ReportID", SourceColumn = "ReportID"});
Anyway, here's the issue. When I call _dataAdapter.Update(_table) it works on inserts, but throws an error on Updates. It says: ERROR [22018][SYBASE][ODBC Sybase driver][SQL Server]Implicit conversion from datatype 'NUMERIC' to 'VARCHAR' is not allowed. Use the CONVERT function to run this query.
Examining the variables, there's no reason for this to be happening. ReportID is a varchar, but for some odd reason treated like a numeric on updates. Why is this happening?
UPDATE
Just figured out that it's due to the order I was setting up the parameters. I thought I'd post this anyway just in case someone else gets stuck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论