用MSFlexGrid控件怎么连接SQL
我不会连接数据库 。数据库我已经建好了 服务器名是GAOBAONAN用户名是SA 密码是417652621 要详细步骤
请高手帮我 本人在线等。
本人QQ417652621
本人想做一个 2个按钮 1个MSFlexGrid
2个按钮分别是显示数据 和退出
数据库名是货物信息 表是xinxi 让MSFlexGrid 控件显示我表里的内容
VB使用MSFlexGrid控件怎么连接SQL详细步骤 本人在线等
答案:2 悬赏:70 手机版
解决时间 2021-03-09 11:00
- 提问者网友:骑士
- 2021-03-08 22:05
最佳答案
- 五星知识达人网友:夜余生
- 2021-03-08 23:45
1、链接SQL数据库:
db.ConnectionTimeout = 10
db.CursorLocation = adUseServer
db.ConnectionString = "driver={SQL Server};server=GAOBAONAN;uid=sa;pwd=417652621;database=货物信息"
db.Open
2、链接数据表:
strSQL = "select * from xinxi"
RS.Open strSQL, db, 2, 2
3、显示数据单击事件:
Private Sub command1_Click()
db.CursorLocation = adUseServer
db.ConnectionString = "driver={SQL Server};server=GAOBAONAN;uid=sa;pwd=417652621;database=货物信息"
db.Open
strSQL = "select * from xinxi"
RS.Open strSQL, db, 2, 2
Do While Not RS.EOF
SST = SST + 1
RS.MoveNext
Loop
RS.Close
Set RS = Nothing
RS.Open strSQL, db, 2, 2
MSFlexGrid1.Clear
With MSFlexGrid1
If Not RS.EOF Then
.Cols = 5 '(需要显示的列数,修改为你需要的列数)
.Rows = SST + 1 '自动设置显示行数
.TextMatrix(0, 0) = "列名称1"
.TextMatrix(0, 1) = "列名称2"
.TextMatrix(0, 2) = "列名称3"
'........................
.ColWidth(0) = 600 '列宽度
.ColWidth(1) = 2600
.ColWidth(2) = 900
'.........................
For I = 1 To SST
.TextMatrix(I, 0) = RS("字段1")
.TextMatrix(I, 1) = RS("字段2")
.TextMatrix(I, 2) = RS("字段3")
'...............................
RS.MoveNext
Next
Else
.Clear
End If
End With
RS.Close
Set RS = Nothing
db.Close
Set db = Nothing
End Sub
4、退出事件
Private Sub command2_Click()
End
End Sub
db.ConnectionTimeout = 10
db.CursorLocation = adUseServer
db.ConnectionString = "driver={SQL Server};server=GAOBAONAN;uid=sa;pwd=417652621;database=货物信息"
db.Open
2、链接数据表:
strSQL = "select * from xinxi"
RS.Open strSQL, db, 2, 2
3、显示数据单击事件:
Private Sub command1_Click()
db.CursorLocation = adUseServer
db.ConnectionString = "driver={SQL Server};server=GAOBAONAN;uid=sa;pwd=417652621;database=货物信息"
db.Open
strSQL = "select * from xinxi"
RS.Open strSQL, db, 2, 2
Do While Not RS.EOF
SST = SST + 1
RS.MoveNext
Loop
RS.Close
Set RS = Nothing
RS.Open strSQL, db, 2, 2
MSFlexGrid1.Clear
With MSFlexGrid1
If Not RS.EOF Then
.Cols = 5 '(需要显示的列数,修改为你需要的列数)
.Rows = SST + 1 '自动设置显示行数
.TextMatrix(0, 0) = "列名称1"
.TextMatrix(0, 1) = "列名称2"
.TextMatrix(0, 2) = "列名称3"
'........................
.ColWidth(0) = 600 '列宽度
.ColWidth(1) = 2600
.ColWidth(2) = 900
'.........................
For I = 1 To SST
.TextMatrix(I, 0) = RS("字段1")
.TextMatrix(I, 1) = RS("字段2")
.TextMatrix(I, 2) = RS("字段3")
'...............................
RS.MoveNext
Next
Else
.Clear
End If
End With
RS.Close
Set RS = Nothing
db.Close
Set db = Nothing
End Sub
4、退出事件
Private Sub command2_Click()
End
End Sub
全部回答
- 1楼网友:玩世
- 2021-03-09 00:02
行啊: dim cn as new adodb.connection dim rs as new adodb.recordset public function adodbjet(optional dbfile as string, _ optional pwd as string) as adodb.connection on error resume next dim defdb as string '数据库文件请自行设置 defdb = app.path & "\bond.mdb" if dbfile = "" then dbfile = defdb set adodbjet = new adodb.connection adodbjet.open "provider=microsoft.jet.oledb.4.0;" & _ "persist security info=false;user id=admin; " & _ "jet oledb:database password=" & pwd & "; data source=" & dbfile if adodbjet.state <> 1 then set adodbjet = nothing end function private sub form_load() set cn = adodbjet if cn.state = 1 then rs.open "select * from member2", cn, 1, 2 set vg1.datasource = rs end if with vg1 .editable = flexedkbdmouse '可用键盘和鼠标编辑 .allowuserresizing = flexresizebothuniform '可用鼠标调整单元大小, 如果值为“flexresizeboth”则单独调整行高或列宽 .scrolltrack = true '行随滚动条翻滚 .wordwrap = true '单元中的文本过长换行显示 .additem "" '添加一行为了新增内容 end with end sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯