private static string CheckManyToOne(string receivedTable, string senderTable, string connectionString, out ForeignKeyInfo foreignKey) { using (var con = new SqlConnection(connectionString)) { con.Open(); var cmdForeignKeyReferences = new SqlCommand { CommandText = MyQueries.GetForeignKeyQueryBetweenTablesQuery(senderTable, receivedTable), CommandType = CommandType.Text, Connection = con }; var drForeignKeyReferences = cmdForeignKeyReferences.ExecuteReader(); ForeignKeyInfo foreignkey = null; if (drForeignKeyReferences.Read()) { foreignkey = new ForeignKeyInfo { FkName = drForeignKeyReferences[0].ToString(), TableName = drForeignKeyReferences[1].ToString(), ColumnName = drForeignKeyReferences[2].ToString(), ReferencedTable = drForeignKeyReferences[3].ToString(), ReferencedColumn = drForeignKeyReferences[4].ToString() }; } drForeignKeyReferences.Close(); var primaryKey = GetPrimaryKey(con, senderTable); string result; if (foreignkey != null) { result = "manytoone"; if (foreignkey.ColumnName == primaryKey.ColumnName) { result = (foreignkey.ColumnName == primaryKey.ColumnName) ? "onetoone" : "manytoone"; } } else { result = null; } foreignKey = foreignkey; return(result); } }
private static string CheckOneToOne(string receivedTable, string senderTable, string connectionString, out ForeignKeyInfo oneToOneforeignkey) { using (var con = new SqlConnection(connectionString)) { con.Open(); var cmdOneToOneRelationship = new SqlCommand { CommandText = MyQueries.GetForeignKeyQueryBetweenTablesQuery(receivedTable, senderTable), CommandType = CommandType.Text, Connection = con }; var primaryKey = GetPrimaryKey(con, receivedTable); var drOneToOneRelationship = cmdOneToOneRelationship.ExecuteReader(); var oneToOneForeignKey = new ForeignKeyInfo(); if (drOneToOneRelationship.Read()) { oneToOneForeignKey.FkName = drOneToOneRelationship[0].ToString(); oneToOneForeignKey.TableName = drOneToOneRelationship[1].ToString(); oneToOneForeignKey.ColumnName = drOneToOneRelationship[2].ToString(); oneToOneForeignKey.ReferencedTable = drOneToOneRelationship[3].ToString(); oneToOneForeignKey.ReferencedColumn = drOneToOneRelationship[4].ToString(); } drOneToOneRelationship.Close(); string result; if (oneToOneForeignKey.ColumnName != null) { result = (primaryKey.ColumnName == oneToOneForeignKey.ColumnName) ? "onetoone" : "onetomany"; } else { result = ""; } oneToOneforeignkey = oneToOneForeignKey; return(result); } }