简体   繁体   English

在c#中将数据集元素转换为字符串数组

[英]Convert Dataset element to string array in c#

I am trying to convert the dataset item into the string and save into the array. 我试图将数据集项转换为字符串并保存到数组中。 But it gives me syntax error. 但它给了我语法错误。 Please help me out. 请帮帮我。 This in VS 2005 and C# 这在VS 2005和C#中

Thanks in advance!! 提前致谢!!

string strdetailID[] = new string[4];
for(int i = 0; i < x ; i++)
{
   strdetailID[i] = dsImages.Tables[0].Rows[i]["Ad_detailsID"].ToString();
}
List<string> strDetailIDList = new List<string>();    

foreach(DataRow row in dsImages.Tables[0].Rows)
{
    strDetailIDList.Add(row["Ad_detailsID"].ToString());
}

string strDetailID[] = strDetailIDList.ToArray();

I have used a List<string> instead of a string array, as I think it would be easier to dynamically add elements, but if you want to still use a string array, you should get the general idea. 我使用了List<string>而不是string数组,因为我认为动态添加元素会更容易,但是如果你仍想使用string数组,你应该得到一般的想法。

string[] strdetailID = new string[4];
for(int i = 0; i < x ; i++)
{
   strdetailID[i] = dsImages.Tables[0].Rows[i]["Ad_detailsID"].ToString();
}

This will work ..you made a mistake string strdetailID[] 这将工作..你犯了一个错误字符串strdetailID []

It is possible to size array before for each loop and form array directly without list. 可以在每个循环之前调整数组大小并直接形成数组而不使用列表。

string [] strDetailID = new string[dsImages.Tables[0].Rows.Count];   
int arryIndex = 0;
foreach(DataRow row in dsImages.Tables[0].Rows)
{
    strDetailID [arryIndex] = row["Ad_detailsID"].ToString());
    arryIndex++;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM