gpt4 book ai didi

c# - 获取有关 DataSet 中 DataRelations 的信息

转载 作者:太空宇宙 更新时间:2023-11-03 14:04:54 25 4
gpt4 key购买 nike

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

关于c# - 获取有关 DataSet 中 DataRelations 的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9617704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com