2

Using FillSchema-method of the data adapter I obtain the table structure

    using (SqlConnection sqlConn = new SqlConnection(connectionString))
    {
        var dataAdapter = new SqlDataAdapter();
        var dataSet = new DataSet();

        sqlConn.Open();

        string sqlSelectCommand = "select * from Projects;\nselect * from Staff"

        dataAdapter.SelectCommand = new SqlCommand(sqlSelectCommand, sqlConn);

        dataAdapter.FillSchema(dataSet, SchemaType.Source); 
        dataAdapter.Fill(dataSet);

        dataSet.Tables[0].TableName = "Projects";
        dataSet.Tables[1].TableName = "Staff";

        // create relations between the tables
        // is there an alternative way?
        var relation = new DataRelation("FK_Projects_Staff", dataSet.Tables["Staff"].Columns["ID"], dataSet.Tables["Projects"].Columns["Responsible_ID"], true);
        dataSet.Relations.Add(relation);

        // do some manipulations on data using projectsDataAdapter and staffDataAdapter
        // ...
    }

Is there a similar way to fill the relations of all relevant tables?

alex555
  • 1,676
  • 4
  • 27
  • 45

1 Answers1

1

Please see if the below link can help you.

http://csharptutorial.com/how-to-create-a-dataset-programmatically/

PraveenVenu
  • 8,217
  • 4
  • 30
  • 39
  • They also create the DataRelation object manually, like in my example. But I'd like it automatically... – alex555 Mar 08 '12 at 13:36
  • You can not do it automatically, but can use a loop in the code and manually add – PraveenVenu Mar 08 '12 at 13:55
  • It looks that way. With this [query](http://sqlserver2000.databases.aspfaq.com/schema-how-do-i-find-all-the-foreign-keys-in-a-database.html) I receive all relations for my dataset – alex555 Mar 08 '12 at 14:18