views:

2786

answers:

3

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.

It should create...

1.) XSD file.

2.) Typed dataset represnting the Database

3.) Typed wrappers for all database tables and columns within the tables.

4.) TableAdapters for each table.

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.

+2  A: 

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

Arjan Einbu
xsd.exe will generate the class files from an existing XSD defi. file. I need to generate XSD as well. And I need to create table adapters as well.
this. __curious_geek
Instead of firing up XSD.EXE you could also talk directly to XsdTool.Xsd class or even the System.Data.Design.TypedDataSetGenerator and System.Xml.Serialization.XmlReflectionImporter/XmlSchemaExporter.
Peter Lillevold
+1  A: 

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.

jcollum
+1 they're a pain to do yourself, and often fail, other people have done the work to solve those problems, those people are the ones who've made ORMs.
SnOrfus
yes you're right. we're having our own ORM on top of typed datasets. we're using typed datasets just as a data model and we do not heavily depend on them. But still we need to create that at runtime somehow! any idea.
this. __curious_geek
I'm not understanding why you have an ORM and Typed datasets that you'd generate at runtime. Please update the question to elaborate. Also, it seems like LINQ would be able to tackle this one. Just a hunch tho.
jcollum
It seems like LINQ or your ORM solution should have an API that would generate classes for an unknown-at-design-time database on the fly.
jcollum
+1  A: 

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.

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.

Or perhaps I am mis-reading your requirements.

Serapth