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
那只是创建表.还不算动态,动态要二维数组,表达行和列.如: Dim myDB As DAO.Database
Set myDB = DAO.Workspaces(0).CreateDatabase("d:\md\lll.mdb", dbLangGeneral)
myDB.Execute "Create Table " &表名 &"(" &"id Counter," &mm1 &" Text(10))"
'生成一个d:\md\lll.mdb数据库存
myDB.Close
打断它:. 表名=数组 字段= combo2数组 字段属性= combo3数组
for..i=0 to combo2.ListCount-1'主工部分处理
myDB.Execute "Create Table " &表名 &"(" & combo2.list(i) & combo3.list(i) ")"
next
这样生成的表就是一个完全动态的表名,行 列 字段都能自己定义的动态表,
调用查询/删/增/修改.都分别以数组
如:
cn.Open "provider=Microsoft.jet.OLEDB.4.0data source=" &ur
sq = "select * from " &uyu &" where " &stm(3) &"='" &Form2.Text1(3) &"'"
rs.Open sq, cn, adOpenKeyset, adLockOptimistic
If rs.RecordCount >0 Then
MsgBox "" &stm(3) &"重复,不能添加数据", vbExclamation, "查询"
rs.Close
cn.Close
查询:
For i = 1 To rs.RecordCount
Set Fliv = Form1.ListView1.ListItems.Add()
Fliv.Text = rs.Fields(stm(1))
For r = 1 To sn - 1
Fliv.SubItems(r) = rs.Fields(stm(r + 1))
Next
rs.MoveNext
Next i
'sts = stm(1) &"='" &Form3.Text1(1) &"', " &stm(2) &"='" &Form3.Text1(2) &"', " &stm(4) &"='" &Form3.Text1(4)
For r = 1 To sn - 1
rn = rn + stm(r) &"='" &Form3.Text1(r) &"', "
Next
sts = rn &stm(sn) &"='" &Form3.Text1(sn)
' Form3.Text1(sn).Enabled = False
sq = "Update " &uyu &" set " &sts &"' where " &stm(3) &"='" &Form3.Text1(3) &"'"
cn.Execute (sq)
MsgBox "数据修改完成", vbInformation, "修改"
cn.Close
Set cn = Nothing
Form1.ListView1.ListItems.Clear '先清空表格里的数据
加载列表数据
设成公用变量,全换成数组之后,反面简单明了..
只用提供表名,及字段马上就可以生成你需要数据库..动态数组的交集就是所需字
记录.查询和调用都极为便利.更重要的是大大减小了数据库负担
For i = 2 To 6tbl.Columns.Append "MyField" &i, adInteger '表的第i个字段
Next i
可以使用循环语句创建表
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)