请问要用DataGrid显示数据库中的内容,一定要用静态的那种ADODC控件吗?

jsjgirl 2004-05-08 10:33:28
用动态的那种ADO 行吗?
...全文
172 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
jsjgirl 2004-05-13
  • 打赏
  • 举报
回复
谢谢各位帮忙,实现到了

给分!!
zhaojin0425 2004-05-10
  • 打赏
  • 举报
回复
我一般用的就是ADO和DATAGRID的组合!~
很方便啊!~我强力推荐!`
lihonggen0 2004-05-10
  • 打赏
  • 举报
回复
工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)

Dim CN As New ADODB.Connection '定义数据库的连接
Dim Rs As New ADODB.Recordset
CN.ConnectionString = "Provider=sqloledb;Data Source=pmserver;Initial Catalog=northwind;User Id=sa;Password=sa;"
CN.Open
Rs.CursorLocation = adUseClient
Rs.Open "select * from employees", CN, adOpenDynamic, adLockOptimistic
Set DataGrid1.DataSource = Rs



-------------------
CN.ConnectionString = "Provider=sqloledb;Data Source=服务器名;Initial Catalog=数据库;User Id=用户名;Password=密码;"
haipingma 2004-05-10
  • 打赏
  • 举报
回复
用ado對象,控制比較靈活,推荐使用
puhaohmaru 2004-05-10
  • 打赏
  • 举报
回复
用那个数据库没有关系的
只要修改一下数据引擎就可以了
starsoulxp 2004-05-10
  • 打赏
  • 举报
回复
用别的数据控件也可以,像ado,rdo,odbc等
adodc控件很好用的,不过还是用动态的控制比较好

Private conn As ADODB.Connection
Private rs As ADODB.Recordset

Private Sub Form_Load()
Dim apppath As String
Dim DbFileName As String
Dim ConnectString As String
Dim i As Integer
Set conn = New ADODB.Connection
If Right(App.Path, 1) = "\" Then
apppath = App.Path
Else
apppath = App.Path & "\"
End If
DbFileName = apppath & "article.mdb"
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DbFileName & ";Persist Security Info=False;"
conn.Open ConnectString
conn.CursorLocation = adUseClient

Set rs = New ADODB.Recordset
rs.Open "select id,mc from mz", conn, adOpenDynamic, adLockPessimistic

Set DataGrid1.DataSource = rs
viena 2004-05-10
  • 打赏
  • 举报
回复
ADODC控件没有用过,一直用的都是ADO对象,偶最早是做ASP的,ADO对象比较熟
jeff__lueny 2004-05-10
  • 打赏
  • 举报
回复
别忘记引用!!
工程--->引用--->Microsoft ActiveX Data Object 2.x(版本号)
jeff__lueny 2004-05-10
  • 打赏
  • 举报
回复
我是用ADODB的

'用于执行用户给定SELECT的语句,返回对应的记录集,但只能用于查询
Public Function exesql(ByVal SQL As String) As ADODB.Recordset
Dim DB As New ADODB.Connection '声明连接Connection对象的变量DB

Dim spath As String '使用Open打开一个数据库连接
spath = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Persist Security Info=False;" _
& "Data Source =" & App.Path & "\Inv_new.mdb"
DB.Open spath
DB.CursorLocation = adUseClient

'Dim EF As New ADODB.Recordset
'If EF.State <> adStateClosed Then
' rs.Close
'End If
'EF.Open sql, conn, adOpenStatic, adLockBatchOptimistic 'sql是你的形参
'Set exesql = EF 'exesql函数名
'释放对象型变量
'Set EF = Nothing
'Set DB = Nothing
'声明记录集对象变量EF
Dim EF As New ADODB.Recordset
'执行SQL语句查询记录集,把查询的记录集赋给记录集变量
Set EF = DB.Execute(SQL) '11111-->sql形参
Set exesql = EF 'exesql函数名
'释放对象型变量
Set EF = Nothing
Set DB = Nothing
End Function
dx_cyb 2004-05-10
  • 打赏
  • 举报
回复
控件编程初中级,对象编程高级
ariexcorn 2004-05-09
  • 打赏
  • 举报
回复
推荐使用ADODB,,,比控件方便
jsjgirl 2004-05-09
  • 打赏
  • 举报
回复
我用的是SQL2000
broown 2004-05-09
  • 打赏
  • 举报
回复
引用microsoft activex data object 2.x library
即ADODB

如:
Option Explicit

Public rs As New ADODB.Recordset
Public conn As New ADODB.Connection

Private Sub Form_Load()
Dim strconn As String
strconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\InfoDb2000.mdb;Persist Security Info=False"
conn.CursorLocation = adUseClient
conn.Open strconn

If rs.State <> adStateClosed Then rs.Close
rs.Open "Select * from sc", conn, adOpenStatic, adLockBatchOptimistic ' 3,1 狀態

Set DataGrid1.DataSource = rs


End Sub
northwolves 2004-05-08
  • 打赏
  • 举报
回复
当然可以,动态ADO 更灵活,不推荐使用ADODC控件,虽然可以省下许多代码。
online 2004-05-08
  • 打赏
  • 举报
回复
'引用microsoft activex data object 2.x library
Option Explicit
Private conn As ADODB.Connection
Private rs As ADODB.Recordset

Private Sub Form_Load()
Dim apppath As String
Dim DbFileName As String
Dim ConnectString As String
Dim i As Integer
Set conn = New ADODB.Connection
If Right(App.Path, 1) = "\" Then
apppath = App.Path
Else
apppath = App.Path & "\"
End If
DbFileName = apppath & "article.mdb"
ConnectString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DbFileName & ";Persist Security Info=False;"
conn.Open ConnectString
conn.CursorLocation = adUseClient

Set rs = New ADODB.Recordset
rs.Open "select id,mc from mz", conn, adOpenDynamic, adLockPessimistic
If rs.EOF Then
Exit Sub
End If
Set DataGrid1.DataSource = rs

End Sub

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧