为DataTable设置主键

Gets or sets an array of columns that function as primary keys for the data table.

[Visual Basic]
<Serializable>
Public Property PrimaryKey As DataColumn ()
[C#]
[Serializable]
public DataColumn[] PrimaryKey {get; set;}
[C++]
[Serializable]
public: __property DataColumn* get_PrimaryKey();
public: __property void set_PrimaryKey(DataColumn*[]);
[JScript]
public
   Serializable
function get PrimaryKey() : DataColumn[];
public function set PrimaryKey(DataColumn[]);
Property Value
An array of DataColumn objects.

Exceptions
Exception Type Condition
DataException The key is a foreign key.

Remarks
The primary key of a table must be unique to identify the record in the table. It's also possible to have a table with a primary key made up of two or more columns. This occurs when a single column can't contain enough unique values. For example, a two column primary key might consist of a "FirstName" and "LastName" column. Because primary keys can be made up of more than one column, the PrimaryKey property consists of an array of DataColumn objects.

Example
[Visual Basic, C#] The first example shows how to return the primary key columns for a DataTable displayed in a DataGrid. The second example demonstrates how to set the primary key columns for a DataTable.

[Visual Basic]
Private Sub GetPrimaryKeys(myTable As DataTable)
   ' Create the array for the columns.
   Dim colArr() As DataColumn
   colArr = myTable.PrimaryKey
   ' Get the number of elements in the array.
   Console.WriteLine("Column Count: " & colArr.Length.ToString())
   Dim i As Integer
   For i = 0 To colArr.GetUpperBound(0)
      Console.WriteLine(colArr(i).ColumnName & colArr(i).DataType.ToString())
   Next i
End Sub

Private Sub SetPrimaryKeys()
   ' Create a new DataTable and set two DataColumn objects as primary keys.
   Dim myTable As DataTable = new DataTable()
   Dim keys(2) As DataColumn
   Dim myColumn  As DataColumn
   ' Create column 1.
   myColumn = New DataColumn()
   myColumn.DataType = System.Type.GetType("System.String")
   myColumn.ColumnName= "FirstName"
   ' Add the column to the DataTable.Columns collection.
   myTable.Columns.Add(myColumn)
   ' Add the column to the array.
   keys(0) = myColumn

   ' Create column 2 and add it to the array.
   myColumn = New DataColumn()
   myColumn.DataType = System.Type.GetType("System.String")
   myColumn.ColumnName = "LastName"
   myTable.Columns.Add(myColumn)
   ' Add the column to the array.
   keys(1) = myColumn
   ' Set the PrimaryKeys property to the array.
   myTable.PrimaryKey = keys
End Sub
[C#]
private void GetPrimaryKeys(DataTable myTable){
   // Create the array for the columns.
   DataColumn[] colArr;
   colArr = myTable.PrimaryKey;
   // Get the number of elements in the array.
   Console.WriteLine("Column Count: " + colArr.Length);
   for(int i = 0; i < colArr.Length; i++){
      Console.WriteLine(colArr[i].ColumnName + colArr[i].DataType);
   }
}

private void SetPrimaryKeys(){
   // Create a new DataTable and set two DataColumn objects as primary keys.
   DataTable myTable = new DataTable();
   DataColumn[] keys = new DataColumn[2];
   DataColumn myColumn;
   // Create column 1.
   myColumn = new DataColumn();
   myColumn.DataType = System.Type.GetType("System.String");
   myColumn.ColumnName= "FirstName";
   // Add the column to the DataTable.Columns collection.
   myTable.Columns.Add(myColumn);
   // Add the column to the array.
   keys[0] = myColumn;

   // Create column 2 and add it to the array.
   myColumn = new DataColumn();
   myColumn.DataType = System.Type.GetType("System.String");
   myColumn.ColumnName = "LastName";
   myTable.Columns.Add(myColumn);
   // Add the column to the array.
   keys[1] = myColumn;
   // Set the PrimaryKeys property to the array.
   myTable.PrimaryKey = keys;
}
[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button  in the upper-left corner of the page.

Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows .NET Server family

时间: 2024-09-20 00:07:21

为DataTable设置主键的相关文章

SQL Server 2008设置主键和把它设为自增

环境:SQL Server 2008 问题:设置主键和把它设为自增. 解决:点击table->选中表- >design->选中需要设置主键的字段,单击右键"设置主键"即可. 若要设置主键自增 ,在列属性中找到标识规范,单击左边的"+"号,把否改为是,其他默认即可.

mysql设置主键出错,可我对照网上教程找不出错误

问题描述 mysql设置主键出错,可我对照网上教程找不出错误 请告诉我哪儿错了,谢谢! 解决方案 primary key('book_id'),//这一行首先将book_id的单引号去掉,然后将后面的逗号去掉就可以了,我将改后的代码运行成功了 另外你可以用可视化管理工具来操作的: 如果回答对你有帮助,请采纳 解决方案二: 是不是内置错误或者要区分的大小写错误 解决方案三: 我觉得还是用一个可视化软件操作方便些的 解决方案四: 第五行primary key后边的逗号去掉

sqlite developer 怎么设置主键Id自增长呢

问题描述 sqlite developer 怎么设置主键Id自增长呢 刚接触sqlite数据库,然后想建表操作,用的是sqlite developer,但不知该如何设置主键自增长呢 解决方案 创建表的时候给该键加上 PRIMARY KEY AUTOINCREMENT ?

autoincrement-vs2013 中,如何设置主键identity为true?

问题描述 vs2013 中,如何设置主键identity为true? 以前还有identity这个属性,现在找不到了,那如何设置主键的autoincrement啊? 解决方案 [1]在表名上右键选择修改 [2]在打开的画面中选中你要设置为自动增长的字段名称 [3]在下面的"列属性"页面中找到 [标识规范] 一项,并打开前面的加号 [4]把(是标识)改为"是" [5]标识增量就是每次自动增长的增量了 [6]保存 解决方案二: 这个是在数据库中设置,或者你用sql语句来

请教一个关于EF框架的问题,数据库没有设置主键怎么进行增删改

问题描述 最近公司要求我使用C#开发服务接口,使用EF框架连接Oracle数据库,进行数据的操作,由于我之前没有深入接触过EF,现在对一些没有设置主键的表进行插入时发生出错,提示"更新条目时出错."请教一下要怎么解决这个问题,谢谢了!! 解决方案 解决方案二:急,望知道的大神告诉我一下!!!解决方案三: 解决方案四:表没设置主键,ef不能插入数据解决方案五:用sql写啊.解决方案六:虽然表没有定义主键,但一定有一个或几个字段能够代表行的唯一性,它(们)就是主键的字段,找到以后,到EF设

PowerDesigner-VBSrcipt-自动设置主键,外键名等(SQL Server)

原文:PowerDesigner-VBSrcipt-自动设置主键,外键名等(SQL Server) 在PowerDesigner中的设计SQL Server 数据表时,要求通过vbScript脚本实现下面的功能:   主键:pk_TableName 外键:fk_TableName_ForeignKeyColumnList   当字段作为主键,而且类型为smallint,int,bigint,那么要设置Identity =true. 当字段作为主键,而且类型为uniqueidentifier,那么

在Mysql,SqlServer及Oracle中设置主键自动增长

1.把主键定义为自动增长标识符类型 在mysql中,如果把表的主键设为auto_increment类型,数据库就会自动为主键赋值.例如: create table customers(id int auto_increment primary key not null, name varchar(15)); insert into customers(name) values("name1"),("name2"); select id from customers;

Sql Server代码设置主键并自动生长

  新建一张数据表,将Userid设为主键并自动增长 create table T_UserLogin(UserId int not null primary key identity(1,1),UserName nvarchar(50) not null,Pass varchar(50) not null ) 修改已经建好的一张数据表,将Userid设为主键并自动生长 alter table tb alter column id int not null alter table tb add

SQL Server设置主键自增长列(使用sql语句实现)_MsSql

1.新建一数据表,里面有字段id,将id设为为主键 复制代码 代码如下: create table tb(id int,constraint pkid primary key (id)) create table tb(id int primary key ) 2.新建一数据表,里面有字段id,将id设为主键且自动编号 复制代码 代码如下: create table tb(id int identity(1,1),constraint pkid primary key (id)) create