如何将Dictionary变量传递给另一个过程 [英] How to pass a Dictionary variable to another procedure

查看:345
本文介绍了如何将Dictionary变量传递给另一个过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个C#VS2008 / SQL Server网站应用程序。我以前从未使用过Dictionary类,但是我试图用Dictionary变量替换Hashtable。



这是aspx.cs代码的一部分: / p>

  ... 
字典< string,string> openWith = new Dictionary< string,string>();

(int col = 0; col< headers.Length; col ++)
{
@temp =(col + 1);
@tempS = @ temp.ToString();
@tempT =@col+ @ temp.ToString();
...
openWith.Add(@tempT,headers [col]);
}
...
for(int r = 0; r< myInputFile.Rows.Count; r ++)
{resultLabel.Text = ADONET_methods.AppendDataCT(myInputFile,openWith );

但是这最后一行给我一个编译器错误:
参数'2 ':无法从'System.Collections.Generic.Dictionary'转换为'string'



如何将整个openWith变量传递给AppendDataCT? AppendDataCT是调用我的SQL存储过程的方法。我想传递整行,每行都有一组唯一的值,我想添加到我的数据库表中。例如,如果每行需要单元格A,B和C的值,那么我想将这3个值传递给AppendDataCT,其中所有这些值都是字符串。























AppendDataCT ,它期待一个字符串参数,而不是一个字典<字符串,字符串>



假设这个,你可能想要这样的东西....

  foreach(OpenVith中的KeyValuePair< string,string>项)
{
resultLabel.Text = ADONET_methods.AppendDataCT(myInputFile,item.Value);
}

这将遍历您的字典,并且对于每一对[key,value ]
调用您的AppendDataCT方法,使用该对中的值。


I am developing a C# VS2008/SQL Server website application. I've never used the Dictionary class before, but I am trying to replace my Hashtable with a Dictionary variable.

Here is a portion of my aspx.cs code:

...
Dictionary<string, string> openWith = new Dictionary<string, string>(); 

for (int col = 0; col < headers.Length; col++)
{
   @temp = (col + 1);
   @tempS = @temp.ToString();
   @tempT = "@col" + @temp.ToString();
   ...
   openWith.Add(@tempT, headers[col]);
}
...
for (int r = 0; r < myInputFile.Rows.Count; r++)
 { resultLabel.Text = ADONET_methods.AppendDataCT(myInputFile, openWith); }

But this is giving me a compiler error on this last line: Argument '2': cannot convert from 'System.Collections.Generic.Dictionary' to 'string'

How do I pass the entire openWith variable to AppendDataCT? AppendDataCT is the method that calls my SQL stored proc. I want to pass in the whole row where each row has a unique set of values that I want to add to my database table. For example, if each row requires values for cells A, B, and C, then I want to pass these 3 values to AppendDataCT, where all of these values are strings. How do I do this with Dictionary?

解决方案

the problem is not your dictionary and its not how you are passing it. the problem is your method AppendDataCT, it is expecting a string parameter, not a dictionary<string,string>

assuming this, you probably want something kinda like this....

foreach( KeyValuePair<string,string> item in openWith )
{
    resultLabel.Text = ADONET_methods.AppendDataCT(myInputFile, item.Value);
}

this will iterate over your dictionary, and for every pair of [key,value] call your AppendDataCT method, using the value from the pair.

这篇关于如何将Dictionary变量传递给另一个过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆