| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1891 人关注过本帖
标题:怎么样用ADO控件连接SQL数据库
只看楼主 加入收藏
blueair
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-6-6
收藏
 问题点数:0 回复次数:7 
怎么样用ADO控件连接SQL数据库
我在做一个系统
不知道怎么用ADO连接数据库
老出错
高手
帮忙啊
搜索更多相关主题的帖子: SQL ADO 数据库 控件 
2006-06-08 08:06
blueair
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-6-6
收藏
得分:0 

ADO :没有为命令对象设置命令
这是什么原因
帮下忙吧

2006-06-08 10:35
maui2000
Rank: 1
等 级:新手上路
威 望:1
帖 子:113
专家分:0
注 册:2006-5-23
收藏
得分:0 
[CODE]做个连接模块
‘模块代码
Public mycon As New ADODB.Connection
Public myres As New ADODB.Recordset
Public strsql As String
Public Function OpenCon()
If mycon.State <> adStateOpen Then
'如果mycon没有打开 就
mycon.Open "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=fingu;Data Source=TAN"
End If
End Function
Public Function OpenRes(ByVal strsql As String)
If myres.State = adStateOpen Then
myres.Close
'如果打开了就关掉
End If
With myres
.CursorLocation = adUseClient '在客户端上运行
.CursorType = adOpenDynamic '动态游标
.Open strsql, mycon, , , adCmdText '用SQL语句打开记录集
End With
End Function

Public Function Tra(ByVal strsql As String)
mycon.BeginTrans
mycon.Execute (strsql)
mycon.CommitTrans
End Function
[/CODE]

记得引用部件

2006-06-08 10:43
maui2000
Rank: 1
等 级:新手上路
威 望:1
帖 子:113
专家分:0
注 册:2006-5-23
收藏
得分:0 

然后在程序中用

'打开记录集
Call OpenCon
Call OpenRes("SQL语句")

'关闭打开的对象

mycon.Close


2006-06-08 10:45
blueair
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-6-6
收藏
得分:0 

可以帮我看一下吗
Dim num As Integer
Dim strcnn, SQL As String
Dim rs As New ADODB.Recordset
Dim cnn As ADODB.Connection
Dim i As Integer


Private Sub Command2_Click()
Text1.SetFocus
clear
End Sub

Private Sub Command3_Click()
If Text1 = "" Then
MsgBox "请输入省", vbExclamation
Text1.SetFocus
Else
Data1.Recordset.update
Data1.Recordset.Close
Unload Me: frmgz.Show
End If
End Sub

Private Sub Command4_Click()
Unload Me: frmgz.Show
End Sub

Private Sub Form_Load()
strcnn = "driver={SQL Server};server=LEGEND-ZZTVUDV0\MATING;uid=sa;pwd=082429;database=xb"
Set cnn = New ADODB.Connection
cnn.Open strcnn
SQL = "select * from xbyz"
cnn.Execute SQL
Set rs = New ADODB.Recordset
rs.Open SQL, cnn, adOpenDynamic, adLockBatchOptimistic
rs.MoveFirst

'添加列表combo1
Combo1.AddItem "脊", 0
Combo1.AddItem "上", 1
Combo1.AddItem "中", 2
Combo1.AddItem "下", 3
Combo1.AddItem "谷", 4
Combo1.AddItem "平地", 5

'添加列表combo2
Combo2.AddItem "国有", 0
Combo2.AddItem "集体", 1
Combo2.AddItem "私人", 2
Combo2.AddItem "其他", 3
'添加列表combo3
Combo3.AddItem "东", 0
Combo3.AddItem "南", 1
Combo3.AddItem "西", 2
Combo3.AddItem "北", 3
Combo3.AddItem "东南", 4
Combo3.AddItem "东北", 5
Combo3.AddItem "西南", 6
Combo3.AddItem "西北", 7
Combo3.AddItem "无", 8
'添加列表combo4
Combo4.AddItem "平坡", 0
Combo4.AddItem "缓坡", 1
Combo4.AddItem "斜坡", 2
Combo4.AddItem "陡坡", 3
Combo4.AddItem "急坡", 4
Combo4.AddItem "险坡", 5
'添加列表combo5
Combo5.AddItem "天然林", 0
Combo5.AddItem "人工林", 1
Combo5.AddItem "飞播林", 2
'添加列表combo4
Combo6.AddItem "幼龄林", 0
Combo6.AddItem "近熟林", 1
Combo6.AddItem "成熟林", 2
Combo6.AddItem "过熟林", 3
Combo6.AddItem "中龄林", 4
clear
End Sub

Private Sub Text1_Change()
If Text1 = "" Then
Text1.SetFocus
Else
Text2.Enabled = True: Text2.SetFocus
Text2.BackColor = &H80000005
End If
End Sub
Private Sub Text2_Change()
If Text2 = "" Then
Text2.SetFocus
Else
Text3.Enabled = True: Text3.SetFocus
Text3.BackColor = &H80000005
End If

End Sub


Private Sub Text3_change()

If Text3 = "" Then
Text3.SetFocus
Else
Text4.Enabled = True: Text4.SetFocus
Text4.BackColor = &H80000005
End If

End Sub
Private Sub Text4_change()

If Text4 = "" Then
Text4.SetFocus
Else
Text5.Enabled = True: Text5.SetFocus
Text5.BackColor = &H80000005
End If

End Sub
Private Sub Text5_change()

If Text5 = "" Then
Text5.SetFocus
Else
Text6.Enabled = True: Text6.SetFocus
Text6.BackColor = &H80000005
End If

End Sub
Private Sub Text6_change()

If Text6 = "" Then
Text6.SetFocus
Else
Text7.Enabled = True: Text7.SetFocus
Text7.BackColor = &H80000005
End If
End Sub
Private Sub Text7_change()

If Text7 = "" Then
Text7.SetFocus
Else
Text8.Enabled = True: Text8.SetFocus
Text8.BackColor = &H80000005
End If

End Sub

Private Sub Text8_change()

If Text8 = "" Then
Text8.SetFocus
Else
Text9.Enabled = True: Text9.SetFocus
Text9.BackColor = &H80000005
End If

End Sub

Private Sub Text9_change()

If Text9 = "" Then
Text9.SetFocus
Else
Option1.Enabled = True: Option2.Enabled = True
End If

End Sub
Private Sub option1_click()
Text23 = "国有": Combo2.Enabled = True: Combo2.SetFocus
Combo2.BackColor = &H8000005
End Sub
Private Sub option2_click()
Text23 = "集体": Combo2.Enabled = True: Combo2.SetFocus
Combo2.BackColor = &H8000005
End Sub
Private Sub Combo2_Change()

Text10.Enabled = True: Text10.SetFocus
Text10.BackColor = &H8000005

End Sub
Private Sub Text10_Change()

If Text10 = "" Then
Text10.SetFocus
Else
Text11.Enabled = True: Text11.SetFocus
Text11.BackColor = &H80000005
End If

End Sub

Private Sub Text11_Change()

If Text11 = "" Then
Text11.SetFocus
Else
Text12.Enabled = True: Text12.SetFocus
Text12.BackColor = &H80000005
End If

End Sub
Private Sub Text12_Change()

If Text12 = "" Then
Text12.SetFocus
Else
Text13.Enabled = True: Text13.SetFocus
Text13.BackColor = &H80000005
End If

End Sub
Private Sub Text13_Change()

If Text13 = "" Then
Text13.SetFocus
Else
Text14.Enabled = True: Text14.SetFocus
Text14.BackColor = &H80000005
End If

End Sub
Private Sub Text14_Change()

If Text14 = "" Then
Text14.SetFocus
Else
Combo1.Enabled = True: Combo1.SetFocus
Combo1.BackColor = &H80000005
End If

End Sub
Private Sub Combo3_Change()

Combo4.Enabled = True: Combo4.SetFocus
Combo4.BackColor = &H8000005

End Sub
Private Sub Combo4_Change()

Text15.Enabled = True: Text15.SetFocus
Text15.BackColor = &H8000005

End Sub

Private Sub Text15_Change()

If Text15 = "" Then
Text15.SetFocus
Else
Text16.Enabled = True: Text16.SetFocus
Text16.BackColor = &H80000005
End If

End Sub
Private Sub Text16_Change()

If Text16 = "" Then
Text16.SetFocus
Else
Text17.Enabled = True: Text17.SetFocus
Text17.BackColor = &H80000005
End If

End Sub
Private Sub Text17_Change()

If Text17 = "" Then
Text17.SetFocus
Else
Combo5.Enabled = True: Combo5.SetFocus
Combo5.BackColor = &H80000005
End If

End Sub
Private Sub Combo5_Change()
If keyascii = 13 Then
Text18.Enabled = True: Text18.SetFocus
Text18.BackColor = &H8000005
End If
End Sub
Private Sub Text18_Change()

If Text18 = "" Then
Text18.SetFocus
Else
Text19.Enabled = True: Text19.SetFocus
Text19.BackColor = &H80000005
End If

End Sub
Private Sub Text19_Change()

If Text19 = "" Then
Text19.SetFocus
Else
Combo6.Enabled = True: Combo6.SetFocus
Combo6.BackColor = &H80000005
End If

End Sub
Private Sub Combo6_Change()
If keyascii = 13 Then
Text20.Enabled = True: Text20.SetFocus
Text20.BackColor = &H8000005
End If
End Sub
Private Sub Text20_Change()

If Text20 = "" Then

Text20.SetFocus
Else
Text21.Enabled = True: Text21.SetFocus
Text21.BackColor = &H80000005
End If

End Sub
Private Sub Text21_Change()

If Text21 = "" Then
Text21.SetFocus
Else
Text22.Enabled = True: Text22.SetFocus
Text22.BackColor = &H80000005
End If

End Sub
Private Sub Text22_Change()

If Text22 = "" Then
Text22.SetFocus
Else
Option3.Enabled = True: Option4.Enabled = True
End If

End Sub
Private Sub option3_click()
Text24 = "是": Option5.Enabled = True: Option6.Enabled = True
End Sub
Private Sub option4_click()
Text24 = "否": Option5.Enabled = True: Option6.Enabled = True
End Sub
Private Sub option5_click()
Text25 = "是"
Text25.SetFocus
End Sub
Private Sub option6_click()
Text25 = "否"
Text25.SetFocus
End Sub
Private Sub Command1_Click()
If Text1 = "" Then
MsgBox "请输入省!"
Text1 = ""
Text1.SetFocus
Else
Text1.SetFocus
Adodc1.Recordset.MoveLast
num = Adodc1.Recordset.RecordCount
statusbar1.panels(1).Text = ":" & num
statusbar2.panels(1).Text = ":" & num + 1 & ""
Data1.Recordset.AddNew
clear
End If
End Sub
Private Sub command1_keypress(keyascii As Integer)
If keyascii = 13 Then
If Text1 = "" Then
MsgBox "请输入省!"
Text1 = ""
Text1.SetFocus
Else
Text1.SetFocus
Data1.Recordset.MoveLast
num = Data1.Recordset.RecordCount
statusbar1.panels(1).Text = ":" & num
statusbar2.panels(1).Text = ":" & num + 1 & ""
Data1.Recordset.AddNew
clear
End If
End If
End Sub
Private Sub clear()
Combo1 = "": Combo2 = "": Combo3 = "": Combo4 = "": Combo5 = "": Combo6 = ""
Option1.Enabled = False: Option2.Enabled = False: Option3.Enabled = False: Option4.Enabled = False: Option5.Enabled = False: Option6.Enabled = False:
Combo1.BackColor = &H80000001: Combo2.BackColor = &H80000001: Combo3.BackColor = &H80000001: Combo4.BackColor = &H80000001: Combo5.BackColor = &H80000001: Combo6.BackColor = &H80000001: Combo1.BackColor = &H80000001
Combo1.Enabled = False: Combo2.Enabled = False: Combo3.Enabled = False: Combo4.Enabled = False: Combo5.Enabled = False: Combo6.Enabled = False
Text1 = "": Text2 = "": Text3 = "": Text4 = "": Text5 = "": Text6 = "": Text7 = "": Text8 = "": Text9 = "": Text10 = "": Text11 = "": Text12 = "": Text13 = "": Text14 = "": Text15 = "": Text16 = "": Text17 = "": Text18 = "": Text19 = "": Text20 = "": Text21 = "": Text22 = "":
Text2.Enabled = False: Text3.Enabled = False: Text4.Enabled = False: Text5.Enabled = False: Text6.Enabled = False: Text7.Enabled = False: Text8.Enabled = False: Text9.Enabled = False: Text10.Enabled = False: Text11.Enabled = False: Text12.Enabled = False: Text13.Enabled = False: Text14.Enabled = False: Text15.Enabled = False: Text16.Enabled = False: Text17.Enabled = False: Text18.Enabled = False: Text19.Enabled = False: Text20.Enabled = False: Text21.Enabled = False: Text22.Enabled = False:
Text2.BackColor = &H80000001: Text3.BackColor = &H80000001: Text4.BackColor = &H80000001: Text5.BackColor = &H80000001: Text6.BackColor = &H80000001: Text7.BackColor = &H80000001: Text8.BackColor = &H80000001: Text9.BackColor = &H80000001: Text10.BackColor = &H80000001: Text11.BackColor = &H80000001: Text12.BackColor = &H80000001: Text13.BackColor = &H80000001: Text14.BackColor = &H80000001: Text15.BackColor = &H80000001: Text16.BackColor = &H80000001: Text17.BackColor = &H80000001: Text18.BackColor = &H80000001: Text19.BackColor = &H80000001: Text20.BackColor = &H80000001: Text21.BackColor = &H80000001: Text22.BackColor = &H80000001:
End Sub

2006-06-08 11:23
blueair
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-6-6
收藏
得分:0 
我想要实现数据的录入
但是弄不出来
2006-06-08 11:24
maui2000
Rank: 1
等 级:新手上路
威 望:1
帖 子:113
专家分:0
注 册:2006-5-23
收藏
得分:0 
不好意思
今天在赶一个程序
晚点才能看~~
你要不先按照我的办法来
做一个连接SQL的模块
通过在程序中调用模块里的函数来连接SQL

把你之前连接SQL的办法换下
2006-06-08 14:03
blueair
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2006-6-6
收藏
得分:0 
谢谢哦
2006-06-08 14:50
快速回复:怎么样用ADO控件连接SQL数据库
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035850 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved