注意:实用程序类的源包含在附件中。 有关详细信息,请参见本文末尾。
因此,有时我需要使用DataSet来进行原型设计或只是尝试使用它们,而我总是发现创建它们很麻烦。 Visual Studio有一个设计师,但是我一直发现您需要连接到数据库(或Access文件)来创建它们,这比我需要一些快速数据来做时要做的更多。
为此,我做了一个小实用程序,可以在2D数组中定义一些数据,保留这些数据的列表,然后将它们作为表加载到DataSet中。 我意识到可能有很多方法可以做到这一点,但是我发现它很适合我,并且由于我发现它相当有用,所以我希望与他人分享它,希望其他人也能从中受益。
我创建的实用程序支持无类型数据(定义为字符串并以这种方式加载)或有类型数据(指定的类型,加载指定的数据类型)。 这是一个静态的全局类,上面有获取数据的方法。 命名空间是
DataFactories ,该类是DataFactory 。 可用的方法有.../// <summary>
/// Builds the sample data from a list of table definitions.
/// </summary>
/// <param name="tableDefinitions">The TableDefinitions object containing the table data.</param>
/// <param name="columnRow">The row specifying the column. Optional, defaults to 0.</param>
/// <param name="typeRow">The row specifying the types. Optional, defaults to -1 (no types define, defaulting to string data).</param>
/// <param name="dataSetName">The name of the data set. Optional, defaults to no name.</param>
/// <returns>A DataSet containing the created DataTable objects.</returns>
public static DataSet BuildDataSet(TableDefinitions tableDefinitions, int columnRow = 0, int typeRow = -1, string dataSetName = "")
/// <summary>
/// Builds the sample data from a list of table definitions.
/// </summary>
/// <param name="tableDefinitions">The list of table definitions.</param>
/// <param name="columnRow">The row specifying the column. Optional, defaults to 0.</param>
/// <param name="typeRow">The row specifying the types. Optional, defaults to -1 (no types define, defaulting to string data).</param>
/// <param name="dataSetName">The name of the data set. Optional, defaults to no name.</param>
/// <returns>A DataSet containing the created DataTable objects.</returns>
public static DataSet BuildDataSet(List<KeyValuePair<string, object[][]>> tableDefinitions, int columnRow = 0, int typeRow = -1, string dataSetName = "")
/// <summary>
/// Creates a DataTable from the parameters provided.
/// </summary>
/// <param name="tableName">The name of the table to create.</param>
/// <param name="rawDataTable">The definition of the table.</param>
/// <param name="columnRow">The row number the column names can be found on (typically the first row).</param>
/// <param name="typeRow">The row number the types can be found on. Set this to -1 if all types are to be strings.</param>
/// <returns></returns>
public static DataTable BuildDataTable(string tableName, object[][] rawDataTable, int columnRow = 0, int typeRow = -1)
我发现使用此方法的最佳方法是在我的项目中创建一个名为SampleData的新类。
在此类中,我提供了一个静态方法,该方法返回要加载的所有数据。
我将为需要一个数据集的每个解决方案创建此数据,以自定义返回的数据。
现在,对于实际数据...我将其定义为二维数组,这使我可以将其设置为外观,使其易于阅读。
如前所述,该实用程序支持加载两种数据,字符串数据和类型化数据,因此让我们来看一个使用字符串数据的示例。
可以说我有一个员工表...所以也许我会做这样的事情...
private static string[][] RAW_EMPLOYEES = new string[][]
{
new string[] { "ID", "FIRST_NAME", "LAST_NAME" },
// ------------------------------------------
new string[] { "1", "Gary", "Texmo" },
new string[] { "2", "Joe", "Smith" },
new string[] { "3", "Jane", "Doe" }
};
请注意,我已经在第一行中定义了列。
这些方法将默认为零行作为列行,但是您可以在任意行中定义列,只要您指定即可。
这些是将用于数据集中的列的名称。
现在,DataFactory.BuildDataSet方法的tableDefinitions参数的类型为List <KeyValuePair <string,object [] [] >>。 这使我们能够建立一个表列表,为该表提供一个字符串名称,以及该表的2D数据数组。 要创建此表定义参数,我们需要...
List<KeyValuePair<string, object[][]>> tableDefinitions = new List<KeyValuePair<string, object[][]>>()
{
new KeyValuePair<string, object[][]>("EMPLOYEES", RAW_EMPLOYEES)
};
...根据需要添加更多表的行。
然后,我们将通过调用BuildDataSet方法加载数据集,如下所示:
DataSet data = DataFactory.BuildDataSet(tableDefinitions);
因为此数据类型可能会使某些人感到困惑,所以我创建了另一个实用程序类来存储这些KeyValuePair <string,object [] []>对象的列表以及用于接收该对象的重载方法。
因此,作为上述替代方案,我们可以执行以下操作...
TableDefinitions tableDefinitions = new TableDefinitions();
tableDefinitions.Add("EMPLOYEES", RAW_EMPLOYEES);
DataSet data = DataFactory.BuildDataSet(tableDefinitions);
使用任何您发现更容易的方法。
无论如何,现在我们有了一个可加载示例数据并返回DataSet对象的类。
public class SampleData
{
private static string[][] RAW_EMPLOYEES = new string[][]
{
new string[] { "ID", "FIRST_NAME", "LAST_NAME" },
// ------------------------------------------
new string[] { "1", "Gary", "Texmo" },
new string[] { "2", "Joe", "Smith" },
new string[] { "3", "Jane", "Doe" }
};
public static DataSet GetSampleData()
{
TableDefinitions tableDefinitions = new TableDefinitions();
tableDefinitions.Add("EMPLOYEES", RAW_EMPLOYEES);
return DataFactory.BuildDataSet(tableDefinitions);
}
}
现在,我们可以在程序中的任何位置调用它。
最好将它放在try / catch块中,因为遇到错误时会抛出多个异常。
try
{
DataSet data = SampleData.GetSampleData();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
如果在其中放置一个断点并使用VisualStudio DataSet Visualizer(将鼠标悬停在DataSet对象上,然后单击放大镜),我们将看到以下信息:
现在,正如我提到的,我还支持加载类型化的数据,因此可以说我们希望数据中的ID列为整数数据类型。 我们需要将数组从string [] []更改为object [] [],定义一行类型,然后定义数据。 DataFactory类上的方法采用一个参数,该参数定义类型所在的行。 默认值为-1,表示未定义类型,并且列数据为字符串。
因此,带有类型的修改后的对象数组看起来像...
private static object[][] RAW_EMPLOYEES = new object[][]
{
new object[] { "ID", "FIRST_NAME", "LAST_NAME" },
new object[] { typeof(int), typeof(string), typeof(string) },
// ------------------------------------------
new object[] { 1, "Gary", "Texmo" },
new object[] { 2, "Joe", "Smith" },
new object[] { 3, "Jane", "Doe" }
};
注意第二行,定义每一列的类型。
现在,我们可以将对BuildDataSet的调用更改为指定列行为0,类型行为1。
return DataFactory.BuildDataSet(tableDefinitions, 0, 1);
如果我们要检查数据集并查看列的数据类型,我们将能够看到它现在是整数(相对于以前的字符串类型)。
当您需要更多表时,只需创建更多2D数组并将它们添加到tableDefinitions对象中即可。 它们都将被加载到Tables属性的DataSet中,您可以按名称(或索引)访问它们。
因此,我们可以轻松地定义和加载数据集,以便为程序使用提供示例数据。 如果以后再建立数据库连接,则可以简单地将示例数据集替换为数据库查询的结果,并且程序应该可以继续进行。
因为我坚信源代码共享,所以我不会为此提供dll,而是为其附加源代码。 请参阅附件列表
DataFactory.cs.txt 。 将其重命名为DataFactory.cs并将其放置在您的项目或解决方案中的新项目中。如果您有任何问题/意见,请随时发布。
谢谢!
From: https://bytes.com/topic/c-sharp/insights/914243-load-dataset-datatable-2d-array-objects