| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 398 人关注过本帖
标题:[求助]请问大家数据库连接error怎么改
只看楼主 加入收藏
hikers126
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-5-9
收藏
 问题点数:0 回复次数:0 
[求助]请问大家数据库连接error怎么改

我是用vb做的毕业设计.连接数据库总是提示对象关闭时不能操作,不知道是什么问题如何修改啊,带码如下:求大虾帮忙啊!!!!


Private Sub SaveToDB(ByRef Fld As ADODB.Field, DiskFile As String)
'定义数据块数组

Dim mstream As ADODB.Stream
'判断文件是否存在
If Dir(DiskFile) <> "" Then
SourceFile = FreeFile
'读入二进制文件
Set mstream = New ADODB.Stream
mstream.LoadFromFile (DiskFile)
'存入数据库
Fld.Value = mstream.Read
Else
MsgBox "文件不存在,请重新指定文件!", vbExclamation, "注意"
End If
End Sub
Private Function GetFileName() As String

CommonDialog1.CancelError = True
On Error GoTo CancelErr
CommonDialog1.Filter = "所有文件(*.*)|*.*"
CommonDialog1.ShowOpen
GetFileName = CommonDialog1.FileName
Exit Function
CancelErr:
GetFileName = ""

End Function
Private Sub cmdSave2DB_Click()
Call Save2DB
End Sub
Private Sub Save2DB()
'建立一个ADO数据连接
Dim DataConn As New ADODB.Connection
Dim DataRec As New ADODB.Recordset
Dim strSQL As String
'Set DataRec = New ADODB.Recordset
'若数据库连接出错,则转向ConnectionERR
On Error GoTo ConnectionERR



'建立一个连接字串
'这个连接串可能根据数据库配置的不同而不同
DataConn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;"
DataConn.ConnectionString = DataConn.ConnectionString & "Persist Security Info=false;"
DataConn.ConnectionString = DataConn.ConnectionString & "Initial Catalog=tempdb;"
DataConn.ConnectionString = DataConn.ConnectionString & "Data Source=A1"

'建立数据库连接
DataConn.Open

'若RecordSet建立出错,则转向RecordsetERR
On Error GoTo RecordSetERR
'DataConn.Open "dsn=localhost"
strSQL = "SELECT * FROM Temp "
DataRec.Open strSQL, DataConn, adOpenDynamic, adLockOptimistic
If DataRec.EOF Then DataRec.AddNew

On Error GoTo OtherERR

DataRec.Fields("Name").Value = Text1.Text
Call SaveToDB(DataRec.Fields("Binary"), Text2.Text)
DataRec.Update
Exit Sub

ConnectionERR:
'错误处理程序
MsgBox "数据库连接错误," & Err.Description, vbCritical, "出错"
Exit Sub

RecordSetERR:
MsgBox "RecordSet生成错误," & Err.Description, vbCritical, "出错"
DataConn.Close
Exit Sub

OtherERR:
MsgBox "其他错误," & Err.Description, vbCritical, "出错"

DataRec.Close

DataConn.Close




End Sub

Private Sub Command1_Click()
Text2.Text = GetFileName
End Sub

搜索更多相关主题的帖子: 数据库 
2006-05-23 13:09
快速回复:[求助]请问大家数据库连接error怎么改
数据加载中...
 
   



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

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