0

我遇到了一个问题,每当用户创建新记录时,程序必须检查数据是否已经创建。我的代码目前有一些错误,我找不到错误所在。谁能给我一些意见??谢谢!

下面是代码,让你们对我的问题有更多的了解。

private void btnCreate_Click(object sender, EventArgs e)
    {
        using (satsEntities Setupctx = new satsEntities())
        {
            //int[] stations = StationNameList();
            //int[] locations = LocationNameList();
            locationstation ls = new locationstation();
            ls.stationID = Convert.ToInt32(cbStation.SelectedValue);
            ls.locationID = Convert.ToInt32(cbLocation.SelectedValue);

            var checkLS = from CLS in Setupctx.locationstations
                          where CLS.stationID == Convert.ToInt32(cbStation.SelectedValue)
                          where CLS.locationID == Convert.ToInt32(cbLocation.SelectedValue)
                          select CLS;
            if (checkLS = checked)
            {
                MessageBox.Show("This Location Station Has Been Created. Please enter a new Location Station.");
            }     
            else
                    {
                        {
                            Setupctx.locationstations.AddObject(ls);
                            Setupctx.SaveChanges();

                            cbStation.SelectedIndex = -1;
                            cbLocation.SelectedIndex = -1;

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

需要比较的列保存在数组中

private int[] LocationNameList()
    {
        using (satsEntities Setupctx = new satsEntities())
        {
            return Setupctx.locationstations.Select(x => x.locationID).OrderBy(x => x).ToArray();
        }
    }

    private int[] StationNameList()
    {
        using (satsEntities Setupctx = new satsEntities())
        {
            return Setupctx.locationstations.Select(y => y.stationID).OrderBy(y => y).ToArray();
        }
    }

任何帮助将不胜感激。

4

2 回答 2

0

这个答案对我有用,因为我自己想通了。

private void btnCreate_Click(object sender, EventArgs e)
    {
        using (satsEntities Setupctx = new satsEntities())
        {
            locationstation ls = new locationstation();
            int Stations = Convert.ToInt32(cbLocation.SelectedValue);
            int Locations = Convert.ToInt32(cbStation.SelectedValue);
            ls.stationID = Convert.ToInt32(cbStation.SelectedValue);
            ls.locationID = Convert.ToInt32(cbLocation.SelectedValue);

            var AuthCheck = from Ls in Setupctx.locationstations
                            where (Ls.locationID == Stations && Ls.stationID == Locations)
                            select Ls;

            if (AuthCheck.Count() != 0)
            {
                MessageBox.Show("This Location Station Has Been Created. Please enter a new Location Station.");
            }     
            else
            {
                Setupctx.locationstations.AddObject(ls);
                Setupctx.SaveChanges();

                cbStation.SelectedIndex = -1;
                cbLocation.SelectedIndex = -1;

                MessageBox.Show("New Location Station Is Created");     
            }
        }
    }
于 2012-08-01T01:54:29.000 回答
0

一种粗略的方法是在这两列上创建一个唯一索引,并尝试保存在 try/catch 块中。

于 2012-08-01T01:45:17.520 回答