Академический Документы
Профессиональный Документы
Культура Документы
Data-Path
Helper Program
Data-Path
Therefore, it is possible to transverse through the ERD as it is a directed graph. However, the directed graph can only be traversed in one direction. For example, A cannot directly go to G. Fortunately, we can easily transform the ERD into general graph, in which edges have no direction. We just need to modify the query as follow:
SELECT object_name(parent_object_id) FROM Sys.Foreign_Keys WHERE object_name(referenced_object_id) = '<TableName>' UNION SELECT object_name(referenced_object_id) FROM Sys.Foreign_Keys WHERE object_name(parent_object_id) = '<TableName>'
With this query, the ERD will be the general graph, in which we can traverse more easily as long as there is a connection between each table. It does not matter whether the connection is primaryto-foreign or foreign-to-primary. By using the query, we can write a recursive graph traversal function, which will find a path from one table to another. One biggest advantage is that it will save time. Suppose we have a hundreds of tables linked to each other. In this case, looking for manually a data-path among the tables will be really tedious. Just by giving source and destination tables and letting the program figure out the path between them will be really helpful.
2
Helper Program
Data-Path
The ERD traversal program has 2 main parts: the first function is to look for all the links to others for a given table, and the second function is, by using the first function, to actually look for the path between two tables. Code: GetDestinations Function
private List<string> GetDestinations(string Source) { List<string> destinations = new List<string>(); SqlConnection cnx = new SqlConnection(ConnectionString); cnx.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = cnx; cmd.CommandText = "SELECT object_name(parent_object_id) FROM Sys.Foreign_Keys WHERE object_name(referenced_object_id) = '" + Source + "' UNION SELECT object_name(referenced_object_id) FROM Sys.Foreign_Keys WHERE object_name(parent_object_id) = '" + Source + "'"; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); foreach (DataRow dr in ds.Tables[0].Rows) { destinations.Add(dr.ItemArray[0].ToString()); } cnx.Close(); return destinations; }
Helper Program
Data-Path