正在获取有关数据集中数据关系的信息



使用数据适配器的FillSchema方法,我获得了表结构

    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
        // ...
    }

是否有类似的方法来填充所有相关表格的关系?

请查看下面的链接是否可以帮助您。

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

最新更新