不可调用的 Hashtable.keys 成员不能用作方法
The non-invocable Hashtable.keys member can not be used as a method
我正在用 c# 创建一个小型 Windows 应用程序,并使用哈希表,当我想在存储过程中添加值时出现此错误。
错误:
"The non-invocable Hashtable.keys member can not be used as a method"
错误出现在hs.keys(n)
之后
这是我正在编写的代码:
public int EscribirBackup(string nombre, Hashtable hs)
{
SqlCommand cm = new SqlCommand();
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = nombre;
for (var n = 0; n <= hs.Values.Count - 1; n++)
cm.Parameters.AddWithValue(hs.Keys(n), hs.Values);
cm.Connection = ABRIR("NORMAL");
int i = cm.ExecuteNonQuery();
CERRAR();
return i;
}
现在,当我 运行 程序出现此错误时:
'There is no assignment of object type System.Collections.Hashtable + ValueCollection to a native type of a known managed provider.'
关于 int i = cm.ExecuteNonQuery();
Hashtable.Keys
是一个集合 属性(System.Collections
的成员),其定义如下:
public virtual System.Collections.ICollection Keys { get; }
请注意 ICollection
属性不公开索引器,您应该将其转换为其他公开索引器的集合。
因此,您应该首先使用 Cast<string>()
将其转换为数组或 List
实例,然后使用方括号访问指定的索引,如下例所示:
var keys = hs.Keys.Cast<string>().ToArray();
var values = hs.Values.Cast<string>().ToArray();
for (var n = 0; n <= hs.Values.Count - 1; n++)
{
cm.Parameters.AddWithValue(keys[n], values[n]);
}
我正在用 c# 创建一个小型 Windows 应用程序,并使用哈希表,当我想在存储过程中添加值时出现此错误。 错误: "The non-invocable Hashtable.keys member can not be used as a method"
错误出现在hs.keys(n)
之后这是我正在编写的代码:
public int EscribirBackup(string nombre, Hashtable hs)
{
SqlCommand cm = new SqlCommand();
cm.CommandType = CommandType.StoredProcedure;
cm.CommandText = nombre;
for (var n = 0; n <= hs.Values.Count - 1; n++)
cm.Parameters.AddWithValue(hs.Keys(n), hs.Values);
cm.Connection = ABRIR("NORMAL");
int i = cm.ExecuteNonQuery();
CERRAR();
return i;
}
现在,当我 运行 程序出现此错误时:
'There is no assignment of object type System.Collections.Hashtable + ValueCollection to a native type of a known managed provider.'
关于 int i = cm.ExecuteNonQuery();
Hashtable.Keys
是一个集合 属性(System.Collections
的成员),其定义如下:
public virtual System.Collections.ICollection Keys { get; }
请注意 ICollection
属性不公开索引器,您应该将其转换为其他公开索引器的集合。
因此,您应该首先使用 Cast<string>()
将其转换为数组或 List
实例,然后使用方括号访问指定的索引,如下例所示:
var keys = hs.Keys.Cast<string>().ToArray();
var values = hs.Values.Cast<string>().ToArray();
for (var n = 0; n <= hs.Values.Count - 1; n++)
{
cm.Parameters.AddWithValue(keys[n], values[n]);
}