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

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

问题描述

我有一个从数据库中的某些查询的基础上的数据表。

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?

推荐答案

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

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

所以,如果你是做STUDENT_ID主键,你可以这样做:

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天全站免登陆