1

我的 MySql Workbench 数据库中有 3 个表:站、位置和位置站。station 的 id 和 location 是 locationstation 的外键。问题是当我选择 stationName 和 locationName 时,我无法将其保存到 locationstation 表中。有什么帮助吗?谢谢!

这是我将车站名称和位置链接到组合框中供用户选择的代码。

 private void Create_LS_Load(object sender, EventArgs e)
    {
        using (satsEntities Setupctx = new satsEntities())
        {
            var storeStation = (from SLS in Setupctx.stations
                               select SLS.StationName).Distinct().ToList();                                   
            foreach (var LocationStation in storeStation)
            {
                cbStation.Items.Add(LocationStation);
            }

            var storeLocation1 = (from SLS in Setupctx.stations
                                join location l in Setupctx.locations on SLS.StationID equals l.LocationID
                                select l.LocationName).Distinct().ToList();                                      
            foreach (var LocationStation1 in storeLocation1)
            {
                cbLocation.Items.Add(LocationStation1);
            }
        }
    }

这是要创建的代码。

 private void btnCreate_Click(object sender, EventArgs e)
    {
        using (satsEntities Setupctx = new satsEntities())
        {
            locationstation ls = new locationstation();
            ls.stationID = cbStation.SelectedIndex;
            ls.locationID = cbLocation.SelectedIndex;
            Setupctx.locationstations.AddObject(ls);
            Setupctx.SaveChanges();
            cbStation.SelectedIndex = -1;
            cbLocation.SelectedIndex = -1;

            MessageBox.Show("New Location Station Is Created");
        }
    }

这是我将车站和位置设置到字典中的代码。

private void cbStation_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        Dictionary<int, string> dictStation = new Dictionary<int, string>();

        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            dictStation.Add(Convert.ToInt32(dr["StationID"].ToString()), dr["StationName"].ToString());
        }

        dictStation.Equals((cbStation.SelectedValue));
    }

    private void cbLocation_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataSet ds1 = new DataSet();
        Dictionary<int, string> dictStation = new Dictionary<int, string>();

        foreach (DataRow dr in ds1.Tables[0].Rows)
        {
            dictStation.Add(Convert.ToInt32(dr["LocationID"].ToString()), dr["LocationName"].ToString());
        }

        dictStation.Equals((cbLocation.SelectedValue));
    }

错误是部分:“(ds.Tables [0].Rows中的DataRow dr)”。他们找不到表 [0]。关于错误的任何意见?

4

0 回答 0