验证检查数据库中的重复数据

时间:2012-08-01 01:19:08

标签: c# arrays linq

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

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

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();
        }
    }

任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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");     
            }
        }
    }