如何在ACCESS資料庫表中建立各種資料型別的字段

2021-06-03 22:04:47 字數 4572 閱讀 1617

dim conn as new adodb.connection

dim rs as new adodb.recordset

private sub command1_click()

'長整型

on error goto dataerr

dim connstr as string

dim sql as string

connstr = "provider=microsoft.jet.oledb.4.0;" & _

"persist security info=false;" & _

"jet oledb:database password=lee1012"

conn.open connstr

rs.cursorlocation = aduseclient

sql = "alter table 使用者資訊 add 新字段數字 int null"

rs.open sql, conn, adopenkeyset, adlockpessimistic

'''''''''''''''''''''

set rs = nothing

set conn = nothing

msgbox "建立成功"

dataerr:

if err.number <> 0 then

msgbox err.number & err.description

set rs = nothing

set conn = nothing

exit sub

end if

end sub

private sub command2_click()

'字串

on error goto dataerr

dim connstr as string

dim sql as string

connstr = "provider=microsoft.jet.oledb.4.0;" & _

"persist security info=false;" & _

"jet oledb:database password=lee1012"

conn.open connstr

rs.cursorlocation = aduseclient

sql = "alter table 使用者資訊 add 新字段字元 varchar(40) null"

rs.open sql, conn, adopenkeyset, adlockpessimistic

'''''''''''''''''''''

set rs = nothing

set conn = nothing

msgbox "建立成功"

dataerr:

if err.number <> 0 then

msgbox err.number & err.description

set rs = nothing

set conn = nothing

exit sub

end if

end sub

private sub command3_click()

'boolean是/否

on error goto dataerr

dim connstr as string

dim sql as string

connstr = "provider=microsoft.jet.oledb.4.0;" & _

"persist security info=false;" & _

"jet oledb:database password=lee1012"

conn.open connstr

rs.cursorlocation = aduseclient

sql = "alter table 使用者資訊 add 新字段是否 bit"

rs.open sql, conn, adopenkeyset, adlockpessimistic

'''''''''''''''''''''

set rs = nothing

set conn = nothing

msgbox "建立成功"

dataerr:

if err.number <> 0 then

msgbox err.number & err.description

set rs = nothing

set conn = nothing

exit sub

end if

end sub

private sub command4_click()

'貨幣型

on error goto dataerr

dim connstr as string

dim sql as string

connstr = "provider=microsoft.jet.oledb.4.0;" & _

"persist security info=false;" & _

"jet oledb:database password=lee1012"

conn.open connstr

rs.cursorlocation = aduseclient

sql = "alter table 使用者資訊 add 新字段貨幣 money"

rs.open sql, conn, adopenkeyset, adlockpessimistic

'''''''''''''''''''''

set rs = nothing

set conn = nothing

msgbox "建立成功"

dataerr:

if err.number <> 0 then

msgbox err.number & err.description

set rs = nothing

set conn = nothing

exit sub

end if

end sub

private sub command5_click()

'建立多個字段

on error goto dataerr

dim connstr as string

dim sql as string

connstr = "provider=microsoft.jet.oledb.4.0;" & _

"persist security info=false;" & _

"jet oledb:database password=lee1012"

conn.open connstr

rs.cursorlocation = aduseclient

sql = "alter table 使用者資訊 add 新字段數字 int null, 新字段字元 varchar(40) null, 新字段是否 bit, 新字段貨幣 money"

rs.open sql, conn, adopenkeyset, adlockpessimistic

'''''''''''''''''''''

set rs = nothing

set conn = nothing

msgbox "建立成功"

dataerr:

if err.number <> 0 then

msgbox err.number & err.description

set rs = nothing

set conn = nothing

exit sub

end if

end sub

private sub command6_click()

'日期/時間

on error goto dataerr

dim connstr as string

dim sql as string

connstr = "provider=microsoft.jet.oledb.4.0;" & _

"persist security info=false;" & _

"jet oledb:database password=lee1012"

conn.open connstr

rs.cursorlocation = aduseclient

sql = "alter table 使用者資訊 add 新字段日期 date null"

rs.open sql, conn, adopenkeyset, adlockpessimistic

'''''''''''''''''''''

set rs = nothing

set conn = nothing

msgbox "建立成功"

dataerr:

if err.number <> 0 then

msgbox err.number & err.description

set rs = nothing

set conn = nothing

exit sub

end if

end sub

一**水電暖專家    

如何在資料庫表中建立復合主鍵

即多個字段同時作為主鍵 主鍵是資料庫表的乙個重要屬性,建立主鍵可以避免表中存在完全相同的記錄,也就是說主鍵在一張表中的記錄值是唯一的。建立主鍵有兩種方法 一種是在資料庫提供的gui環境中建立,另一種是通過sql語句執行建立,下面分別介紹。1.在資料庫提供的gui環境中建立 以sql7為例 輸入表資訊...

如何在MySQL中建立資料庫

mysql是一種複雜的程式。它的所有命令必須通過命令列輸入 且它不提供視覺化介面。因此,了解建立和管理資料庫的基本知識將為你節省不少時間並避免不少麻煩。參考以下方法建立乙個儲存了美國各州及其人口數的資料庫。建立資料庫。在mysql命令列中,輸入命令create database 把命令中的替換為你的...

如何在資料庫動態建表

動態建表首先需要了解statement類 statement 物件用 connection 的方法createstatement 建立,例如 建立連線物件 connection connection dbutil.getconnection 建立statement物件 statement state...