Hi,
I have two unrelated tables in SQL Server. I want to form a relationship with them via C# so the database diagram in SQL Server has the relationship line etc (the code may have flaws apart from the lack of using statements etc, other than that let me know).
I have this code so far:
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=Test;Integrated Security=SSPI");
SqlDataAdapter ad1;
SqlDataAdapter ad2;
DataSet ds = new DataSet();
DataRelation dr;
ad1 = new SqlDataAdapter("Select * from dept", con);
ad2 = new SqlDataAdapter("select * from emp", con);
ad1.Fill(ds, "dept");
ad2.Fill(ds, "emp");
DataColumn pk = ds.Tables["dept"].Columns["deptno"];
DataColumn fk = ds.Tables["emp"].Columns["deptno"];
dr = new DataRelation("rel", pk, fk, false)
ds.Relations.Add(dr);
ds.AcceptChanges();
ad1.Update(ds, "dept");
ad2.Update(ds, "emp");
When I get to this line:
dr = new DataRelation("rel", pk, fk, false)
I get this exception:
'column' argument cannot be null. Parameter name: column
Is it possible to actually form relationships in SQL Server this way?
What gives?