2012-08-01 85 views
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(); 
     } 
    } 

任何幫助將不勝感激。

+0

究竟是什麼錯誤? – 2012-08-01 01:45:42

+0

我已經發布了我設法找到的答案以及它的工作。 – rookie 2012-08-01 01:55:56

+0

...它的工作...現在.... :) – Darek 2012-08-01 03:34:17

回答

0

一個簡單的方法是在這兩列上創建一個唯一索引,並嘗試保存在try/catch塊中。

+0

我已經找到了答案,它現在工作。我會在這裏發佈。 – rookie 2012-08-01 01:53:39

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");  
      } 
     } 
    } 
+0

@rookie不錯的工作,但請記住,這段代碼在多線程環境下會失敗。 – Darek 2012-08-01 02:44:18