使用数据适配器的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/

07-28 02:55
查看更多