在vb中如何用ado控件动态地创建数据库
PrivateSubCommand1_Click()'创建数据库DimfmAsString'fm变量用来获取用户输入的文件名CommonDialog1.Filter="M...
Private Sub Command1_Click() '创建数据库
Dim fm As String 'fm变量用来获取用户输入的文件名
CommonDialog1.Filter = "MDB文件(*.mdb)|*.mdb|AllFiles(*.*)|*.*|"
CommonDialog1.FilterIndex = 1
CommonDialog1.InitDir = "E:\vb例"
CommonDialog1.Flags = 6
CommonDialog1.Action = 2
If CommonDialog1.FileName = "" Then
MsgBox "你必须输入一个文件名,请重新保存一次!"
Exit Sub
Else
fm = CommonDialog1.FileName
End If
pstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;" '不能把这里的4.0改为3.51
pstr = pstr & "Data Source=" & fm & ";Persist Security Info=False"
cat.Create pstr '创建数据库
Dim tbl As New Table
cat.ActiveConnection = pstr
tbl.Name = "MyTable" '表的名称
tbl.Columns.Append "编号", adInteger '表的第一个字段
tbl.Columns.Append "姓名", adVarWChar, 8 '表的第二个字段
tbl.Columns.Append "住址", adVarWChar, 50 '表的第三个字段
cat.Tables.Append tbl '建立数据表
conn.Open pstr
rs.CursorLocation = adUseClient
rs.Open "MyTable", conn, adOpenKeyset, adLockPessimistic
rs.AddNew '往表中添加新记录
rs.Fields(0).Value = 9801
rs.Fields(1).Value = "孙悟空"
rs.Fields(2).Value = "广州市花果山"
rs.Update
End Sub
Private Sub Command2_Click() '查询
Set DataGrid1.DataSource = rs
End Sub
Private Sub Command3_Click() '更新
rs.UpdateBatch
End Sub
Private Sub Form_Load()
Dim cat As New ADOX.Catalog '不用cat用另外一个名字也可以
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim pstr As String '定义该变量是为了后面的书写方便
End Sub
这是参考别人的代码,但是运行时总是在cat.Create pstr '创建数据库
这条语句出错。请高人指点。特急!!! 展开
Dim fm As String 'fm变量用来获取用户输入的文件名
CommonDialog1.Filter = "MDB文件(*.mdb)|*.mdb|AllFiles(*.*)|*.*|"
CommonDialog1.FilterIndex = 1
CommonDialog1.InitDir = "E:\vb例"
CommonDialog1.Flags = 6
CommonDialog1.Action = 2
If CommonDialog1.FileName = "" Then
MsgBox "你必须输入一个文件名,请重新保存一次!"
Exit Sub
Else
fm = CommonDialog1.FileName
End If
pstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;" '不能把这里的4.0改为3.51
pstr = pstr & "Data Source=" & fm & ";Persist Security Info=False"
cat.Create pstr '创建数据库
Dim tbl As New Table
cat.ActiveConnection = pstr
tbl.Name = "MyTable" '表的名称
tbl.Columns.Append "编号", adInteger '表的第一个字段
tbl.Columns.Append "姓名", adVarWChar, 8 '表的第二个字段
tbl.Columns.Append "住址", adVarWChar, 50 '表的第三个字段
cat.Tables.Append tbl '建立数据表
conn.Open pstr
rs.CursorLocation = adUseClient
rs.Open "MyTable", conn, adOpenKeyset, adLockPessimistic
rs.AddNew '往表中添加新记录
rs.Fields(0).Value = 9801
rs.Fields(1).Value = "孙悟空"
rs.Fields(2).Value = "广州市花果山"
rs.Update
End Sub
Private Sub Command2_Click() '查询
Set DataGrid1.DataSource = rs
End Sub
Private Sub Command3_Click() '更新
rs.UpdateBatch
End Sub
Private Sub Form_Load()
Dim cat As New ADOX.Catalog '不用cat用另外一个名字也可以
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim pstr As String '定义该变量是为了后面的书写方便
End Sub
这是参考别人的代码,但是运行时总是在cat.Create pstr '创建数据库
这条语句出错。请高人指点。特急!!! 展开
1个回答
展开全部
首先要把Dim cat As New ADOX.Catalog '不用cat用另外一个名字也可以
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim pstr As String '定义该变量是为了后面的书写方便
放在全局变量定义位置
其次是要删除Persist Security Info=False;
改后为:
Dim cat As New ADOX.Catalog '不用cat用另外一个名字也可以
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim pstr As String '定义该变量是为了后面的书写方便
Private Sub Command1_Click() '创建数据库
Dim fm As String 'fm变量用来获取用户输入的文件名
CommonDialog1.Filter = "MDB文件(*.mdb)|*.mdb|AllFiles(*.*)|*.*|"
CommonDialog1.FilterIndex = 1
CommonDialog1.InitDir = "E:\vb例"
CommonDialog1.Flags = 6
CommonDialog1.Action = 2
If CommonDialog1.FileName = "" Then
MsgBox "你必须输入一个文件名,请重新保存一次!"
Exit Sub
Else
fm = CommonDialog1.FileName
End If
pstr = "Provider=Microsoft.Jet.OLEDB.4.0;" '不能把这里的4.0改为3.51
pstr = pstr & "Data Source=" & fm '& ";Persist Security Info=False" Persist Security Info=False;
cat.Create pstr '创建数据库
Dim tbl As New Table
cat.ActiveConnection = pstr
tbl.Name = "MyTable" '表的名称
tbl.Columns.Append "编号", adInteger '表的第一个字段
tbl.Columns.Append "姓名", adVarWChar, 8 '表的第二个字段
tbl.Columns.Append "住址", adVarWChar, 50 '表的第三个字段
cat.Tables.Append tbl '建立数据表
conn.Open pstr
rs.CursorLocation = adUseClient
rs.Open "MyTable", conn, adOpenKeyset, adLockPessimistic
rs.AddNew '往表中添加新记录
rs.Fields(0).Value = 9801
rs.Fields(1).Value = "孙悟空"
rs.Fields(2).Value = "广州市花果山"
rs.Update
End Sub
Private Sub Command2_Click() '查询
Set DataGrid1.DataSource = rs
End Sub
Private Sub Command3_Click() '更新
rs.UpdateBatch
End Sub
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim pstr As String '定义该变量是为了后面的书写方便
放在全局变量定义位置
其次是要删除Persist Security Info=False;
改后为:
Dim cat As New ADOX.Catalog '不用cat用另外一个名字也可以
Dim conn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim pstr As String '定义该变量是为了后面的书写方便
Private Sub Command1_Click() '创建数据库
Dim fm As String 'fm变量用来获取用户输入的文件名
CommonDialog1.Filter = "MDB文件(*.mdb)|*.mdb|AllFiles(*.*)|*.*|"
CommonDialog1.FilterIndex = 1
CommonDialog1.InitDir = "E:\vb例"
CommonDialog1.Flags = 6
CommonDialog1.Action = 2
If CommonDialog1.FileName = "" Then
MsgBox "你必须输入一个文件名,请重新保存一次!"
Exit Sub
Else
fm = CommonDialog1.FileName
End If
pstr = "Provider=Microsoft.Jet.OLEDB.4.0;" '不能把这里的4.0改为3.51
pstr = pstr & "Data Source=" & fm '& ";Persist Security Info=False" Persist Security Info=False;
cat.Create pstr '创建数据库
Dim tbl As New Table
cat.ActiveConnection = pstr
tbl.Name = "MyTable" '表的名称
tbl.Columns.Append "编号", adInteger '表的第一个字段
tbl.Columns.Append "姓名", adVarWChar, 8 '表的第二个字段
tbl.Columns.Append "住址", adVarWChar, 50 '表的第三个字段
cat.Tables.Append tbl '建立数据表
conn.Open pstr
rs.CursorLocation = adUseClient
rs.Open "MyTable", conn, adOpenKeyset, adLockPessimistic
rs.AddNew '往表中添加新记录
rs.Fields(0).Value = 9801
rs.Fields(1).Value = "孙悟空"
rs.Fields(2).Value = "广州市花果山"
rs.Update
End Sub
Private Sub Command2_Click() '查询
Set DataGrid1.DataSource = rs
End Sub
Private Sub Command3_Click() '更新
rs.UpdateBatch
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询