我是用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