【问题标题】:fill array from a dataset c#从数据集中填充数组c#
【发布时间】:2014-06-16 21:20:32
【问题描述】:

我有一个包含 6 个字符串的数据集。如何用数据集中的这些字符串填充字符串数组。我想象循环遍历数据集。
我有一个问答课。

这里是填充数据集的代码。

            SqlConnection connect = new SqlConnection(connectionString);
            SqlCommand command = new SqlCommand();
            command.Connection = connect;
            command.CommandType = CommandType.Text;
            command.CommandText = "Select * from Questions";

            SqlDataAdapter dataapt = new SqlDataAdapter(command);
            DataSet questions = new DataSet();

            try
            {
                connect.Open();
                dataapt.Fill(questions, "Questions");
            }

这是我的测验课

           public class Quiz
            {
              public string[] questions { get; set; }
              public string[] answers { get; set; }



    public Quiz()
    {
        questions = new string[] { "First", "Second", "Third", "Fourth","Fifth","sixth" };
        answers = new string[] { "First", "Second", "Third", "Fourth", "Fifth", "sixth" };
    }

是否可以从数据集中填充数组??

【问题讨论】:

  • 是的。让我们试试吧!

标签: c# asp.net arrays dataset sqlclient


【解决方案1】:

是的。可以办到。看看这个例子。

DataTable dt = new DataTable();
dt.Columns.Add("Questions");

dt.Rows.Add(new String[] { "One" });
dt.Rows.Add(new String[] { "Two" });
dt.Rows.Add(new String[] { "Three" });
dt.Rows.Add(new String[] { "Four" });
dt.Rows.Add(new String[] { "Five" });

String[] questions = dt.AsEnumerable().Select(x => x["Questions"].ToString()).ToArray();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多