用vb怎么设置数据库的自动编号主键? 50
下面是我实验的代码,数据库是建成功了,就是往里面写记录的时候,主键不能自动编号,不知道问题出在哪里,往高手指点一下!谢谢!DimctAsADOX.CatalogDimid...
下面是我实验的代码,数据库是建成功了,就是往里面写记录的时候,主键不能自动编号,不知道问题出在哪里,往高手指点一下!谢谢!
Dim ct As ADOX.Catalog
Dim idx As ADOX.Index
Dim tb As Table
Dim iKey As Key
Dim strDB As String
Dim myDefaultvalue
'创建数据库
strDB = "Access_db" '指定数据库名称
If Dir(App.Path & "\" & strDB & ".mdb") <> "" Then
Kill App.Path & "\" & strDB & ".mdb"
End If
Set cat = New ADOX.Catalog
cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\" & strDB & ".mdb;" & _
"Jet OLEDB:Database Password=abc" '不要密码,可以省略
'连接数据库
Set cat = New ADOX.Catalog
cat.ActiveConnection = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\" & strDB & ".mdb;" & _
"Jet OLEDB:Database Password=abc"
'创建新数据库中的表 tb_info
Set tb = New Table
tb.Name = "wzdz"
tb.Columns.Append "编号", adInteger
tb.Columns.Append "网站名称", adVarWChar, 50
tb.Columns.Append "网站地址", adVarWChar, 50
tb.Columns.Append "网站描述", adVarWChar, 50
cat.Tables.Append tb
'创建字段 id 为主键
Set iKey = New Key
iKey.Name = "123456"
iKey.Type = adKeyPrimary
iKey.RelatedTable = "wzdz"
iKey.Columns.Append "编号"
cat.Tables("wzdz").Keys.Append iKey
'创建字段id的索引
Set idx = New ADOX.Index
idx.Name = "自动编号"
idx.Columns.Append "编号"
' 将索引追加到表上
tb.Indexes.Append idx
'指定iDub的默认值
myDefaultvalue = "1"
cat.Tables("wzdz").Columns("网站描述").Properties("Default").Value = myDefaultvalue
'CREATE TABLE MyCustomers (CustID int IDENTITY (100,1) PRIMARY KEY, CompanyName nvarchar (50)):CustID
Set tb = Nothing
Set cat = Nothing
MsgBox "你成功创建了数据库" & strDB
End Sub
实验了一下wys_sans的方法,提示无效的sqi语句。还是不行呀!不用sqi语句不可以建主键吗? 还有我用的是ACCESS数据库 展开
Dim ct As ADOX.Catalog
Dim idx As ADOX.Index
Dim tb As Table
Dim iKey As Key
Dim strDB As String
Dim myDefaultvalue
'创建数据库
strDB = "Access_db" '指定数据库名称
If Dir(App.Path & "\" & strDB & ".mdb") <> "" Then
Kill App.Path & "\" & strDB & ".mdb"
End If
Set cat = New ADOX.Catalog
cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\" & strDB & ".mdb;" & _
"Jet OLEDB:Database Password=abc" '不要密码,可以省略
'连接数据库
Set cat = New ADOX.Catalog
cat.ActiveConnection = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & App.Path & "\" & strDB & ".mdb;" & _
"Jet OLEDB:Database Password=abc"
'创建新数据库中的表 tb_info
Set tb = New Table
tb.Name = "wzdz"
tb.Columns.Append "编号", adInteger
tb.Columns.Append "网站名称", adVarWChar, 50
tb.Columns.Append "网站地址", adVarWChar, 50
tb.Columns.Append "网站描述", adVarWChar, 50
cat.Tables.Append tb
'创建字段 id 为主键
Set iKey = New Key
iKey.Name = "123456"
iKey.Type = adKeyPrimary
iKey.RelatedTable = "wzdz"
iKey.Columns.Append "编号"
cat.Tables("wzdz").Keys.Append iKey
'创建字段id的索引
Set idx = New ADOX.Index
idx.Name = "自动编号"
idx.Columns.Append "编号"
' 将索引追加到表上
tb.Indexes.Append idx
'指定iDub的默认值
myDefaultvalue = "1"
cat.Tables("wzdz").Columns("网站描述").Properties("Default").Value = myDefaultvalue
'CREATE TABLE MyCustomers (CustID int IDENTITY (100,1) PRIMARY KEY, CompanyName nvarchar (50)):CustID
Set tb = Nothing
Set cat = Nothing
MsgBox "你成功创建了数据库" & strDB
End Sub
实验了一下wys_sans的方法,提示无效的sqi语句。还是不行呀!不用sqi语句不可以建主键吗? 还有我用的是ACCESS数据库 展开
2个回答
展开全部
直接用SQL语句创建表时,将字段设置为自动编号主键就可以了。
dim cn as adodb.connection
dim sql as string
set cn=new adodb.connection
cn.open "driver={microsoft access driver (*.mdb)};uid=admin;pwd=;dbq=" & app.path & "\Access_db.mdb"
sql="'CREATE TABLE MyCustomers (CustID int IDENTITY (100,1) PRIMARY KEY, CompanyName nvarchar (50))"
cn.execute sql
set cn=nothing
dim cn as adodb.connection
dim sql as string
set cn=new adodb.connection
cn.open "driver={microsoft access driver (*.mdb)};uid=admin;pwd=;dbq=" & app.path & "\Access_db.mdb"
sql="'CREATE TABLE MyCustomers (CustID int IDENTITY (100,1) PRIMARY KEY, CompanyName nvarchar (50))"
cn.execute sql
set cn=nothing
展开全部
直接用SQL语句创建表时,将字段设置为自动编号主键就可以了。
dim cn as adodb.connection
dim sql as string
set cn=new adodb.connection
cn.open "driver={microsoft access driver (*.mdb)};uid=admin;pwd=;dbq=" & app.path & "\Access_db.mdb"
sql="'CREATE TABLE MyCustomers (CustID int IDENTITY (100,1) PRIMARY KEY, CompanyName nvarchar (50))"
cn.execute sql
set cn=nothing
dim cn as adodb.connection
dim sql as string
set cn=new adodb.connection
cn.open "driver={microsoft access driver (*.mdb)};uid=admin;pwd=;dbq=" & app.path & "\Access_db.mdb"
sql="'CREATE TABLE MyCustomers (CustID int IDENTITY (100,1) PRIMARY KEY, CompanyName nvarchar (50))"
cn.execute sql
set cn=nothing
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询