使用带有外键的链接表创建数据

时间:2012-07-30 09:09:22

标签: c# linq dictionary foreign-keys

我的MySql Workbench数据库中有3个表:station,location和locationstation。站点和位置的id是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]。对错误的看法是什么意见?

0 个答案:

没有答案