『壹』 在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