我有这个问题:
SELECT CableNumber, CblType, ApprxmtLngth, ME.EquipmentNumber, ME2.EquipmentNumber, CblStatus, InstallMthd, S.PrdtCd FROM CableId C
INNER JOIN MajorEquipment ME ON C.FromLoc = ME.MEId
INNER JOIN MajorEquipment ME2 ON C.ToLoc = ME2.MEId
INNER JOIN SupplierInfo S ON C.SupplierId = S.SupplierId WHERE
(@CblType IS NULL OR CblType LIKE @CblType)
AND (@CblStatus IS NULL OR CblStatus LIKE @CblStatus)
AND (@FromLoc IS NULL OR ME.EquipmentNumber LIKE @FromLoc)
AND (@ToLoc IS NULL OR ME2.EquipmentNumber LIKE @ToLoc);
当我将结果传递给模型时,如何定义哪个EquipmentNumber
进入哪个变量?
while (mySqlReader.Read())
{
CableID_Controller.CList.Add(new CableID_Model
{
CableNumber = Convert.ToString(mySqlReader["CableNumber"]),
CableType = Convert.ToString(mySqlReader["CblType"]),
SupplierPart = Convert.ToString(mySqlReader["PrdtCd"]),
CableLength = Convert.ToInt32(mySqlReader["ApprxmtLngth"]),
InstallMethod = Convert.ToString(mySqlReader["InstallMthd"]),
Origin = Convert.ToString(mySqlReader["ME.EquipmentNumber"]),
Destination = Convert.ToString(mySqlReader["ME2.EquipmentNumber"]),
Status = Convert.ToString(mySqlReader["CblStatus"])
});
}
因为ME.EquipmentNumber
和ME2.EquipmentNumber
不是标题的实际名称。
答案 0 :(得分:2)
只需在您的查询和代码中使用EquipmentNumber1
运算符为列添加不同的别名(EquipmentNumber2
和as
)。您还必须删除代码中的ME1
和ME2
。
SELECT CableNumber, CblType, ApprxmtLngth, ME.EquipmentNumber as EquipmentNumber1, ME2.EquipmentNumber as EquipmentNumber2, CblStatus, InstallMthd, S.PrdtCd FROM CableId C
INNER JOIN MajorEquipment ME ON C.FromLoc = ME.MEId
INNER JOIN MajorEquipment ME2 ON C.ToLoc = ME2.MEId
INNER JOIN SupplierInfo S ON C.SupplierId = S.SupplierId WHERE
(@CblType IS NULL OR CblType LIKE @CblType)
AND (@CblStatus IS NULL OR CblStatus LIKE @CblStatus)
AND (@FromLoc IS NULL OR ME.EquipmentNumber LIKE @FromLoc)
AND (@ToLoc IS NULL OR ME2.EquipmentNumber LIKE @ToLoc);
while (mySqlReader.Read())
{
CableID_Controller.CList.Add(new CableID_Model
{
CableNumber = Convert.ToString(mySqlReader["CableNumber"]),
CableType = Convert.ToString(mySqlReader["CblType"]),
SupplierPart = Convert.ToString(mySqlReader["PrdtCd"]),
CableLength = Convert.ToInt32(mySqlReader["ApprxmtLngth"]),
InstallMethod = Convert.ToString(mySqlReader["InstallMthd"]),
Origin = Convert.ToString(mySqlReader["EquipmentNumber1"]),
Destination = Convert.ToString(mySqlReader["EquipmentNumber2"]),
Status = Convert.ToString(mySqlReader["CblStatus"])
});
}
答案 1 :(得分:1)
通过向返回的列添加别名来修改查询:
SELECT CableNumber,
CblType,
ApprxmtLngth,
ME.EquipmentNumber AS EquipmentNumber1,
ME2.EquipmentNumber AS EquipmentNumber2,
CblStatus,
InstallMthd, [...]
然后,您可以分别引用EquipmentNumber1
和EquipmentNumber2
。
答案 2 :(得分:1)
你能否给列不同的aliases?
SQL别名用于临时重命名表或列 标题。
像
这样的东西SELECT
CableNumber,
CblType,
ApprxmtLngth,
ME.EquipmentNumber as MEEquipmentNumber,
ME2.EquipmentNumber as ME2EquipmentNumber,
CblStatus,
InstallMthd,
S.PrdtCd
...
然后,您将在C#代码中使用这些别名。像
这样的东西while (mySqlReader.Read())
{
CableID_Controller.CList.Add(new CableID_Model
{
CableNumber = Convert.ToString(mySqlReader["CableNumber"]),
CableType = Convert.ToString(mySqlReader["CblType"]),
SupplierPart = Convert.ToString(mySqlReader["PrdtCd"]),
CableLength = Convert.ToInt32(mySqlReader["ApprxmtLngth"]),
InstallMethod = Convert.ToString(mySqlReader["InstallMthd"]),
Origin = Convert.ToString(mySqlReader["MEEquipmentNumber"]),
Destination = Convert.ToString(mySqlReader["ME2EquipmentNumber"]),
Status = Convert.ToString(mySqlReader["CblStatus"])
});
}