vb动态建立一个access数据库

vb动态建立一个access数据库,第1张

我的作法是,先创建一个空的access数据库

1.mdb

放在程序的目录

Dim

destPath

As

String

destPath

=

App.Path

&

"\hello.mdb"

FileCopy

App.Path

&

"\1.mdb",

destPath '复制成新的,相当于建立

Dim

cn

As

New

ADODB.Connection

cn.Open

"Provider=Microsoft.Jet.OLEDB.4.0Data

Source="

&

destPath

&

""

Dim

sql

As

String

sql

=

"create

table

table1

(姓名

ntext

not

null,

家庭住址

ntext

null)"

cn.Execute

sql

sql

=

"create

table

table2

(电话号码

ntext

not

null)"

cn.Execute

sql

cn.Close

Set

cn

=

Nothing

'你要连接什么样的控件?下面是连接DataGrid控件的例子:

Option Explicit

Private Function Selectsql(SQL As String) As ADODB.Recordset '返回ADODB.Recordset对象

Dim ConnStr As String

Dim Conn As ADODB.Connection

Dim rs As ADODB.Recordset

Set rs = New ADODB.Recordset

Set Conn = New ADODB.Connection

'On Error GoTo MyErr:

ConnStr = "Provider=SQLOLEDB.1Persist Security Info=TrueUser ID=saPassword=001234Initial Catalog=数据库名Data Source=MERRYCHINA" '这是连接SQL数据库的语句

Conn.Open ConnStr

rs.CursorLocation = adUseClient

rs.Open Trim$(SQL), Conn, adOpenDynamic, adLockOptimistic

Set Selectsql = rs

'Exit Function

'MyErr:

'Set rs = Nothing

'Set Conn = Nothing '释放相关的系统资源

'MsgBox Err.Description, vbInformation, "系统提示" '显示出错信息

End Function

Private Sub Form_Load()

Dim rs As ADODB.Recordset

Set rs = Selectsql(" select * from 表名")

If rs.RecordCount >0 Then

Set DataGrid1.DataSource = rs

DataGrid1.Refresh

End If

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6641841.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存