2012-07-30 2 views
0

이제 배열 비교 및 ​​생성에 대해 고민하고 있습니다. 나는 2 개의 테이블, 역 및 위치가있다. 방송국 내부의 열은 ID와 방송국 이름입니다. 거기 위치 테이블에 동일한 이름이 다를뿐입니다. 나는 id를 배열에 넣고 배열의 이름을 넣는다. 이름과 ID를 비교하여 사용자가 이름을 선택할 때 프로그램이 어떤 ID가 선택된 이름에 속해 있는지를 알고 기본 키를 사용하여 다른 테이블에 저장하도록했습니다. 여기에 내가 만든 코드가 있었지만 그것을 해결하는 방법을 모르겠습니다. 희망을 좀 불러주세요. 감사!배열 비교 및 ​​데이터베이스에 데이터 작성

private void btnCreate_Click(object sender, EventArgs e) 
    { 
     using (testEntities Setupctx = new testEntities()) 
     { 
      string[] stations = StationNameList(); 
      int[] stationsID = StationIDList(); 
      string[] locations = LocationNameList(); 
      int[] locationsID = LocationIDList(); 

      int Stationindex = cbStation.SelectedIndex; 
      int Locationindex = cbLocation.SelectedIndex; 

      trolleyservice TS = new trolleyservice(); 
      TS.stationID = cbStation.SelectedIndex; 
      TS.locationID = cbLocation.SelectedIndex; 
      TS.tServiceTiming = txtTime.Text; 
      TS.tServiceType = txtType.Text; 
      Setupctx.trolleyservices.AddObject(TS); 
      txtTime.Text = ""; 
      txtType.Text = ""; 

      MessageBox.Show("New Trolley Service Has Been Created."); 
     } 
    } 

여기에는 각 테이블에 대해 생성 한 모든 배열이 포함됩니다.

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

     } 
    } 

    private int[] StationIDList() 
    { 
     using (testEntities Setupctx = new testEntities()) 
     { 
      return Setupctx.stations.Select(sid => sid.idstations).OrderBy(sid => sid).ToArray(); 
     } 
    } 

    private string[] LocationNameList() 
    { 
     using (testEntities Setupctx = new testEntities()) 
     { 
      return Setupctx.locations.Select(l => l.Location1).OrderBy(l => l).ToArray(); 
     } 
    } 

    private int[] LocationIDList() 
    { 
     using (testEntities Setupctx = new testEntities()) 
     { 
      return Setupctx.locations.Select(lid => lid.idlocation).OrderBy(lid => lid).ToArray(); 
     } 
    } 
+0

정확히 무엇이 문제입니까? –

+0

스테이션 이름을 선택할 때 스테이션 이름이 ID임을 알고 해당 ID를 해당 테이블의 외래 키로 사용할 때 다른 테이블에 저장해야합니다. – rookie

답변

0

여기서 당신은, 일을 할 수 대신 이름과 같은 역 클래스의 목록을 취할 수있는 ID의 두 개의 서로 다른 배열을 복용.

private List<stations> StationNameList() 
    { 
    using (testEntities Setupctx = new testEntities()) 
    { 
     return Setupctx.stations.OrderBy(s => s.Station1).ToList(); 
    } 
    } 

이제 방송국 이름과 관련된 스테이션 ID를 얻을 것이다 이제 대신 selectedIndex 속성의 conbobox의 SelectedValue 속성을 사용하여이

cbStation.DataSource =StationNameList() ; 
cbStation.DataTextField = "Station1";//it is text field that you want to display 
cbStation.DataValueField = "idstations";//it is value field of combo box 
cbStation.DataBind(); 

같은 데이터 소스로 cbStation에이 목록을 지정합니다.

TS.stationID = cbStation.SelectedValue; 

같은 일이 또한 위치

당신은 또한 여기에 목록을 대신 배열을 사용할 수 있습니다위한 것입니다.