我有一个包含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" };
}
可以从数据集中填充数组吗??
是。这是可以做到的。看看这个例子。
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();