Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在哈希表中存储SqlParameter_C#_Sqlparameter - Fatal编程技术网

C# 如何在哈希表中存储SqlParameter

C# 如何在哈希表中存储SqlParameter,c#,sqlparameter,C#,Sqlparameter,如何将SqlParameter存储在哈希表中?在下面的示例中,我试图将P放在param中 Hashtable param = new Hashtable(); SqlParameter P = new SqlParameter("@Picture", SqlDbType.Varbinary, b.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, b); 使用上述变量名向哈希表中添加值的基本语

如何将
SqlParameter
存储在
哈希表中?在下面的示例中,我试图将
P
放在
param

Hashtable param = new Hashtable();
SqlParameter P = new SqlParameter("@Picture", SqlDbType.Varbinary, b.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, b);

使用上述变量名向哈希表中添加值的基本语法为:

param.Add("Picture", P);
其中“Picture”是一个键,您可以稍后通过以下方式从哈希表中检索该项,例如:

SqlParameter retrieved = (SqlParameter)param["Picture"];

也就是说,正如其他人已经对你的问题发表了评论一样,可能值得考虑使用字典而不是哈希表。字典是哈希表的类型化实例,因此您可以指定要插入的对象的类型,从而防止向其中添加任何旧垃圾(当然,除非您有理由不希望键入它)。我相信在拳击方面,有一个打字字典也会带来性能上的好处…

你应该使用
字典
,而不是
哈希表
@SLaks:他的问题最少:|如果你问你想做什么,OP,你会得到更好的帮助。具体问题是什么?特别是,您希望参数是键还是值?你试过什么?发生了什么事?真的。很高兴看到您希望如何从哈希表(应该是字典)中检索它。请注意,使用
哈希表
,检索时需要强制转换。(而且,
string
SqlParameter
不涉及装箱)