我通过这种方式使用关系填充来自多个表的数据集:
private void UcitajZahtjeve()
{
string query1 = "SELECT tblrezgo.IDRadnik, CONCAT(tblradnik.Ime, ' ', tblradnik.Prezime) AS ImePrezime, tblrezgo.EmailRadnik, tblrezgo.creationTime, tblrezgo.IDUnos ";
query1 += "FROM tblrezgo ";
query1 += "LEFT JOIN tblradnik ON tblradnik.IDRadnik = tblrezgo.IDRadnik ";
query1 += "LEFT JOIN tblrezgolog ON tblrezgolog.IDUnos = tblrezgo.IDUnos ";
query1 += "WHERE (tblrezgolog.OdobrenoN=1 AND tblrezgolog.OdobrenoHR=1) ";
query1 += "ORDER BY tblrezgo.creationTime ASC";
string query2 = "SELECT IDUnos, DatumGO, OdobrenoN, OdobrenoHR FROM tblrezgolog";
gridZahtjevi.DataSource = dbTwoTableRelation(query1, query2, "Detalji", "IDUnos", "IDUnos").Tables[0];
gridZahtjevi.LevelTree.Nodes.Add("Detalji", gridDetails);
}
private DataSet dbTwoTableRelation(string query1, string query2, string relationName, string fieldName1, string fieldName2)
{
MySqlDataAdapter table1;
MySqlDataAdapter table2;
DataSet myDataSet = new DataSet();
table1 = new MySqlDataAdapter(query1, this.connection);
table2 = new MySqlDataAdapter(query2, this.connection);
table1.Fill(myDataSet, "mainList");
table2.Fill(myDataSet, "subList");
myDataSet.Relations.Add(relationName, myDataSet.Tables["mainList"].Columns[fieldName1], myDataSet.Tables["subList"].Columns[fieldName2]).Nested = true;
return myDataSet;
}
查询工作正常,但如果我运行此代码,即使应用程序加载网格为空,我也会收到错误。
类型' System.ArgumentException'的第一次机会异常。发生了 在System.Data.dll
中
如果我从query1中删除一个join子句,那么everthing就可以了。 我不明白是什么原因导致问题,两个连接是否有问题?!
答案 0 :(得分:1)
只需在运行时获取构造的query1并尝试在数据库中执行, 然后很容易找到错误
答案 1 :(得分:0)
使用查询像这样。它应该工作。
string query1 = "SELECT tblrezgo.IDRadnik, CONCAT(tblradnik.Ime, ' ',
tblradnik.Prezime) AS ImePrezime, tblrezgo.EmailRadnik,
tblrezgo.creationTime, tblrezgo.IDUnos ";
query1 += "FROM tblrezgo ";
query1 += "LEFT JOIN tblradnik ON tblradnik.IDRadnik =
tblrezgo.IDRadnik , tblrezgo ";
query1 += "LEFT JOIN tblrezgolog ON tblrezgolog.IDUnos =
tblrezgo.IDUnos ";
query1 += "WHERE (tblrezgolog.OdobrenoN=1 AND
tblrezgolog.OdobrenoHR=1) ";
query1 += "ORDER BY tblrezgo.creationTime ASC";