Question

I've been using ORM's for so long, I appear to have forgotten most of my basic data handling skills in dotnet :(

Is it possibly to do something like this?

        DataSet ds = new DataSet();
        var compiledConnection = new SqlConnection(cDbConnectionString);
        SqlDataAdapter daChart = new SqlDataAdapter("select * from Chart", compiledConnection);
        daChart.Fill(ds, "chart");

        if (ds.Tables["chart"].Rows.Count > 0)
        {
            var sourceConnection = new SqlConnection(sourceDbConnectionString);
            SqlDataAdapter daSource = new SqlDataAdapter("select * from source", sourceConnection);
            daSource.Fill(ds, "source");

            DataRelation chart_source = new DataRelation("dr", ds.Tables["chart"].Columns["intItemId"],
                   ds.Tables["source"].Columns["intRowId"], false);
            ds.Relations.Add(chart_source);
        }

And then use one of the columns in the table "chart" to order the data in the table "source" across the datarelation?

(Before anyone asks, these two tables are in separare instances of SqlServer on separate sites, so just pulling the data as one table is not a straightforward task. Hence this approach)

Cheers, Matt

Was it helpful?

Solution 2

Thanks for the suggestion, but I discovered you can do it with LINQ rather more easily:

            DataTable source = ds.Tables["source"];
            DataTable chart = ds.Tables["chart"];

            var joinedTable =
                from s in source.AsEnumerable()
                join c in chart.AsEnumerable()
                on s.Field<Int64>("intRowId") equals
                    c.Field<Int64>("intItemId")
                select new
                {
                    intRowId = s.Field<Int64>("intRowID"),
                    strTitle = s.Field<string>("strTitle"),
                    intWeight = c.Field<Int64>("intWeight")
                };

            var sortedTable = from j in joinedTable
                              orderby j.intWeight descending
                              select j;

OTHER TIPS

That just creates the equivalent of a foreign key. You seem to want the equivalent of an INNER JOIN.

In addition to creating the relationship, it requires adding all the columns of one to the other, loops to fill in the rows and GetParentRows. MS has some good starting point code:

http://support.microsoft.com/kb/326080

EDIT. You could also do a SQL version, by creating a linked server and using 4 part names [server].[database].[owner].[table]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top