2
约DataRelations信息使用数据适配器我获得表结构获取DataSet中的
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
// ...
}
有没有类似的方式来填补所有相关表格的关系的FillSchema的法?
他们还手动创建DataRelation对象,就像我的示例中一样。但我喜欢它自动... – alex555 2012-03-08 13:36:50
你不能自动做到这一点,但可以在代码中使用循环,并手动添加 – PraveenVenu 2012-03-08 13:55:49
看起来这样。有了这个[查询](http://sqlserver2000.databases.aspfaq.com/schema-how-do-i-find-all-the-foreign-keys-in-a-database.html),我收到了我的所有关系数据集 – alex555 2012-03-08 14:18:57