简体   繁体   English

如何在运行时使用C#创建强类型数据集?

[英]How do I create strongly typed dataset during runtime using C#?

I need to create a strongly typed dataset during run-time for the user preferred target database. 我需要在运行时为用户首选的目标数据库创建一个强类型的数据集。 Visual studio has massive design time support for creating typed datasets. Visual Studio具有大量的设计时支持,可用于创建类型化的数据集。 I need to automate the process of generating typed datasets for the target database at runtime. 我需要在运行时自动为目标数据库生成类型化数据集的过程。

It should create... 它应该创建...

1.) XSD file. 1.)XSD文件。

2.) Typed dataset represnting the Database 2.)表示数据库的类型化数据集

3.) Typed wrappers for all database tables and columns within the tables. 3.)为所有数据库表和表中的列键入包装器。

4.) TableAdapters for each table. 4.)每个表的TableAdapter。

So I need to generate the same typed dataset at runtime which is generally created while design time using Typed dataset designer of the Visual Studio. 因此,我需要在运行时生成相同的类型化数据集,该类型化数据集通常是在设计时使用Visual Studio的类型化数据集设计器创建的。

You could probably use XSD.EXE . 您可能使用XSD.EXE Fire it up from your program... 从您的程序启动它...

I tend to agree with jcollum on this one, using a Typed Dataset at runtime is probably the wrong way to go. 我倾向于在这一方面同意jcollum,在运行时使用类型化数据集可能是错误的方法。 If on the other hand, you just want to be able to get structured data ( aka, a DataTable ) from a database at runtime, you could use reflection to create a TableAdapter from an arbitrary data result. 另一方面,如果您只希望能够在运行时从数据库中获取结构化数据(也称为DataTable),则可以使用反射从任意数据结果中创建TableAdapter。

var results = (from data in db.SomeTable select data).ToArray();
DataTable dt = ObjectArrayToDataTable(results);
// At this point you have a properly structure DataTable.

// Here is your XSD, if absolutely needed.
dt.WriteXMLSchema("c:\somepath\somefilename.xsd");

private static System.Data.DataTable ObjectArrayToDataTable(object[] data)
{
    System.Data.DataTable dt = new System.Data.DataTable();
    // if data is empty, return an empty table
    if (data.Length == 0) return dt;

    Type t = data[0].GetType();
    System.Reflection.PropertyInfo[] piList = t.GetProperties();

    foreach (System.Reflection.PropertyInfo p in piList)
    {
        dt.Columns.Add(new System.Data.DataColumn(p.Name, p.PropertyType));
    }

    object[] row = new object[piList.Length];

    foreach (object obj in data)
    {
        int i = 0;
        foreach (System.Reflection.PropertyInfo pi in piList)
        {
            row[i++] = pi.GetValue(obj, null);
        }
        dt.Rows.Add(row);
    }

    return dt;
}

You could apply the same principal to create a structured DataSet and easily create a DataAdapter to it. 您可以应用相同的主体来创建结构化的DataSet并轻松地为其创建DataAdapter。

Or perhaps I am mis-reading your requirements. 也许我误读了您的要求。

Given my experience with Typed Datasets in the past -- and all their accompanying failures and issues -- I'd strongly encourage you to investigate doing this with an ORM mapper. 鉴于我过去使用类型数据集的经验以及所有伴随的失败和问题,我强烈建议您研究使用ORM映射器进行此操作。 In other words, run away from Typed Datasets. 换句话说,请远离类型化数据集。

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

相关问题 C#在数据输入表单中使用强类型数据集tableadapter - C# Using strongly typed dataset tableadapter in data entry forms C#强类型数据集验证方法 - C# Strongly typed dataset validation approach 如何使用 Linq 在 C# 中找出强类型表是否包含来自另一个强类型表的值 - How can I find out if a strongly typed table contains a value from another strongly typed table in C# using Linq 如何以编程方式创建强类型数据集? - How to create strongly typed dataset programmatically? 如何为 void 方法编写单元测试并创建模拟数据集和数据行(数据集和数据行都是强类型)? - How do I write a unit test for a void method and create a mock dataset and a datarow(Both dataset and datarow are strongly typed)? 如何使用 C# MongoDB 驱动程序编写强类型过滤器,其中嵌套的 object 属性不能相等? - How to do I write a strongly typed filter where nested object properties cannot be equal using C# MongoDB driver? 如何在ASP.NET MVC运行时创建强类型视图? - How to create a strongly typed view during runtime in ASP.NET MVC? 如何使用C#从xml获取强类型对象? - How to get a strongly typed object from xml using c#? 在C#中使用类型化数据集时如何更改SQLite文件位置 - How do I change SQLite file location when using typed dataset in C# 在C#中的强类型DataSet中返回单行 - Returning a single row in a strongly typed DataSet in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM