C#将字符串数组转换为数据集 [英] C# Convert string array to dataset

查看:196
本文介绍了C#将字符串数组转换为数据集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组,需要将它们转换成一个数据集。有没有一些快捷方式来做到这一点?例如:

I have an array of strings and need to convert them to a dataset. Is there some shortcut way to do this? For example:

string[] results = GetResults();    
DataSet myDataSet = new DataSet();

results = myDataSet.ToDataSet();  // Convert array to data set

我不太在意格式化或结构化。 >

I’m not too bothered about formatting or structure.

推荐答案

我看不到任何价值,但如果你真的必须按照你的要求,代码将使用单个表创建一个数据集,该表具有数组中每个项目的单个列和一行。

I can't see any value in this but if you really must do as you request, the following code will create a dataset with a single table that has a single column and a row per item in the array.

internal static class Program
{
    private static void Main(string[] args)
    {
        string[] array = new [] { "aaa", "bbb", "ccc" };
        DataSet dataSet = array.ToDataSet();
    }

    private static DataSet ToDataSet(this string[] input)
    {
        DataSet dataSet = new DataSet();
        DataTable dataTable = dataSet.Tables.Add();
        dataTable.Columns.Add();
        Array.ForEach(input, c => dataTable.Rows.Add()[0] = c);
        return dataSet;
    }
}

这篇关于C#将字符串数组转换为数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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