频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

为DataTable设置主键

来源: 作者: 出处:巧巧读书 2006-09-10 进入讨论组

  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 familyURL:http://www.qqread.com/dotnet/y238118.html进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
最新论坛文章
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章