处理我的程序我遇到了一个我无法弄清楚的错误。当我尝试将数据从数据库提取到表中时,就会发生这种情况。我可以从单个表中正确提取它,问题是我必须使用两个表。我很确定这意味着我需要加入。但是,当我尝试加载表格所在的页面时,我在标题中收到错误。
代码:
AccessDataSource theSource = PData;
theSource.SelectCommand = "SELECT Membership.idMembership, Membership.role, Users.lastname"
+"FROM Membership "
+"INNER JOIN Users"
+"ON Membership.idUser = Users.idUsers;";
try
{
DataView dv = (DataView)theSource.Select(DataSourceSelectArguments.Empty);
TableHeaderRow thr = new TableHeaderRow();
TableHeaderCell ID = new TableHeaderCell();
TableHeaderCell Role = new TableHeaderCell();
TableHeaderCell User = new TableHeaderCell();
ID.Text = "MemberID";
Role.Text = "Role";
User.Text = "User";
thr.Cells.Add(ID);
thr.Cells.Add(Role);
thr.Cells.Add(User);
tblNames.Rows.Add(thr);
foreach (DataRowView aRow in dv)
{
String id = (String)aRow["Membership.idMembership"].ToString();
String role = (String)aRow["Membership.role"];
String user = (String)aRow["Users.lastname"].ToString();
TableRow tr = new TableRow();
TableCell one = new TableCell();
TableCell two = new TableCell();
TableCell three = new TableCell();
one.Text = id;
one.HorizontalAlign = HorizontalAlign.Center;
two.Text = role;
two.HorizontalAlign = HorizontalAlign.Center;
three.Text = user;
three.HorizontalAlign = HorizontalAlign.Center;
tr.Cells.Add(one);
tr.Cells.Add(two);
tr.Cells.Add(three);
tblNames.Rows.Add(tr);
}
}
catch (Exception ex)
{
lblError.Text = ex.ToString();
}
}
请告知
答案 0 :(得分:3)
本声明:
"SELECT Membership.idMembership, Membership.role, Users.lastname"
+"FROM Membership "
+"INNER JOIN Users"
+"ON Membership.idUser = Users.idUsers;";
将创建此字符串:
SELECT Membership.idMembership, Membership.role, Users.lastnameFROM Membership INNER JOIN UsersON Membership.idUser = Users.idUsers;
您需要在Users
和Users.lastname
之后添加空格:
"SELECT Membership.idMembership, Membership.role, Users.lastname "
+"FROM Membership "
+"INNER JOIN Users "
+"ON Membership.idUser = Users.idUsers;";
答案 1 :(得分:0)
您的查询中缺少两个空格:
theSource.SelectCommand = "SELECT Membership.idMembership, Membership.role, Users.lastname " // added extra space here
+"FROM Membership "
+"INNER JOIN Users " // added extra space here
+"ON Membership.idUser = Users.idUsers;";
答案 2 :(得分:0)
在SQL命令中缺少两个空格,一个在SELECT语句的末尾,一个在JOIN语句的末尾,所以请尝试:
theSource.SelectCommand = "SELECT Membership.idMembership, Membership.role, Users.lastname "
+"FROM Membership "
+"INNER JOIN Users "
+"ON Membership.idUser = Users.idUsers";
您遇到错误,因为没有空格是无效的SQL查询;你可以在SSMS中查看它。
答案 3 :(得分:0)
有些答案缺少空格;在" FROM"。
之前也需要它们正确答案是这样的(*表示应该放置额外空格的地方):
theSource.SelectCommand = "SELECT Membership.idMembership, Membership.role, Users.lastname"
+ "*FROM Membership "
+ "INNER JOIN Users"
+ "*ON Membership.idUser = Users.idUsers;"