我正在尝试返回与其相关产品或相关付款类型的客户列表。我可以自己返回客户,但不能提供嵌入式/扩展信息请求。我是编码的新手,所以大量的错误消息对我来说意义不大,因为我不知道如何解释它们。尝试退还客户的产品时,我收到了IndexOutOfRangeException:产品ID和一个SQLException:''附近的语法不正确。付款方式。我想我已经正确命名和引用了所有数据字段和别名,但我只是找不到我要忽略的内容。
针对具有产品的客户的代码:
string command = "";
string customerColumns = @"
SELECT c.Id AS 'CustomerId',
c.FirstName AS 'CustomerFirstName',
c.LastName AS 'CustomerLastName',
c.AccountCreated AS 'DateJoined',
c.LastActive AS 'LastActive',
c.Archived";
string customerTable = "FROM Customer c";
if (include == "products")
{
string productColumns = @"
pt.Name AS 'ProductType',
p.Id,
p.Title AS 'ProductName',
p.Description AS 'ProductDescription',
p.Price AS 'ProductPrice',
p.Quantity AS 'QuantityAvailable',
p.Archived";
string productTables = @"
JOIN Product p ON p.CustomerId = c.Id
JOIN ProductType pt ON pt.Id = p.ProductTypeId";
command = $@"{customerColumns},
{productColumns}
{customerTable}
{productTables}";
}
...
cmd.CommandText = command;
SqlDataReader reader = cmd.ExecuteReader();
List<Customer> Customers = new List<Customer>();
while (reader.Read())
{
Customer currentCustomer = new Customer
{
Id = reader.GetInt32(reader.GetOrdinal("CustomerId")),
FirstName = reader.GetString(reader.GetOrdinal("CustomerFirstName")),
LastName = reader.GetString(reader.GetOrdinal("CustomerLastName")),
AccountCreated = reader.GetDateTime(reader.GetOrdinal("DateJoined")),
LastActive = reader.GetDateTime(reader.GetOrdinal("LastActive")),
Archived = reader.GetBoolean(reader.GetOrdinal("Archived"))
};
if (include == "products")
{
Product currentProduct = new Product
{
Id = reader.GetInt32(reader.GetOrdinal("Id")),
Title = reader.GetString(reader.GetOrdinal("ProductName")),
Description = reader.GetString(reader.GetOrdinal("ProductDescription")),
Quantity = reader.GetInt32(reader.GetOrdinal("QuantityAvailable")),
Price = reader.GetDouble(reader.GetOrdinal("ProductPrice"))
};
上面显示了当前代码。
任何能为我提供指导或提供有关如何理解或找出错误消息的见解的人,深表感谢!谢谢!