【问题标题】:C# view data in form linked with foreign keyC#以与外键链接的形式查看数据
【发布时间】:2015-01-27 03:31:31
【问题描述】:

我有一个本地数据库 (SQL),其中包含两个表 Contact 和 Address。 Contact 表包含 5 个地址字段(Address1、Address2、...),它们是链接到 Address 表的主键的外键。 ik 想要做的是当我选择(例如使用组合框)联系人姓名时,查看链接到联系人的所有地址。我是 C# 编程中的一个完整的菜鸟,并且没有想法使上述情况发生。谁能告诉我如何通过选择联系人姓名来查看地址?

编辑(尝试一些编码后): 好的,这就是我能走多远。我有我的两个表格。 FORM 1 有一个datagridview,查看一个按钮、名字和姓氏。在 textBox1 和 textBox2 中输入 firstname 和 lastname 并按下 button1 会生成与 firstname OR lastname 匹配的记录列表。 单击第 0 列中的按钮会显示联系表。我尝试将名字和姓氏传递给文本框 tboFNAME 和 tboLNAME,但这些文本框中没有显示任何内容。

在下一阶段,我想将地址 ID(外键)传递给联系表,然后将链接的数据加载到相应的文本框中。

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

表格 2(联系表)

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

}

【问题讨论】:

  • 您的问题太宽泛,请说明您的问题并显示一些代码。循序渐进,第一件事是什么是你不能做的?

标签: c# foreign-keys foreign-key-relationship


【解决方案1】:

首先,您可能知道,您必须连接到您的 SQL 数据库。 我认为最简单的方法是使用实​​体框架(版本 5 或 6)。 创建新的 edmx 文件、与数据库的新连接并导入表。

尝试编写一些代码。可能你想通了。如果没有,请通过您的尝试示例提出更准确的问题:)

【讨论】:

    猜你喜欢
    • 2021-06-05
    • 2017-02-22
    • 2021-01-03
    • 2021-05-18
    • 2011-04-24
    • 2018-10-16
    • 2011-02-01
    • 2020-09-19
    • 1970-01-01
    相关资源
    最近更新 更多