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