2012-07-13 4 views
1

사용자가 데이터베이스를 만들었다면 데이터베이스에 동일한 스테이션 이름이 있는지 확인하는 방법을 알 수 없습니다. 난 배열에 데이터베이스의 열을 저장할 수 있지만 나는 여전히 확인하는 방법을 얻을 수 없습니다. 아무도 그것으로 나를 도울 수 있습니까? 나는 지금 막 붙어있다.데이터베이스에서 반복되는 값을 확인하기 위해 TextBox와 배열을 비교하십시오.

데이터베이스에 데이터를 생성하는 버튼 핸들러의 코드입니다.

private void btnCreate_Click(object sender, EventArgs e) 
    { 
     using (testEntities Setupctx = new testEntities()) 
     { 
      station creStation = new station(); 
      creStation.Station1 = txtStation.Text; 
      creStation.Seats = cbSeats.SelectedItem.ToString(); 
       Setupctx.stations.AddObject(creStation); 
       Setupctx.SaveChanges(); 
       txtStation.Text = ""; 
       cbSeats.SelectedIndex = -1; 
       MessageBox.Show("New Station Has Been Created."); 
     } 
    } 

이것은 열을 배열에 저장하는 코드입니다.

private string[] StationNameList() 
    { 
     using (testEntities Setupctx = new testEntities()) 
     { 
      return Setupctx.stations.Select(x => x.Station1).OrderBy(x => x).ToArray(); 
     } 
    } 

아무도 도와 줄 수 있습니까? 귀하의 도움을 크게 주시면 감사하겠습니다.

답변

1

스테이션이 데이터베이스에 이미 존재하는지 확인하려면 스테이션을 추가하기 전에 btnCreate_Click에서 StationNameList의 스테이션 목록을 얻은 다음 텍스트 상자에 입력 된 스테이션 이름이 이미 존재하는지 확인하십시오 그 목록. 당신은

private void btnCreate_Click(object sender, EventArgs e) 
    { 
     using (testEntities Setupctx = new testEntities()) 
     { 
     string[] stations = StationNameList(); 

     if(stations.Contains(txtStation.Text)) 
       { 
       //already exists 
       } 
     else 
       { 
       //does not exists 
       //your code to insert the new object 
       } 
     } 
    } 
+0

고마워요! 나는 그것을 끝낼 수 있었다! – Philemon

0

내가이 도움이되기를 바랍니다이

 private void btnCreate_Click(object sender, EventArgs e) 
    { 
     using (testEntities Setupctx = new testEntities()) 
     { 
      station creStation = new station(){Station1=txtStation.Text,Name=NameTextbox.text,Seats = cbSeats.SelectedItem.ToString()}; 
      if (!Setupctx.Stations.Any(st => st.Name == creStation.Name)) 
      { 
       Setupctx.stations.AddObject(creStation); 
       Setupctx.SaveChanges(); 
       txtStation.Text = ""; 
       cbSeats.SelectedIndex = -1; 
       MessageBox.Show("New Station Has Been Created."); 
      } 
     } 
    } 

시도 할 수 있고, 난 당신이 역 클래스의 이름 속성을 가지고 있으며, UI의 일부 텍스트 상자에 결합되어 기대합니다.

관련 문제