2014-11-28 1 views
0

연락처 및 주소 테이블 두 개가있는 로컬 데이터베이스 (SQL)가 있습니다. Contact 테이블은 Address 테이블의 기본 키에 연결된 외래 키인 5 개의 주소 필드 (Address1, Address2, ...)를 포함합니다. ik가 원하는 것은 콘택트 이름을 선택 (예를 들어 콤보 박스를 사용)하고, 연락처에 연결된 모든 주소를 보는 것입니다. 나는 C# 프로그래밍의 완벽한 멍청한 놈이다. 누구든지 연락처 이름을 선택하여 주소를 볼 수있는 방법을 보여줄 수 있습니까?외래 키와 연결된 형태의 C#보기 데이터

EDIT (코딩을 시도한 후) : 좋아, 이것은 내가 얻는 것입니다. 나는 두 가지 형태를 가지고있다. FORM 1에는 단추, 이름 및 성을 보는 DataGrid 뷰가 있습니다. textBox1과 textBox2에 firstname과 lastname을 입력하고 button1을 누르면 firstname 또는 lastname과 일치하는 레코드 목록이 생성됩니다. 0 열의 버튼을 클릭하면 연락처 시트가 표시됩니다. firstname과 lastname을 tboFNAME 및 tboLNAME이라는 텍스트 상자에 전달하려고 시도했지만 아무 것도 이러한 텍스트 상자에 표시되지 않습니다.

다음 단계에서는 주소록 (외래 키)을 연락처 시트에 전달한 다음 해당 텍스트 상자에 연결된 데이터를로드하고 싶습니다.

FORM 1 : 당신은 아마 알고

public partial class Form1 : Form 
{ 
    //SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\xxx\Documents\Visual Studio 2013\Projects\xxx\xxx\xxx.mdf;Integrated Security=True;Connect Timeout=30"); 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\xxx\Documents\Visual Studio 2013\Projects\xxx\xxx\xxx.mdf;Integrated Security=True;Connect Timeout=30"); 
     dataGridView1.Visible = true; 
     int varCount; 
     varCount = 0; 
     int i = 0; 
     for (i = 1 ; i < dataGridView1.Rows.Count-1; i++) 
     { 
      if (!dataGridView1.Rows[i].IsNewRow) 
      { 

       if (dataGridView1[3, i].Value.ToString() == textBox1.Text 
        || dataGridView1[5, i].Value.ToString() == textBox2.Text 
        ) 
       { 
        dataGridView1.Rows[i].Visible = true; 

        varCount += 1; 
        Console.WriteLine(varCount); 
        int RHeight = dataGridView1.RowTemplate.Height; 
        int gridHeight = (varCount * RHeight) + RHeight; 
        dataGridView1.Height = gridHeight; 
       } 

       else 
       { 
        dataGridView1.Rows[i].Visible = false; 
       } 
      } 

     } 
    } 
    private void Form1_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'sAFIREDBDataSet1.contactdata' table. You can move, or remove it, as needed. 
      this.contactdataTableAdapter1.Fill(this.sAFIREDBDataSet1.contactdata); 
     this.contactdataTableAdapter.Fill(this.sAFIREDBDataSet.contactdata); 
     } 
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
     { 
     var senderGrid = (DataGridView)sender; 
     String fnameRef = (String)dataGridView1.Rows[e.RowIndex].Cells[3].Value; 
     String lnameRef = (String)dataGridView1.Rows[e.RowIndex].Cells[5].Value; 
     if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && 
     e.RowIndex >= 0) 
     { 
      Contactsheet myForm = new Contactsheet(); 
      myForm.getFNAME = fnameRef; 
      myForm.getLNAME = lnameRef; 
      myForm.Show(); 
     } 
    } 
} 

FORM 2 (Contactsheet) 모든

public partial class Contactsheet : Form 
{ 
    public Contactsheet() 
    { 
     InitializeComponent(); 
    } 
    public string getFNAME; 
    public string getLNAME; 
    private void Contactsheet_Load(object sender, EventArgs e) 
    { 
     tboFNAME.Text = getFNAME; 
     tboLNAME.Text = getLNAME; 
    } 

} 
+2

귀하의 질문이 너무 광범위합니다, 문제를 명시하고 몇 가지 코드를 표시하십시오. 단계별로 시작하십시오. 당신이 할 수없는 첫 번째 일은 무엇입니까? – mybirthname

답변

0

먼저 당신은 당신의 SQL DB를 연결해야합니다. 가장 간단한 방법은 Entity Framework (버전 5 또는 6)를 사용하는 것입니다. 새 edmx 파일을 만들고 데이터베이스에 새 연결을 만들고 테이블을 가져옵니다.

몇 가지 코드를 작성하십시오. 당신이 알아 낸 것일 수도 있습니다. 그렇지 않다면 당신의 시도의 예와 함께 더 정확한 질문을하십시오 :)