1

我刚刚开始使用 C# 并想创建一个简单的数据库应用程序,它允许我创建一个客户条目(表Customer),然后为该客户子项创建记录(类似于表Orders)。

我已经安装了 VS2012 express 和 SQL2012 express。

  1. 我首先在数据库中创建了一个名为的表,customer现在可以显示它并从中添加/删除。
  2. 然后我创建了一个名为 的表children,并在其中创建了一个外键 -custid来自customer表。
  3. 我将此 ( children) 表添加到我的解决方案中,并且在dataSource与设计器一起编辑时,我可以看到 2 个表之间存在关联KF-Children-customers
  4. 最后我DataGridView在我的解决方案中添加了一个,单击 [smart-tag] 按钮并想选择外键关系但它不可见

问:children在我已经将customer表添加到解决方案之后,我添加了表和外键等,我需要做些什么吗?

我真的才刚刚开始使用 C# 和 .Net,我以前没有做过任何编程,所以如果我没有完全解释清楚,请可怜我!

克雷格

4

2 回答 2

0

在客户中有一个身份列并将其设为主键,在儿童中有一个身份列并创建一个外键。数据绑定代码:

void gridbind() { try { string qrystring = "你的查询";

             SqlCommand cmd = new SqlCommand(qrystring, conn);
             SqlDataAdapter DA = new SqlDataAdapter(qrystring, conn);
             SqlCommandBuilder scb = new SqlCommandBuilder(DA);
             DataTable dTable = new DataTable();
             DA.Fill(dTable);
             GridView1.DataSource = dTable;
             GridView1.DataBind();

}
         catch (SqlException)
         {
             lblnameinfo.Text = "Gridview binding error";
         }
         finally
         {
             if (conn != null)
             {
                 conn.Close();
             }
         }
     }

*如果您将数据从一个表引用到另一个表,例如外部约束,则需要连接。*这是一个示例代码..可以帮助你..

于 2012-10-27T11:21:54.780 回答
0

你的问题不是很清楚。客户是 DB 还是 Table?如果您在具有外键约束的数据库中有两个不同的表,您可能需要使用“JOIN”,特别是内部连接可能会对您有所帮助。

SQL JOINS 在这里很容易解释:http ://www.lsql-tutorial.ru

示例查询:“SELECT userlist.username,tblcountry.country from userlist inner join tblcountry on tblcountry.countryID=userlist.countryname”;

表:用户列表、tblcountry。这两个表都受到外部约束。

于 2012-10-27T10:16:52.040 回答