帮忙呀!
下面这段代码,编译可以通过,但是运行的时候出现错误
错误如下:
Customer ID: ALFKI Name:
xiaowei.li
该行不属于与此关系相同的数据集。
请问是什么原因!
try
{
SqlConnection sqlcnt=new SqlConnection (@"Data Source=(local);Integrated Security=SSPI;"+"Initial Catalog=northwind");
sqlcnt.Open();
SqlDataAdapter sqlad=new SqlDataAdapter("Select CustomerID,CompanyName from Customers",sqlcnt);
SqlCommandBuilder sqlcmdad=new SqlCommandBuilder (sqlad);
SqlDataAdapter sqlcust=new SqlDataAdapter("Select * from Customers",sqlcnt);
SqlDataAdapter sqlorder=new SqlDataAdapter("Select * from Orders",sqlcnt);
SqlCommandBuilder sqlcmdcust=new SqlCommandBuilder (sqlcust);
SqlCommandBuilder sqlcmdorder=new SqlCommandBuilder (sqlorder);
DataSet thisset=new DataSet ();
sqlcust.Fill(thisset,"Customers");
sqlorder.Fill(thisset,"Orders");
DataRelation custordrel=new DataRelation("custord",
thisset.Tables["Customers"].Columns["CustomerID"],thisset.Tables["Orders"].Columns["CustomerID"]);
foreach (DataRow custrow in thisset.Tables["Customers"].Rows)
{
Console.WriteLine("Customer ID: " + custrow["CustomerID"] +
" Name: " + custrow["CompanyName"]);
foreach (DataRow orderrow in custrow.GetChildRows(custordrel))
{
Console.WriteLine(" Order ID: " + orderrow["OrderID"]);
}
}
custordrel.Nested =true;
}
catch(Exception err)
{
Console.Write(err.Message );
}