2013-06-24 75 views
1

嗨,大家好我有一个数据库中没有主键的表。我认为原因在于它只是一个有很多外键的表。我真的不知道我的SQL很差。所以表格不是由我创建的,但是在创建我的数据表时我遇到了问题。要更新我的数据集,该表需要一个主键。由于所有列都不是唯一的,所以我不得不制作复合键。任何人都知道。这里是第一个创建主键的代码。DataTable Vb.net创建复合键

'after using adapter and all the connection process to fill the dataset 

     Dim tableProduct As DataTable = productDataSet.Tables(0) 


    tableProduct.PrimaryKey = New DataColumn() {tableProduct.Columns(0)} 

这里是我试图创建组合键

  Dim tableProduct As DataTable = productDataSet.Tables(0) 


    tableProduct.PrimaryKey = New DataColumn() {tableProduct.Columns(0)} 
     tableProduct.PrimaryKey = New DataColumn() {tableProduct.Columns(1)} 

我最近开始vb.net以前我曾在Java和PHP,jQuery和这样的。所以我不是那么经验.Net框架如果我问错了问题请告诉我,所以我可以编辑谢谢

回答

0

如果我正确理解你的问题,你需要使用两个(或更多)数据列定义你的表主键。在上面的代码中,首先将PrimaryKey设置为一列,然后更改此主键并使用不同的列。但是总是只有一列的PrimaryKey。

尝试用这种

tableProduct.PrimaryKey = New DataColumn() _ 
           { 
            tableProduct.Columns(0), 
            tableProduct.Columns(1) 
           } 

How to: Initialize an Array Variable in VB.NET

+0

有反正检查哪些列是primarykeys以及如何的有多少?像msgbox(keycount或其他) –

+0

当然,PrimaryKey是一个也可以读取的数组。 'tableProduct.PrimaryKey.Length'和'for tableProduct.PrimaryKey'中的每个dc请参见[MSDN VB示例](http://msdn.microsoft.com/zh-cn/library/system.data.datatable.primarykey.aspx? cs-save-lang = 1&cs-lang = vb#code-snippet-1) – Steve

+0

Theres仍然有些问题,但现在有几个键我猜它只是数据库谢谢!!!!! –