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

时间:2022-02-27 19:05:58

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. I need to automate the process of generating typed datasets for the target database at runtime.

我需要在运行时为用户首选目标数据库创建一个强类型数据集。 Visual Studio为创建类型化数据集提供了大量的设计时间支持。我需要在运行时自动化为目标数据库生成类型化数据集的过程。

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.)每个表的TableAdapters。

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的Typed数据集设计器创建的。

3 个解决方案

#1


I tend to agree with jcollum on this one, using a Typed Dataset at runtime is probably the wrong way to go. 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.

我倾向于同意jcollum,在运行时使用Typed Dataset可能是错误的方法。另一方面,如果您希望能够在运行时从数据库获取结构化数据(也称为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.

或许我错误地阅读了您的要求。

#2


You could probably use XSD.EXE. Fire it up from your program...

您可以使用XSD.EXE。从你的程序中解雇它......

#3


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. In other words, run away from Typed Datasets.

鉴于我过去使用类型化数据集的经验 - 以及所有相关的失败和问题 - 我强烈建议您使用ORM映射器进行调查。换句话说,远离Typed Datasets。

#1


I tend to agree with jcollum on this one, using a Typed Dataset at runtime is probably the wrong way to go. 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.

我倾向于同意jcollum,在运行时使用Typed Dataset可能是错误的方法。另一方面,如果您希望能够在运行时从数据库获取结构化数据(也称为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.

或许我错误地阅读了您的要求。

#2


You could probably use XSD.EXE. Fire it up from your program...

您可以使用XSD.EXE。从你的程序中解雇它......

#3


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. In other words, run away from Typed Datasets.

鉴于我过去使用类型化数据集的经验 - 以及所有相关的失败和问题 - 我强烈建议您使用ORM映射器进行调查。换句话说,远离Typed Datasets。