将现有列标记为数据表中的主键 [英] marking existing column as primary key in datatable

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

问题描述



我希望该datatable具有现有列的主键。



我该怎么做?

解决方案

假设数据中的列名称你想成为主键的表被称为 pk_column ,你可以这样做(假设 dt 是你的 DataTable ):

  dt.PrimaryKey = new DataColumn [] {dt.Columns [ pk_column]}; 

如果您的主键由多列组成,可以将它们添加到数组中,如:

  dt.PrimaryKey = new DataColumn [] {dt.Columns [pk_column1],dt.Columns [pk_column2 ]}; 

所以如果你正在做student_id你的主键,你可以这样做:

  dt.PrimaryKey = new DataColumn [] {dt.Columns [student_id]}; 


I have a datatable from database on the basis of some query.

I want that datatable to have a primary key for an existing column.

How can I do this?

解决方案

Assuming the name of the column in your data table you want to be the primary key is called pk_column, you could do this (assume dt is your DataTable):

dt.PrimaryKey = new DataColumn[] { dt.Columns["pk_column"] };

If your primary key is made up of multiple columns, you can add them to the array, like so:

dt.PrimaryKey = new DataColumn[] { dt.Columns["pk_column1"], dt.Columns["pk_column2"] };

So if you were making student_id your primary key, you could do this:

dt.PrimaryKey = new DataColumn[] { dt.Columns["student_id"] };

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

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