创建Access DB&表 [英] Creating an Access DB & Tables

查看:69
本文介绍了创建Access DB&表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道创建一个访问数据库的好例子,然后在该数据库中使用
表。


到目前为止我找到的所有例子使用SQL数据库。


谢谢,




Does anyone know of a good example for creating a access database and then
tables within that database.

All the examples I have found so far use a SQL database.

Thanks,

Ken

推荐答案

Ken


这是我从之前的一个程序剪切并粘贴的样本

希望它有所帮助。请注意,在附加项目时,您可以指定

列数据是否可为空,大小,数据类型等等。


Mike


私有子CreateCatabase(ByVal文件名为字符串)


Dim CnnNew作为ADODB.Connection


Dim CatNew作为ADOX .Catalog


试试


''打开一个连接并创建目录(数据库)


''


CnnNew =新ADODB.Connection


CatNew =新ADOX.Catalog


CatNew.Create(" Provider = Microsoft.Jet.OLEDB.4.0;& _


" Data Source ="& filename&";" ;& _


Jet OLEDB:引擎类型= 5)

''打开连接


CnnNew.Open(" Provider = Microsoft.Jet.OLEDB.4.0;&" Data Source =" filename)


''打开目录


CatNew.ActiveConnection = Cn nNew


''


''现在根据需要构建所有数据库表。根据需要添加或删除列


''属性或属性。


''


''构建并添加第一个表


''


Dim objDesignTable As New ADOX.Table


使用objDesignTable

..Name =" xxxxxxxx"


..ParentCatalog = CatNew


使用.Columns


..Append(" Units",ADOX.DataTypeEnum.adVarWChar)


..Append( " Code",ADOX.DataTypeEnum.adVarWChar)


追加(" Change X",adDouble)


..Append(" ;密度",adDouble)


..Append(" NA1",adDouble)


..Item(" NA1") .Attributes = ColumnAttributesEnum.adColNullable

..Append(偶尔负载系数,adDouble)


..Append(" Load)案例W1",adBoolean)


..Append(" Load Case T1" ,adBoolean)


..Append("加载X",adDouble)

..Item("加载X")。 Attributes = ColumnAttributesEnum.adColNullable

..Append(" NA2",adDouble)

..Item(" NA2")。 Attributes = ColumnAttributesEnum.adColNullable

..Append(" FLAG1",adBoolean)

..Append(" FLAG2",adBoolean) )


..Append(" FLAG3",adBoolean)


结束


''创建并附加一个新的自动递增列使用


''作为表的主键


..Columns。追加(" KeyID",adInteger)


..Columns(" KeyID")。属性(" AutoIncrement")。Val ue = True

结束


''创建并附加新密钥。请注意,我们只是通过


''" KeyID"列作为主键的源。这个


''新密钥将附加到Keys Collection。


Dim objDesignKey As New ADOX.Key


objDesignKey.Name =" PrimaryKey"

objDesignKey.Type = KeyTypeEnum.adKeyPrimary


objDesignKey.Columns.Append (" KeyID")

objDesignTable.Keys.Append(objDesignKey)


''将新创建的表追加到Tables Collection


CatNew.Tables.Append(objDesignTable)


''清理对象

objDesignKey =什么都没有


objDesignTable =没什么


''。根据需要添加或删除列


''属性或属性。


catch


' '错误代码在这里


结束尝试


end sub


" Ken Kazinski" <柯********* @ discussions.microsoft.com>在消息中写道

news:42 ********************************** @ microsof t.com ...
Ken

Here''s a sample I cut and pasted from a prior program I worked on
Hope it helps. Note that when appending the items you can specify if the
column data is nullable, a size, a datatype etc etc.

Mike

Private Sub CreateDatabase(ByVal filename As String)

Dim CnnNew As ADODB.Connection

Dim CatNew As ADOX.Catalog

Try

'' Open a Connection and create the Catalog (Database)

''

CnnNew = New ADODB.Connection

CatNew = New ADOX.Catalog

CatNew.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=" & filename & ";" & _

"Jet OLEDB:Engine Type=5")
''Open the connection

CnnNew.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & filename)

''Open the Catalog

CatNew.ActiveConnection = CnnNew

''

''Now build all the Database tables as desired. Add or delete column

''attributes or properties as desired.

''

''Build and add the first Table

''

Dim objDesignTable As New ADOX.Table

With objDesignTable

..Name = "xxxxxxxx"

..ParentCatalog = CatNew

With .Columns

..Append("Units", ADOX.DataTypeEnum.adVarWChar)

..Append("Code", ADOX.DataTypeEnum.adVarWChar)

Append("Change X", adDouble)

..Append("Density", adDouble)

..Append("NA1", adDouble)

..Item("NA1").Attributes = ColumnAttributesEnum.adColNullable

..Append("Occasional Load Factor", adDouble)

..Append("Load Case W1", adBoolean)

..Append("Load Case T1", adBoolean)

..Append("Load X", adDouble)

..Item("Load X").Attributes = ColumnAttributesEnum.adColNullable

..Append("NA2", adDouble)

..Item("NA2").Attributes = ColumnAttributesEnum.adColNullable

..Append("FLAG1", adBoolean)

..Append("FLAG2", adBoolean)

..Append("FLAG3", adBoolean)

End With

''Create and Append a new auto incrementing column to be used

''as the primary key for the table

..Columns.Append("KeyID", adInteger)

..Columns("KeyID").Properties("AutoIncrement").Val ue = True

End With

''Create and Append a new key. Note that we are merely passing

''the "KeyID" column as the source of the primary key. This

''new Key will be Appended to the Keys Collection.

Dim objDesignKey As New ADOX.Key

objDesignKey.Name = "PrimaryKey"

objDesignKey.Type = KeyTypeEnum.adKeyPrimary

objDesignKey.Columns.Append("KeyID")

objDesignTable.Keys.Append(objDesignKey)

''Append the newly created table to the Tables Collection

CatNew.Tables.Append(objDesignTable)

'' clean up objects

objDesignKey = Nothing

objDesignTable = Nothing

''. Add or delete column

''attributes or properties as desired.

catch

''error code here

end try

end sub

"Ken Kazinski" <Ke*********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
有没有人知道创建一个访问数据库然后在该数据库中的表格的好例子。

我所有的例子到目前为止发现使用了一个SQL数据库。

谢谢,

Ken
Does anyone know of a good example for creating a access database and then
tables within that database.

All the examples I have found so far use a SQL database.

Thanks,

Ken



谢谢Mike。


需要什么参考?


Ken

" Michael"写道:
Thanks Mike.

What references are required?

Ken
"Michael" wrote:
Ken

这是我从之前的一个项目剪切和粘贴的样本
希望它有所帮助。请注意,在附加项目时,您可以指定
列数据是否可为空,大小,数据类型等等。

Mike

私有子创建数据库(ByVal) filename as String)

Dim CnnNew作为ADODB.Connection

Dim CatNew作为ADOX.Catalog

尝试

' '打开一个连接并创建目录(数据库)


CnnNew =新的ADODB.Connection

CatNew =新的ADOX.Catalog

CatNew.Create(" Provider = Microsoft.Jet.OLEDB.4.0;"& _

" Data Source ="& filename&" ;"& _

Jet OLEDB:引擎类型= 5)

''打开连接

CnnNew.Open( " Provider = Microsoft.Jet.OLEDB.4.0;&" Data Source ="& filename)

''打开目录

CatNew。 ActiveConnection = CnnNew

''

''现在根据需要构建所有数据库表。根据需要添加或删除列

''属性或属性。

''

''构建并添加第一个表

''

Dim objDesignTable As New ADOX.Table

使用objDesignTable

..Name =" xxxxxxxx"

..ParentCatalog = CatNew

使用.Columns

..Append(" Units",ADOX.DataTypeEnum.adVarWChar)

/> ..Append(" Code",ADOX.DataTypeEnum.adVarWChar)

追加(" Change X",adDouble)

..Append(" Density" ;,adDouble)

..Append(" NA1",adDouble)

..Item(" NA1")。Attributes = ColumnAttributesEnum.adColNullable

..Append(偶然负载系数,adDouble)

..Append(Load Case W1,adBoolean)

..Append( " Load Case T1",adBoolean)

..Append(" Load X",adDouble)

..Item(" Load X")。Attri butes = ColumnAttributesEnum.adColNullable

..Append(" NA2",adDouble)

..Item(" NA2")。Attributes = ColumnAttributesEnum.adColNullable

..Append(" FLAG1",adBoolean)

..Append(" FLAG2",adBoolean)

..Append(" FLAG3" ;,adBoolean)

结束

''创建并附加一个新的自动递增列使用

''作为主键表格

..Columns.Append(" KeyID",adInteger)

..Columns(" KeyID")。Properties(" AutoIncrement")。Val ue = True

结束

''创建并附加新密钥。请注意,我们只是通过了KeyID和KeyID。列作为主键的源。这个

''新密钥将附加到Keys Collection。

Dim objDesignKey As New ADOX.Key

objDesignKey.Name =" PrimaryKey"

objDesignKey.Type = KeyTypeEnum.adKeyPrimary

objDesignKey.Columns.Append(" KeyID")

objDesignTable.Keys.Append( objDesignKey)

''将新创建的表附加到Tables Collection

CatNew.Tables.Append(objDesignTable)

''清理对象

objDesignKey =没什么

objDesignTable =没什么

''。根据需要添加或删除列

''属性或属性。

'

''错误代码在这里

最后尝试

结束子

Ken Kazinski <柯********* @ discussions.microsoft.com>在消息中写道
新闻:42 ********************************** @ microsof t.com。 ..
Ken

Here''s a sample I cut and pasted from a prior program I worked on
Hope it helps. Note that when appending the items you can specify if the
column data is nullable, a size, a datatype etc etc.

Mike

Private Sub CreateDatabase(ByVal filename As String)

Dim CnnNew As ADODB.Connection

Dim CatNew As ADOX.Catalog

Try

'' Open a Connection and create the Catalog (Database)

''

CnnNew = New ADODB.Connection

CatNew = New ADOX.Catalog

CatNew.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=" & filename & ";" & _

"Jet OLEDB:Engine Type=5")
''Open the connection

CnnNew.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" & filename)

''Open the Catalog

CatNew.ActiveConnection = CnnNew

''

''Now build all the Database tables as desired. Add or delete column

''attributes or properties as desired.

''

''Build and add the first Table

''

Dim objDesignTable As New ADOX.Table

With objDesignTable

..Name = "xxxxxxxx"

..ParentCatalog = CatNew

With .Columns

..Append("Units", ADOX.DataTypeEnum.adVarWChar)

..Append("Code", ADOX.DataTypeEnum.adVarWChar)

Append("Change X", adDouble)

..Append("Density", adDouble)

..Append("NA1", adDouble)

..Item("NA1").Attributes = ColumnAttributesEnum.adColNullable

..Append("Occasional Load Factor", adDouble)

..Append("Load Case W1", adBoolean)

..Append("Load Case T1", adBoolean)

..Append("Load X", adDouble)

..Item("Load X").Attributes = ColumnAttributesEnum.adColNullable

..Append("NA2", adDouble)

..Item("NA2").Attributes = ColumnAttributesEnum.adColNullable

..Append("FLAG1", adBoolean)

..Append("FLAG2", adBoolean)

..Append("FLAG3", adBoolean)

End With

''Create and Append a new auto incrementing column to be used

''as the primary key for the table

..Columns.Append("KeyID", adInteger)

..Columns("KeyID").Properties("AutoIncrement").Val ue = True

End With

''Create and Append a new key. Note that we are merely passing

''the "KeyID" column as the source of the primary key. This

''new Key will be Appended to the Keys Collection.

Dim objDesignKey As New ADOX.Key

objDesignKey.Name = "PrimaryKey"

objDesignKey.Type = KeyTypeEnum.adKeyPrimary

objDesignKey.Columns.Append("KeyID")

objDesignTable.Keys.Append(objDesignKey)

''Append the newly created table to the Tables Collection

CatNew.Tables.Append(objDesignTable)

'' clean up objects

objDesignKey = Nothing

objDesignTable = Nothing

''. Add or delete column

''attributes or properties as desired.

catch

''error code here

end try

end sub

"Ken Kazinski" <Ke*********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
有没有人知道在这个数据库中创建一个访问数据库然后
表的好例子。

我到目前为止找到的所有例子都使用一个SQL数据库。

谢谢,

Ken
Does anyone know of a good example for creating a access database and then
tables within that database.

All the examples I have found so far use a SQL database.

Thanks,

Ken




Ken

添加项目COM引用

用于DDL和安全的Microsoft ADO ext(#。#)

Microsoft ActiveX数据对象#。#Library

Mike


" Ken Kazinski" <柯********* @ discussions.microsoft.com>在消息中写道

新闻:4B ********************************** @ microsof t.com ...
Ken
Add Project COM references
Microsoft ADO ext (#.#) for DDL and Security
Microsoft ActiveX Data Objects #.# Library
Mike

"Ken Kazinski" <Ke*********@discussions.microsoft.com> wrote in message
news:4B**********************************@microsof t.com...
谢谢Mike。

需要什么参考?

$

" Michael" ;写道:
Thanks Mike.

What references are required?

Ken
"Michael" wrote:
Ken

这是我从之前的一个项目剪切和粘贴的样本
希望它有所帮助。请注意,在附加项目时,您可以指定
列数据是否可为空,大小,数据类型等等。

Mike

私有子创建数据库(ByVal) filename as String)

Dim CnnNew作为ADODB.Connection

Dim CatNew作为ADOX.Catalog

尝试

' '打开一个连接并创建目录(数据库)


CnnNew =新的ADODB.Connection

CatNew =新的ADOX.Catalog

CatNew.Create(" Provider = Microsoft.Jet.OLEDB.4.0;"& _

" Data Source ="& filename&" ;"& _

Jet OLEDB:引擎类型= 5)

''打开连接

CnnNew.Open( " Provider = Microsoft.Jet.OLEDB.4.0;&" Data Source ="&
filename)

''打开目录

CatNew.ActiveConnection = CnnNew

''

''现在根据需要构建所有数据库表。根据需要添加或删除列

''属性或属性。

''

''构建并添加第一个表

''

Dim objDesignTable As New ADOX.Table

使用objDesignTable

..Name =" xxxxxxxx"

..ParentCatalog = CatNew

使用.Columns

..Append(" Units",ADOX.DataTypeEnum.adVarWChar)

/> ..Append(" Code",ADOX.DataTypeEnum.adVarWChar)

追加(" Change X",adDouble)

..Append(" Density" ;,adDouble)

..Append(" NA1",adDouble)

..Item(" NA1")。Attributes = ColumnAttributesEnum.adColNullable

..Append(偶然负载系数,adDouble)

..Append(Load Case W1,adBoolean)

..Append( " Load Case T1",adBoolean)

..Append(" Load X",adDouble)

..Item(" Load X")。Attri butes = ColumnAttributesEnum.adColNullable

..Append(" NA2",adDouble)

..Item(" NA2")。Attributes = ColumnAttributesEnum.adColNullable

..Append(" FLAG1",adBoolean)

..Append(" FLAG2",adBoolean)

..Append(" FLAG3" ;,adBoolean)

结束

''创建并附加一个新的自动递增列使用

''作为主键表格

..Columns.Append(" KeyID",adInteger)

..Columns(" KeyID")。Properties(" AutoIncrement")。Val ue = True

结束

''创建并附加新密钥。请注意,我们只是通过了KeyID和KeyID。列作为主键的源。这个

''新密钥将附加到Keys Collection。

Dim objDesignKey As New ADOX.Key

objDesignKey.Name =" PrimaryKey"

objDesignKey.Type = KeyTypeEnum.adKeyPrimary

objDesignKey.Columns.Append(" KeyID")

objDesignTable.Keys.Append( objDesignKey)

''将新创建的表附加到Tables Collection

CatNew.Tables.Append(objDesignTable)

''清理对象

objDesignKey =没什么

objDesignTable =没什么

''。根据需要添加或删除列

''属性或属性。

'

''错误代码在这里

最后尝试

结束子

Ken Kazinski <柯********* @ discussions.microsoft.com>在消息中写道
新闻:42 ********************************** @ microsof t.com。 ..
Ken

Here''s a sample I cut and pasted from a prior program I worked on
Hope it helps. Note that when appending the items you can specify if the
column data is nullable, a size, a datatype etc etc.

Mike

Private Sub CreateDatabase(ByVal filename As String)

Dim CnnNew As ADODB.Connection

Dim CatNew As ADOX.Catalog

Try

'' Open a Connection and create the Catalog (Database)

''

CnnNew = New ADODB.Connection

CatNew = New ADOX.Catalog

CatNew.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & _

"Data Source=" & filename & ";" & _

"Jet OLEDB:Engine Type=5")
''Open the connection

CnnNew.Open("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=" &
filename)

''Open the Catalog

CatNew.ActiveConnection = CnnNew

''

''Now build all the Database tables as desired. Add or delete column

''attributes or properties as desired.

''

''Build and add the first Table

''

Dim objDesignTable As New ADOX.Table

With objDesignTable

..Name = "xxxxxxxx"

..ParentCatalog = CatNew

With .Columns

..Append("Units", ADOX.DataTypeEnum.adVarWChar)

..Append("Code", ADOX.DataTypeEnum.adVarWChar)

Append("Change X", adDouble)

..Append("Density", adDouble)

..Append("NA1", adDouble)

..Item("NA1").Attributes = ColumnAttributesEnum.adColNullable

..Append("Occasional Load Factor", adDouble)

..Append("Load Case W1", adBoolean)

..Append("Load Case T1", adBoolean)

..Append("Load X", adDouble)

..Item("Load X").Attributes = ColumnAttributesEnum.adColNullable

..Append("NA2", adDouble)

..Item("NA2").Attributes = ColumnAttributesEnum.adColNullable

..Append("FLAG1", adBoolean)

..Append("FLAG2", adBoolean)

..Append("FLAG3", adBoolean)

End With

''Create and Append a new auto incrementing column to be used

''as the primary key for the table

..Columns.Append("KeyID", adInteger)

..Columns("KeyID").Properties("AutoIncrement").Val ue = True

End With

''Create and Append a new key. Note that we are merely passing

''the "KeyID" column as the source of the primary key. This

''new Key will be Appended to the Keys Collection.

Dim objDesignKey As New ADOX.Key

objDesignKey.Name = "PrimaryKey"

objDesignKey.Type = KeyTypeEnum.adKeyPrimary

objDesignKey.Columns.Append("KeyID")

objDesignTable.Keys.Append(objDesignKey)

''Append the newly created table to the Tables Collection

CatNew.Tables.Append(objDesignTable)

'' clean up objects

objDesignKey = Nothing

objDesignTable = Nothing

''. Add or delete column

''attributes or properties as desired.

catch

''error code here

end try

end sub

"Ken Kazinski" <Ke*********@discussions.microsoft.com> wrote in message
news:42**********************************@microsof t.com...
>有没有人知道创建访问数据库的好例子
>那么
>该数据库中的表格。
>
>到目前为止我找到的所有示例都使用SQL数据库。
>
>谢谢,
>
> Ken
>
> Does anyone know of a good example for creating a access database and
> then
> tables within that database.
>
> All the examples I have found so far use a SQL database.
>
> Thanks,
>
> Ken
>




这篇关于创建Access DB&amp;表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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