65.9K
CodeProject 正在变化。 阅读更多。
Home

创建带自动编号索引字段的表

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.50/5 (6投票s)

2009年5月6日

CPOL

2分钟阅读

viewsIcon

69980

downloadIcon

1271

创建新表并重新创建表以将现有字段设置为主键。

引言

我之前写了一篇关于 ActiveX 控件 (DataGrid) 包含 (ComboBox) 和 (DTPicker) 控件到 DataGrid 的任何列的文章。当我创建数据库文件来测试 ActiveX 时,我向表 (Students) 添加了 AutoNumberIndexed 字段 (StudentID)。 我为我的文章发现了以下问题

“我想知道如何像您在 StudentID 列中的例子一样,在 datagrid 中进行索引列?”

现在我将尝试回答这个问题。

背景

我认为创建唯一索引字段的方法不止一种。

第一种方法

在设计数据库文件时,使用 Microsoft Access,我们可以选择字段类型及其属性。

第二种方法

VB6 的菜单中,打开 Add-Ins(加载项),然后选择 Visual Data Manager(可视化数据管理器),然后使用 VisData 更改字段的属性,但 VisData 管理器与 Access 97 兼容。

第三种方法

通过代码,我尝试解释如何向现有表添加 AutoNumber 字段或 Indexed 字段,以及如何向数据库文件添加新表。

要向现有表 Customers 添加新字段 CustID

strSql = "ALTER TABLE Customers ADD COLUMN CustID Integer" : type of field is Integer.

要向现有表 Customers 添加 AutoNumber 字段 CustID,请确保该表没有另一个字段作为 AutoNumber

strSql = "ALTER TABLE Customers ADD COLUMN CustID COUNTER"

要将 AutoNumber 字段作为主键 CustID 添加到现有表 Customers,请确保该表没有另一个字段作为 AutoNumber

strSql = "ALTER TABLE Customers ADD COLUMN CustID COUNTER PRIMARY KEY"

也许有人会问:如果我想将表中现有字段设为主键,该怎么办?

我尝试回答:当添加以下引用时,您可以创建一个唯一的索引字段
将 "Microsoft ADO Ext.2.8 for DDL and Security" 添加到您的项目中。您可以在以下几行中阅读有关此想法的内容。
我也将写关于创建新表并将字段添加到表中的内容。

Using the Code

AutoNumber 字段作为主键添加: 确保您添加引用:“Microsoft ActiveX Data Objects 2.x”

Dim cn As ADODB.ConnectionDim DataFile As StringDim strCon As StringDim strSql As String
DataFile = App.Path + "\CustomDB.mdb"   
Set cn = New ADODB.Connection   
strCon = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataFile & ";"   
cn.Open strCon   	'add the field (CustomerID) as AutoNumber to the table (Customers),
		'and set it as Primary Key,   
		'make sure that table has not another field as AutoNumber:   
strSql = "ALTER TABLE Customers ADD COLUMN CustomerID COUNTER PRIMARY KEY"   
cn.Execute strSql
cn.Close   Set cn = Nothing

创建新表

确保您添加引用:“Microsoft ActiveX Data Objects 2.x”
以及引用:“Microsoft ADO Ext.2.8 for DDL and Security”

Dim strCon As String
Dim DataFile As String
Dim cn As ADODB.Connection
Dim cat As New ADOX.Catalog
Dim NewTable As ADOX.Table
Dim Indx As ADOX.Index

   DataFile = App.Path + "\CustomDB.mdb"
   Set cn = New ADODB.Connection
   strCon = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataFile & ";"
   cn.Open strCon

   Set cat.ActiveConnection = cn
   Set NewTable = New ADOX.Table
   'make sure that your database file has not table with same name,
   'of new table:
   NewTable.Name = "Employees"

   'Append Columns.
   With NewTable.Columns
      .Append "EmployeeID", adInteger 'Long Integer
      .Append "EmployeeName", adVarWChar, 30 'Text
      .Append "City", adVarWChar, 20 'Text
      .Append "Address", adVarWChar, 40 'Text
      .Append "Phone", adVarWChar, 15 'Text

      With !EmployeeID
         Set .ParentCatalog = cat
         .Properties("Autoincrement") = True 'AutoNumber.
      End With

      With !EmployeeName
         Set .ParentCatalog = cat
         .Properties("Nullable") = False 'Required.
         .Properties("Jet OLEDB:Allow Zero Length") = False
      End With
   End With

   cat.Tables.Append NewTable 'Save the table

   Set Indx = New ADOX.Index 'Create a primary key index
   Indx.Name = "PrimaryKey"
   Indx.PrimaryKey = True
   Indx.Columns.Append "EmployeeID"
   NewTable.Indexes.Append Indx

   Set Indx = Nothing
   Set NewTable = Nothing
   Set cat = Nothing

设置现有字段为主键

确保该字段没有 Null 值并且没有重复值,否则您将收到一个错误。
确保您添加了两个之前的引用。

Dim strCon As StringDim DataFile As StringDim cn As ADODB.ConnectionDim cat _
	As New ADOX.CatalogDim NewTable As ADOX.TableDim Indx As ADOX.Index

   DataFile = App.Path + "\CustomDB.mdb"
   Set cn = New ADODB.Connection
   strCon = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataFile & ";"
   cn.Open strCon

   Set cat.ActiveConnection = cn
   Set'Create a Primary Key index (Cannot contain Null value)
   NewTable = cat.Tables("Customers")

   Set Indx = New ADOX.Index
   Indx.Name = "PrimaryKey"
   Indx.PrimaryKey = True
   'make sure that table has field with the name "CustID" and cannot contain Null value,
   'or change following field name:
   Indx.Columns.Append "CustID"
   'if the field has Null value you have error.
   NewTable.Indexes.Append Indx

   Set Indx = Nothing
   Set NewTable = Nothing
   Set cat = Nothing

备注

当解压 prjTest.zip 文件时,您可以看到之前的代码是如何工作的。

最后的话

我希望这篇文章对您有所帮助,并能帮助您在应用程序中使用。 如果您有任何想法或发现任何问题,请告诉我。 感谢 CodeProject 和所有人。

-- Mostafa Kaisoun
M_Kaisoun@hotmail.com

© . All rights reserved.