『壹』 在VB中怎樣建立資料庫 然後再與資料庫連接
新建exe工程
菜單-工程-引用
Microsoft ActiveX Data Objects 6.1 Library
Microsoft ADO Ext. 6.0 for DDL and Security
添加模塊conn
粘貼代碼
PubliccatAsNewADOX.Catalog
PublicconnAsNewADODB.Connection
PublicrsAsNewADODB.Recordset
PublicmdbpathAsString
PrivateFunctionGetconn()'連接資料庫
Cl
mdbpath=app.Path'資料庫路徑設置為程序本體根目錄
pstr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&mdbpath&"Main.mdb"&";Mode=12;JetOLEDB:DatabasePassword=pdjj888"
conn.Openpstr
conn.CursorLocation=adUseClient
EndFunction
PublicFunctionCl()'斷開資料庫
OnErrorGoToerr
Ifconn.State=adStateOpenThen
conn.Close
Setconn=Nothing
EndIf
err:
EndFunction
PublicFunctionSe(sqlAsString)'執行sql語句
'OnErrorGoToerr
Getconn
conn.Executesql,Se
'err:
EndFunction
PublicFunctionGe(sqlAsString)'查詢
'OnErrorGoToerr
Getconn
rs.Opensql,conn,adOpenDynamic,adLockPessimistic
'err:
'Iferr=3265Then
'Ck"提示","伺服器繁忙,請重試!"
'EndIf
EndFunction
PublicFunctionCatdata()'創建初始資料庫
pstr="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&mdbpath&"Main.mdb"&";Mode=12;JetOLEDB:DatabasePassword=pdjj888"
DimsqlAsString
IfDir(mdbpath&"Main.mdb")=""Then
cat.Createpstr'創建資料庫
Setcat=Nothing
conn.Openpstr'打開資料庫
'創建商品類型表
sql="CREATETABLEspstyle(lidintprimarykey,stylenameVARCHAR,styltagVARCHAR,FontVARCHAR,styledjint)"
conn.Execute(sql)
sql="(1,1)"'自動編號
conn.Execute(sql)
'創建商品表
sql="CREATETABLEsps(sidintprimarykey,lidint,spnameVARCHAR,sptagVARCHAR,spdjint,spdatedate)"
conn.Execute(sql)
sql="(1,1)"'自動編號
conn.Execute(sql)
'創建獎券類型表
sql="CREATETABLEjzstyle(jidintprimarykey,jznameVARCHAR,jztagVARCHAR,jzslint,jzjlDouble,jzdatedate)"
conn.Execute(sql)
sql="(1,1)"'自動編號
conn.Execute(sql)
'創建獎券表
sql="CREATETABLEjzs(zidintprimarykey,jznameVARCHAR,jztagVARCHAR,jzdatedate)"
conn.Execute(sql)
sql="(1,1)"'自動編號
conn.Execute(sql)
Cl
EndIf
EndFunction