我写了一个关于人事信息管理的软件,但遇到了严重的问题,程序里可以保存员工的照片,可以显示,可以保存出来,这些我都做到了,问题是,我想替换照片,程序就出现死机,或者在我添加记录的时候,第一次没有添加照片,第二次通过修改的功能,想添加照片,一样的出现死机,我在网上查了一下,stream只能用于new stream,也就是说对于新添加记录是有效的,但是对于修改记录是无效的.所以想请教高手,一旦保存了以后,怎么修改image类型的字段啊?就是更新之类的操作.....想了好方法都不行啊这是我的数据库
id name password memo photo(image类型)
代码如下
Public photoFilename As String (公用部分)
Dim mst As New ADODB.Stream
Public txtSQL As StringPrivate Sub Command1_Click() (选择图片按钮)
With CommonDialog1
.InitDir = App.Path & "\相片"
.Filter = "图像格式(*.jpg)|*.jpg"
.DefaultExt = ".jpg"
.DialogTitle = "打开影像文件"
.ShowOpen
photoFilename = .FileName
Image1.Picture = LoadPicture(photoFilename)
End With
End SubPrivate Sub cmdOK_Click() (保存按钮)
Dim sSQL As String
Dim mrc As ADODB.Recordset
Dim msgtext As String
If gintUmode = 1 Then (添加新记录模式)
If Trim(Text1(0).Text) = "" Then
MsgBox "请输入用户名称!", vbOKOnly + vbInformation, "警告"
Exit Sub
Text1(0).SetFocus
Else
sSQL = "select * from users"
Set mrc = ExecuteSQL(sSQL, msgtext)
If Not mrc.EOF Then
If Trim(mrc.Fields(0)) = Trim(Text1(0)) Then
MsgBox "用户已经存在,请重新输入用户名!", vbOKOnly + vbInformation, "警告"
Text1(0).SetFocus
Text1(0).Text = ""
Text1(1).Text = ""
Text1(2).Text = ""
Exit Sub
Else
mrc.MoveNext
End If
End If
End If
If Trim(Text1(1).Text) <> Trim(Text1(2).Text) Then
MsgBox "两次输入密码不一样,请确认!", vbOKOnly + vbInformation, "警告"
Text1(1).SetFocus
Text1(1).Text = ""
Text1(2).Text = ""
Exit Sub
Else
If Text1(1).Text = "" Then
MsgBox "密码不能为空!", vbOKOnly + vbInformation, "警告"
Text1(1).SetFocus
Text1(1).Text = ""
Text1(2).Text = ""
Else
mrc.AddNew
mrc.Fields(0) = Trim(Text1(0).Text)
mrc.Fields(1) = Trim(Text1(3).Text)
mrc.Fields(2) = Trim(Text1(1).Text)
mrc.Fields(3) = ""
mst.Type = adTypeBinary
mst.Open
If photoFilename <> "" Then mst.LoadFromFile photoFilename
mrc.Fields(4) = mst.Read
mrc.Update
mrc.Close
MsgBox "添加用户成功!", vbOKOnly + vbInformation, "添加用户信息"
Unload Me
frmUser.Adodc1.RecordSource = "select * from users order by id"
frmUser.Adodc1.Refresh
frmUser.Label1.Caption = "共有" & frmUser.Adodc1.Recordset.RecordCount & "个记录"
End If
End If
ElseIf gintUmode = 2 Then (修改模式)
sSQL = "select * from users where id= '" & Trim(Text1(0)) & "'"
Set mrc = ExecuteSQL(sSQL, msgtext)
If Not mrc.EOF Then
mrc.Fields(0) = Trim(Text1(0))
If Text1(1).Text = "" Then
MsgBox "密码不能为空!", vbOKOnly + vbInformation, "警告"
Text1(1).SetFocus
Text1(1).Text = ""
Text1(2).Text = ""
Exit Sub
End If
If Trim(Text1(1).Text) <> Trim(Text1(2).Text) Then
MsgBox "两次输入密码不一样,请确认!", vbOKOnly + vbInformation, "警告"
Text1(1).SetFocus
Text1(1).Text = ""
Text1(2).Text = ""
Exit Sub
End If
mrc.Fields(2) = Trim(Text1(1))
mst.Type = adTypeBinary
mst.Open
If photoFilename <> "" Then mst.LoadFromFile photoFilename
mrc.Fields(4) = mst.Read
mrc.Update
mrc.Close
End If
MsgBox "信息修改成功!", vbOKOnly + vbInformation, "修改用户信息"
Unload Me
frmUser.Adodc1.RecordSource = "select * from users order by id"
frmUser.Adodc1.Refresh
frmUser.Label1.Caption = "共有" & frmUser.Adodc1.Recordset.RecordCount & "个记录"
End If
End Sub
id name password memo photo(image类型)
代码如下
Public photoFilename As String (公用部分)
Dim mst As New ADODB.Stream
Public txtSQL As StringPrivate Sub Command1_Click() (选择图片按钮)
With CommonDialog1
.InitDir = App.Path & "\相片"
.Filter = "图像格式(*.jpg)|*.jpg"
.DefaultExt = ".jpg"
.DialogTitle = "打开影像文件"
.ShowOpen
photoFilename = .FileName
Image1.Picture = LoadPicture(photoFilename)
End With
End SubPrivate Sub cmdOK_Click() (保存按钮)
Dim sSQL As String
Dim mrc As ADODB.Recordset
Dim msgtext As String
If gintUmode = 1 Then (添加新记录模式)
If Trim(Text1(0).Text) = "" Then
MsgBox "请输入用户名称!", vbOKOnly + vbInformation, "警告"
Exit Sub
Text1(0).SetFocus
Else
sSQL = "select * from users"
Set mrc = ExecuteSQL(sSQL, msgtext)
If Not mrc.EOF Then
If Trim(mrc.Fields(0)) = Trim(Text1(0)) Then
MsgBox "用户已经存在,请重新输入用户名!", vbOKOnly + vbInformation, "警告"
Text1(0).SetFocus
Text1(0).Text = ""
Text1(1).Text = ""
Text1(2).Text = ""
Exit Sub
Else
mrc.MoveNext
End If
End If
End If
If Trim(Text1(1).Text) <> Trim(Text1(2).Text) Then
MsgBox "两次输入密码不一样,请确认!", vbOKOnly + vbInformation, "警告"
Text1(1).SetFocus
Text1(1).Text = ""
Text1(2).Text = ""
Exit Sub
Else
If Text1(1).Text = "" Then
MsgBox "密码不能为空!", vbOKOnly + vbInformation, "警告"
Text1(1).SetFocus
Text1(1).Text = ""
Text1(2).Text = ""
Else
mrc.AddNew
mrc.Fields(0) = Trim(Text1(0).Text)
mrc.Fields(1) = Trim(Text1(3).Text)
mrc.Fields(2) = Trim(Text1(1).Text)
mrc.Fields(3) = ""
mst.Type = adTypeBinary
mst.Open
If photoFilename <> "" Then mst.LoadFromFile photoFilename
mrc.Fields(4) = mst.Read
mrc.Update
mrc.Close
MsgBox "添加用户成功!", vbOKOnly + vbInformation, "添加用户信息"
Unload Me
frmUser.Adodc1.RecordSource = "select * from users order by id"
frmUser.Adodc1.Refresh
frmUser.Label1.Caption = "共有" & frmUser.Adodc1.Recordset.RecordCount & "个记录"
End If
End If
ElseIf gintUmode = 2 Then (修改模式)
sSQL = "select * from users where id= '" & Trim(Text1(0)) & "'"
Set mrc = ExecuteSQL(sSQL, msgtext)
If Not mrc.EOF Then
mrc.Fields(0) = Trim(Text1(0))
If Text1(1).Text = "" Then
MsgBox "密码不能为空!", vbOKOnly + vbInformation, "警告"
Text1(1).SetFocus
Text1(1).Text = ""
Text1(2).Text = ""
Exit Sub
End If
If Trim(Text1(1).Text) <> Trim(Text1(2).Text) Then
MsgBox "两次输入密码不一样,请确认!", vbOKOnly + vbInformation, "警告"
Text1(1).SetFocus
Text1(1).Text = ""
Text1(2).Text = ""
Exit Sub
End If
mrc.Fields(2) = Trim(Text1(1))
mst.Type = adTypeBinary
mst.Open
If photoFilename <> "" Then mst.LoadFromFile photoFilename
mrc.Fields(4) = mst.Read
mrc.Update
mrc.Close
End If
MsgBox "信息修改成功!", vbOKOnly + vbInformation, "修改用户信息"
Unload Me
frmUser.Adodc1.RecordSource = "select * from users order by id"
frmUser.Adodc1.Refresh
frmUser.Label1.Caption = "共有" & frmUser.Adodc1.Recordset.RecordCount & "个记录"
End If
End Sub
mst 对象已经打开了,没有关闭而再次调用 Open 当然会出错。
http://www.51testing.com/?170805/action_viewspace_itemid_88365.html
http://www.3snews.net/html/37/7037-10510.html