1

我在 C# 中使用以下查询,它不返回任何数据(意味着返回 0 行或第一个 reader.Read() 返回 false

var commandText = 
            "SELECT m.Id, m.LastName, m.FirstName, m.MembershipStartDate as     StartDate, m.MembershipEndDate as EndDate, t.Id as TierId, t.Name, t.Value as Dues " +
            "FROM Member m " +
            "INNER JOIN MembershipTiers t ON m.MemberShipTierId = t.Id";

var command = _connection.CreateCommand();
command.CommandText = commandText;
command.CommandType = CommandType.Text;

SQLiteDataReader reader = command.ExecuteReader();
while (reader.Read())
{
    var member = new Member();

    member.Id = Convert.ToInt32(reader["Id"]);
    member.LastName = Convert.ToString(reader["LastName"]);
    member.FirstName = Convert.ToString(reader["FirstName"]);
    member.StartDate = DateTime.Parse(Convert.ToString(reader["StartDate"]));
            member.EndDate = reader["EndDate"] == null ? DateTime.MaxValue : DateTime.Parse(Convert.ToString(reader["StartDate"]));
    member.IsVested = (Convert.ToInt32(reader["IsVested"]) != 0);

    member.Tier = new MemberShipTier();
    member.Tier.Id = Convert.ToInt32(reader["TeirId"]);
    member.Tier.Name = Convert.ToString(reader["Name"]);
    member.Tier.Value = Convert.ToInt32(reader["Dues"]);

    members.Add(member);
}

当我在 SQLite 的数据库浏览器中运行它时,它工作正常。

查询成功

当我摆脱 INNER JOIN 时,它工作正常。

这是从数据库导出生成的 sql(只是一个测试数据库)

BEGIN TRANSACTION;
CREATE TABLE `Sponsorship` (
`SponsorMemberId`   INTEGER,
`NewMemberId`   INTEGER,
`DateSponsored` TEXT
);
CREATE TABLE "MembershipTiers" (
`Id`    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`Name`  TEXT NOT NULL UNIQUE,
`Value` NUMERIC NOT NULL
);
INSERT INTO `MembershipTiers` VALUES (1,'Senior Family',3310);

CREATE TABLE `Member` (
`Id`    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`LastName`  TEXT NOT NULL,
`FirstName` TEXT NOT NULL,
`MemberShipTierId`  INTEGER NOT NULL,
`MembershipStartDate`   TEXT,
`MembershipEndDate` TEXT,
`IsVested`  INTEGER NOT NULL DEFAULT 0
);
INSERT INTO `Member` VALUES (1,'Dodge','Branden',1,'2015-10-20','',1);
COMMIT;
4

0 回答 0