wpf 如何用C#在SQL中从外键表中检索数据

ercv8c1e  于 2023-01-21  发布在  C#
关注(0)|答案(1)|浏览(117)

我试图构建我的第一个应用程序用于学习目的,但我遇到了障碍。我的目标是从标记为PC的表和几个具有ID和字符串值的外键表中检索数据。我能够执行SQL查询,从PC表中检索所有信息(不包括外键ID),但我希望能够看到PC的“品牌”和“型号”。

我的错误消息

System.InvalidCastException: 'Specified cast is not valid.'

我的PC模型在C#中如下所示

public class PC
{
    public int PC_ID { get; set; }
    public string PC_SERIAL { get; set; }
    public string PC_NAME { get; set; }
    public Nullable<int> PC_BRAND_ID { get; set; }
    public PCBrand PC_Brand { get; set; }
    public Nullable<int> PC_MODEL_ID { get; set; }
    public PCBrand PC_Model { get; set; }
    public Nullable<int> PC_OS_ID { get; set; }
    public PCOS PC_OS { get; set; }
    public Nullable<int> PC_NET_ID { get; set; }
    public PCNet PC_Net { get; set; }
    public Nullable<int> PC_RAM_ID { get; set; }
    public PCRam PC_Ram { get; set; }
    public override string ToString()
    {
        return $"{PC_ID} - {PC_NAME} - {PC_SERIAL} - {PC_Model} - {PC_Brand} - {PC_OS} - {PC_Net} - {PC_Ram}";
    }
}

例如,我的一个外键表如下所示:

namespace Inventory.Data.Model
{
    public class PCBrand
    {
        public int PC_BRAND_ID { get; set; }
        public string PC_BRAND { get; set; }
    }
}

这是我仓库类中的代码

private const string SqlGetAllPC = @"Select PC.PC_ID, PC.PC_NAME,
     PC_BRAND.PC_BRAND,PC_MODEL.PC_MODEL,PC_NET.
     PC_NET,PC_OS.PC_OS,PC_RAM.PC_RAM
        FROM PC
        INNER JOIN PC_BRAND on PC.PC_BRAND_ID=PC_BRAND.PC_BRAND_ID
        INNER JOIN PC_MODEL ON PC.PC_MODEL_ID=PC_MODEL.PC_MODEL_ID
        INNER JOIN PC_NET ON PC.PC_NET_ID=PC_NET.PC_NET_ID
        INNER JOIN PC_OS ON PC.PC_OS_ID=PC_OS.PC_OS_ID
        INNER JOIN PC_RAM ON PC.PC_RAM_ID=PC_RAM.PC_RAM_ID";

    public IEnumerable<PC> GetAllFromPc()
    {
        IEnumerable<PC> list = null;
        try
        {
            connector.OpenConnection();
            System.Data.DataTable data = connector.GetData(SqlGetAllPC);
            if (data != null && data.Rows.Count > 0)
            {
                list = ConvertToPC(data);
            }
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            connector.CloseConnection();
        }
        return list;
    }

    private IEnumerable<PC> ConvertToPC(DataTable data)
    {
        List<PC> tmp = new List<PC>();
        foreach (DataRow row in data.Rows)
        {
            tmp.Add(new PC()
            {
                PC_ID = row.Field<int>(0),
                PC_SERIAL = row.Field<string>(1),
                PC_NAME = row.Field<string>(2),
                PC_BRAND_ID = row.Field<int>(3),
                PC_MODEL_ID = row.Field<int>(4),
                PC_OS_ID = row.Field<int>(5),
                PC_NET_ID = row.Field<int>(6),
                PC_RAM_ID = row.Field<int>(7),
            });
        }
        return tmp;
    }
}

最后是我的按钮,用来检索数据

private void GetAllButton(object sender, RoutedEventArgs e)
{
   Data.Repositories.PCRepository newpc = new Data.Repositories.PCRepository();
   IEnumberable<Data.Model.PC> data = newpc.GetAllFromPc();
   if (data != null)
   {
       StringBuilder sb = new StringBuilder();
       data.ToList()
           .ForEach(x=> sb.AppendLine(x.ToString()))
       MessageBox.Show(sb.ToString());
   }
}

通过一些编辑,我可以从我的PC表中检索。但我还想从我的数据库中的PC_Brand和PC_Model表中检索PC的型号/品牌名称。x1c 0d1x

kmbjn2e3

kmbjn2e31#

您的SELECT正在阅读我认为是字符串值的内容(PC_MODEL.PC_MODEL,PC_NET.PC_NET,PC_OS.PC_OS,PC_RAM.PC_RAM),但您将它们赋值为整数,因此强制转换无效。
为什么我确定这些是字符串?因为你的连接。你连接的是例如PC_MODEL_ID,它几乎肯定是整数。
将您的SELECT更改为:

SELECT PC.PC_ID, PC.PC_NAME, PC_BRAND.PC_BRAND,PC.PC_MODEL_ID, PC.PC_NET_ID, PC.PC_OS_ID,PC.PC_RAM_ID
FROM PC
INNER JOIN PC_BRAND on PC.PC_BRAND_ID=PC_BRAND.PC_BRAND_ID
INNER JOIN PC_MODEL ON PC.PC_MODEL_ID=PC_MODEL.PC_MODEL_ID

请注意,如果只选择整数值,则不需要所有其他连接。
编辑
您需要将选择更改为

SELECT PC.PC_ID, PC.PC_SERIAL, PC.PC_NAME, 
PC_BRAND.PC_BRAND, PC.PC_BRAND_ID, 
PC_MODEL.PC_MODEL, PC.PC_MODEL_ID, 
PC_NET.PC_NET, PC.PC_NET_ID, 
PC_OS.PC_OS, PC.PC_OS_ID, 
PC_RAM.PC_RAM, PC.PC_RAM_ID 
FROM PC 
INNER JOIN PC_BRAND on PC.PC_BRAND_ID = PC_BRAND.PC_BRAND_ID 
INNER JOIN PC_MODEL ON PC.PC_MODEL_ID = PC_MODEL.PC_MODEL_ID 
INNER JOIN PC_NET ON PC.PC_NET_ID = PC_NET.PC_NET_ID 
INNER JOIN PC_OS ON PC.PC_OS_ID = PC_OS.PC_OS_ID 
INNER JOIN PC_RAM ON PC.PC_RAM_ID= PC_RAM.PC_RAM_ID

现在您可以在ConvertToPC中执行以下操作:

private static IEnumerable<PC> ConvertToPC(DataTable data)
{
    List<PC> tmp = new List<PC>();
    foreach (DataRow row in data.Rows)
    {
        tmp.Add(new PC()
        {
            PC_ID = row.Field<int>(0),
            PC_SERIAL = row.Field<string>(1),
            PC_NAME = row.Field<string>(2),
            PC_Brand = new PCBrand
            {
                PC_BRAND = row.Field<string>(3),
                PC_BRAND_ID = row.Field<int>(4)
            },
            PC_BRAND_ID = row.Field<int>(4),
            PC_Model = new PCModel
            {
                PC_MODEL = row.Field<string>(5),
                PC_MODEL_ID = row.Field<int>(6)
            },
            PC_MODEL_ID = row.Field<int>(6),
            PC_Net = new PCNet
            {
                PC_NET = row.Field<string>(7),
                PC_NET_ID = row.Field<int>(8)
            },
            PC_NET_ID = row.Field<int>(8),
            PC_OS = new PCOS
            {
                PC_OS = row.Field<string>(9),
                PC_OS_ID = row.Field<int>(10)
            },
            PC_OS_ID = row.Field<int>(10),
            PC_Ram = new PCRam
            {
                PC_RAM = row.Field<string>(11),
                PC_RAM_ID = row.Field<int>(12)
            },
            PC_RAM_ID = row.Field<int>(12)
        });
    }
    return tmp;
}

请注意,我已经尽可能少地修改了你的代码。在实践中,我会做很多非常不同的事情。例如,所有的外键id现在都是重复的:在你的PC类中,你不需要一个PCModel类型的对象和一个intPC_MODEL_ID,你只需要PCModel对象,因为它已经包含了ID。

相关问题