oledb忽略sql anywhere-db上的参数
oledb diregarding parameters on sqlanywhere-db
我有一个 SqlContext
class 是为 SQL 服务器、MySql、Odbc 和 Oledb 实现的。它提供插入、更新和删除等基本功能。在我必须连接到 SqlAnywhere 数据库之前,一切正常。当我调用更新时,我收到一条错误消息,指出未找到该列。
这是更新函数的 Odbc 实现:
public override int Update(string tableName, Action<ISqlParameterizer> actionSqlParameterizerUpdate, Action<ISqlParameterizer> actionSqlParameterizerWhere)
{
var commandText = $"update {tableName} set @update where @where";
var whereBuilder = new StringBuilder();
var updateBuilder = new StringBuilder();
var whereParameterizer = SqlParameterizer.CreateFrom(this);
var updateParameterizer = SqlParameterizer.CreateFrom(this);
actionSqlParameterizerWhere?.Invoke(whereParameterizer);
actionSqlParameterizerUpdate?.Invoke(updateParameterizer);
List<IDbDataParameter> parameterList = new List<IDbDataParameter>();
foreach (var whereParameter in whereParameterizer.GetParameters())
{
whereBuilder.Append($" and {whereParameter.ParameterName} = @{whereParameter.ParameterName}");
parameterList.Add(whereParameter);
}
foreach (var updateParameter in updateParameterizer.GetParameters())
{
updateBuilder.Append($", {updateParameter.ParameterName} = @{updateParameter.ParameterName}");
parameterList.Add(updateParameter);
}
commandText = commandText.Replace("@where", whereBuilder.ToString().Substring(4));
commandText = commandText.Replace("@update", updateBuilder.ToString().Substring(1));
return base.ExecuteNonQuery(commandText, parameterList.ToArray());
}
我这样称呼它:
base.Source.Update("SOME_TABLE", set =>
{
set.Add("ANWENDER_ID", anwender_id); //Store some FK in here
},where =>
{
where.Add("user_name", user_name); //For a specific user (user_name is pk)
});
异常信息:
ERROR [42S22] [SAP][ODBC Driver][SQL Anywhere]Spalte '@user_name' nicht gefunden
它说 "column '@user_name' not found"。
commandText
看起来像这样:
update SOME_TABLE set ANWENDER_ID = @ANWENDER_ID where user_name = @user_name
我的SqlCommand
参数填写正确。为什么说找不到该列?该列存在并且具有正确的名称。当我复制 commandText
并填写参数时,我可以执行查询。有人可以解释一下吗?
您的 CommandText 中的@parameter 不是 SQLAnywhere 的有效语法。
Placeholders for parameters in SQL statements are indicated by the ? character. For any INSERT, UPDATE, or DELETE, each ? is referenced according to its ordinal position in the command's parameters collection. For example, the first ? is referred to as 0, and the second as 1.
因此您的 CommandText 需要如下所示:
update SOME_TABLE set ANWENDER_ID = ? where user_name = ?
我有一个 SqlContext
class 是为 SQL 服务器、MySql、Odbc 和 Oledb 实现的。它提供插入、更新和删除等基本功能。在我必须连接到 SqlAnywhere 数据库之前,一切正常。当我调用更新时,我收到一条错误消息,指出未找到该列。
这是更新函数的 Odbc 实现:
public override int Update(string tableName, Action<ISqlParameterizer> actionSqlParameterizerUpdate, Action<ISqlParameterizer> actionSqlParameterizerWhere)
{
var commandText = $"update {tableName} set @update where @where";
var whereBuilder = new StringBuilder();
var updateBuilder = new StringBuilder();
var whereParameterizer = SqlParameterizer.CreateFrom(this);
var updateParameterizer = SqlParameterizer.CreateFrom(this);
actionSqlParameterizerWhere?.Invoke(whereParameterizer);
actionSqlParameterizerUpdate?.Invoke(updateParameterizer);
List<IDbDataParameter> parameterList = new List<IDbDataParameter>();
foreach (var whereParameter in whereParameterizer.GetParameters())
{
whereBuilder.Append($" and {whereParameter.ParameterName} = @{whereParameter.ParameterName}");
parameterList.Add(whereParameter);
}
foreach (var updateParameter in updateParameterizer.GetParameters())
{
updateBuilder.Append($", {updateParameter.ParameterName} = @{updateParameter.ParameterName}");
parameterList.Add(updateParameter);
}
commandText = commandText.Replace("@where", whereBuilder.ToString().Substring(4));
commandText = commandText.Replace("@update", updateBuilder.ToString().Substring(1));
return base.ExecuteNonQuery(commandText, parameterList.ToArray());
}
我这样称呼它:
base.Source.Update("SOME_TABLE", set =>
{
set.Add("ANWENDER_ID", anwender_id); //Store some FK in here
},where =>
{
where.Add("user_name", user_name); //For a specific user (user_name is pk)
});
异常信息:
ERROR [42S22] [SAP][ODBC Driver][SQL Anywhere]Spalte '@user_name' nicht gefunden
它说 "column '@user_name' not found"。
commandText
看起来像这样:
update SOME_TABLE set ANWENDER_ID = @ANWENDER_ID where user_name = @user_name
我的SqlCommand
参数填写正确。为什么说找不到该列?该列存在并且具有正确的名称。当我复制 commandText
并填写参数时,我可以执行查询。有人可以解释一下吗?
您的 CommandText 中的@parameter 不是 SQLAnywhere 的有效语法。
Placeholders for parameters in SQL statements are indicated by the ? character. For any INSERT, UPDATE, or DELETE, each ? is referenced according to its ordinal position in the command's parameters collection. For example, the first ? is referred to as 0, and the second as 1.
因此您的 CommandText 需要如下所示:
update SOME_TABLE set ANWENDER_ID = ? where user_name = ?