如何删除数据表中的主键列? [英] Howto remove a primary key column in a datatable?

查看:141
本文介绍了如何删除数据表中的主键列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我知道你必须先删除约束。以下位无效
无效(其中t是DataTable)。它给出了''引用未设置为

对象''错误


t.PrimaryKey.Clear(t.PrimaryKey,0,1)

如果t.Columns.Contains(" MYKEY")那么

如果t.Columns.CanRemove(t.Columns(" MYKEY"))那么

t.Columns.Remove(" MYKEY")

结束如果

结束如果

Hi,

I understand you must first remove the constraint. The following bit does
not work (where t is a DataTable). It gives a ''reference not set to an
object'' error

t.PrimaryKey.Clear(t.PrimaryKey, 0, 1)
If t.Columns.Contains("MYKEY") Then
If t.Columns.CanRemove(t.Columns("MYKEY")) Then
t.Columns.Remove("MYKEY")
End If
End If

推荐答案




感谢您的发布。至于你提到的问题,似乎

是非引用异常。是因为我们没有提供正确的
ColumnName或DataColumn实例。我已尝试在本地进行一些测试,只要CanRemove

返回true,我们就可以从数据表中删除某个数据列。


我认为您可以尝试将DataColumn对象实例提供到Remove

方法而不是columnName中,以查看它是否有帮助。例如:


如果t.Columns.Contains(" KEY")那么

如果t.Columns.CanRemove(t.Columns(" KEY") ;))然后

dim col as DataColumn = t.Columns(" KEY")

t.Columns.Remove(col)

结束如果

结束如果


如果您还有其他任何发现,请随时在此处发布。谢谢。

问候,


Steven Cheng

微软在线支持


获取安全! www.microsoft.com/security

(此帖子按原样提供,不作任何保证,并且不授予

权利。)

Hi ,

Thanks for your posting. As for the problem you mentioned, it seems that
the "non reference exception" is due to we didn''t provide a correctly
ColumnName or DataColumn instance. I''ve tried do some tests locally and we
can remove a certain datacolumn from datatable as long as the CanRemove
return true.

I think you can try provide the DataColumn object instance into the Remove
method instead of the columnName to see whether it helps. For example:

If t.Columns.Contains("KEY") Then
If t.Columns.CanRemove(t.Columns("KEY")) Then
dim col as DataColumn = t.Columns("KEY")
t.Columns.Remove(col)
End If
End If

If you have anyother findings, please also feel free to post here. Thanks.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


好的,是的,我会认为这是一个快速修复,但我相信它还有更多的东西。


我从来不需要得到它尝试删除它时列的实例。

它与主键列有关,删除主键

似乎会损害列名不知何故。我有其他代码片段,我从

集合中删除主键后,我得到了一个''找不到列''错误。例如。删除

主键后,t.AcceptChanges也会感到困惑。


我们这里有错误吗?
All Right, Yeah, I''ll regard that as a quick fix, but I am sure that there''s
something more to it.

I never needed to get the instance of the column when trying to remove it.
It has to do with it being a Primary Key Column, and removing the Primary Key
seems to hurt the column name somehow. I have other pieces of code where I
get a ''Column not found'' error after removing the Primary Key from the Keys
collection. E.g. the t.AcceptChanges also gets confused after removing a
Primary Key.

Do we have a bug here?


嗨Martin,


我已经在myside上做了一些进一步的测试,似乎是删除方法

也将columnName作为param效果很好。是DataTable(DAtaSet)在

你的应用程序是从DataBase检索的,并且该表有一列

PK还是复合PK?我测试了从数据库中检索到的DataTable或者是在程序上构建的包含单列PK的
。我删除了

PK设置,然后通过Colums.Remove(" ColumnName")删除PK列,

工作正常。所以我不确定你的应用中是否还有其他特别的东西。还会尝试我上面提到的东西或者提供一个简单的

演示代码示例,以便我可以尝试在我这边重复它吗?

此外,如果您有任何其他问题,请随时告诉我。

谢谢。


问候,


Steven Cheng
Microsoft在线支持


安全! www.microsoft.com/security

(此帖子按原样提供,不作任何保证,并且不授予

权利。)

Hi Martin,

I''ve done some further test on myside and seems the Remove method which
take the columnName as param also works well. Is the DataTable(DAtaSet) in
your app retrieved from the DataBase and is the table has a single columns
PK or composite PK? I''ve test on DataTable retrieved from database or
programly built on the fly which contains a single column PK. I remove the
PK setting and then remove the PK column via Colums.Remove("ColumnName") ,
that worked ok. So I''m not sure whether there''re anything else particular
in your app. Would also try the thing I mentioned above or provide a simple
demo code sample so that I can try repro it on my side?
Also, if you have any other concerns, please feel free to let me know.
Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


这篇关于如何删除数据表中的主键列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆