在VB中怎样建立数据库 然后再与数据库连接 200
展开全部
新建exe工程
菜单-工程-引用
Microsoft ActiveX Data Objects 6.1 Library
Microsoft ADO Ext. 6.0 for DDL and Security
添加模块conn
粘贴代码
Public cat As New ADOX.Catalog
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public mdbpath As String
Private Function Getconn() '连接数据库
Cl
mdbpath = app.Path '数据库路径设置为程序本体根目录
pstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & "\Main.mdb" & ";Mode = 12;Jet OLEDB:Database Password=pdjj888"
conn.Open pstr
conn.CursorLocation = adUseClient
End Function
Public Function Cl() '断开数据库
On Error GoTo err
If conn.State = adStateOpen Then
conn.Close
Set conn = Nothing
End If
err:
End Function
Public Function Se(sql As String) '执行sql语句
'On Error GoTo err
Getconn
conn.Execute sql, Se
'err:
End Function
Public Function Ge(sql As String) '查询
'On Error GoTo err
Getconn
rs.Open sql, conn, adOpenDynamic, adLockPessimistic
'err:
'If err = 3265 Then
'Ck "提示", "服务器繁忙,请重试!"
'End If
End Function
Public Function Catdata() '创建初始数据库
pstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mdbpath & "\Main.mdb" & ";Mode = 12;Jet OLEDB:Database Password=pdjj888"
Dim sql As String
If Dir(mdbpath & "\Main.mdb") = "" Then
cat.Create pstr '创建数据库
Set cat = Nothing
conn.Open pstr '打开数据库
'创建商品类型表
sql = "CREATE TABLE spstyle(lid int primary key,stylename VARCHAR,styltag VARCHAR,Font VARCHAR,styledj int)"
conn.Execute (sql)
sql = "ALTER TABLE spstyle ALTER COLUMN lid COUNTER (1,1)" '自动编号
conn.Execute (sql)
'创建商品表
sql = "CREATE TABLE sps(sid int primary key,lid int,spname VARCHAR,sptag VARCHAR,spdj int,spdate date)"
conn.Execute (sql)
sql = "ALTER TABLE sps ALTER COLUMN sid COUNTER (1,1)" '自动编号
conn.Execute (sql)
'创建奖券类型表
sql = "CREATE TABLE jzstyle(jid int primary key,jzname VARCHAR,jztag VARCHAR,jzsl int,jzjl Double,jzdate date)"
conn.Execute (sql)
sql = "ALTER TABLE jzstyle ALTER COLUMN jid COUNTER (1,1)" '自动编号
conn.Execute (sql)
'创建奖券表
sql = "CREATE TABLE jzs(zid int primary key,jzname VARCHAR,jztag VARCHAR,jzdate date)"
conn.Execute (sql)
sql = "ALTER TABLE jzs ALTER COLUMN zid COUNTER (1,1)" '自动编号
conn.Execute (sql)
Cl
End If
End Function
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |