使用流对象保存和显示图片 打开vb6,新建工程。添加两个按钮,一个image控件注意:Access中的photo字段类型为OLE对象.SqlServer中的photo字段类型为Image.'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本‘2.5版本以下不支持Stream对象Dim iConcstr As StringDim iConc As ADODB.Connection '保存文件到数据库中Sub s_SaveFile() Dim iStm As ADODB.Stream Dim iRe As ADODB.Recordset Dim iConcstr As String '读取文件到内容 Set iStm = New ADODB.Stream With iStm .Type = adTypeBinary '二进制模式 .Open .LoadFromFile App.Path + "\test.jpg" End With '打开保存文件的表 Set iRe = New ADODB.Recordset With iRe .Open "select * from img", iConc, 1, 3 .AddNew '新增一条记录 .Fields("photo") = iStm.Read .Update End With '完成后关闭对象 iRe.Close iStm.CloseEnd Sub Sub s_ReadFile() Dim iStm As ADODB.Stream Dim iRe As ADODB.Recordset '打开表Set iRe = New ADODB.Recordset‘得到最新添加的纪录 iRe.Open "select top 1 * from img order by id desc", iConc, adOpenKeyset, adLockReadOnly '保存到文件 Set iStm = New ADODB.Stream With iStm .Mode = adModeReadWrite .Type = adTypeBinary .Open .Write iRe("photo")‘这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误. .SaveToFile App.Path & "\test1.jpg" End With Image1.Picture = LoadPicture(App.Path & "\test1.jpg") '关闭对象 iRe.Close iStm.CloseEnd Sub Private Sub Command1_Click()Call s_ReadFileEnd Sub Private Sub Command2_Click()Call s_SaveFileEnd Sub Private Sub Form_Load() '数据库连接字符串 iConcstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _ ";Data Source=F:\csdn_vb\database\保存图片\access图片\img.mdb"‘下面的语句是连接sqlserver数据库的. ‘iConcstr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _‘ "User ID=sa;Password=;Initial Catalog=test;Data Source=yang" Set iConc = New ADODB.Connection iConc.Open iConcstrEnd Sub Private Sub Form_Unload(Cancel As Integer)iConc.CloseSet iConc = NothingEnd Sub 你也可以使用二进制方式读取,但是你可以看到流对象的简单高效.可以参考MS的知识库文章来加强这个知识点:http://support.microsoft.com/default.aspx?scid=kb;EN-US;258038
打开vb6,新建工程。添加两个按钮,一个image控件注意:Access中的photo字段类型为OLE对象.SqlServer中的photo字段类型为Image.'** 引用 Microsoft ActiveX Data Objects 2.5 Library 及以上版本‘2.5版本以下不支持Stream对象Dim iConcstr As StringDim iConc As ADODB.Connection '保存文件到数据库中Sub s_SaveFile() Dim iStm As ADODB.Stream Dim iRe As ADODB.Recordset Dim iConcstr As String '读取文件到内容 Set iStm = New ADODB.Stream With iStm .Type = adTypeBinary '二进制模式 .Open .LoadFromFile App.Path + "\test.jpg" End With '打开保存文件的表 Set iRe = New ADODB.Recordset With iRe .Open "select * from img", iConc, 1, 3 .AddNew '新增一条记录 .Fields("photo") = iStm.Read .Update End With '完成后关闭对象 iRe.Close iStm.CloseEnd Sub Sub s_ReadFile() Dim iStm As ADODB.Stream Dim iRe As ADODB.Recordset '打开表Set iRe = New ADODB.Recordset‘得到最新添加的纪录 iRe.Open "select top 1 * from img order by id desc", iConc, adOpenKeyset, adLockReadOnly '保存到文件 Set iStm = New ADODB.Stream With iStm .Mode = adModeReadWrite .Type = adTypeBinary .Open .Write iRe("photo")‘这里注意了,如果当前目录下存在test1.jpg,会报一个文件写入失败的错误. .SaveToFile App.Path & "\test1.jpg" End With Image1.Picture = LoadPicture(App.Path & "\test1.jpg") '关闭对象 iRe.Close iStm.CloseEnd Sub Private Sub Command1_Click()Call s_ReadFileEnd Sub Private Sub Command2_Click()Call s_SaveFileEnd Sub Private Sub Form_Load() '数据库连接字符串 iConcstr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False" & _ ";Data Source=F:\csdn_vb\database\保存图片\access图片\img.mdb"‘下面的语句是连接sqlserver数据库的. ‘iConcstr = "Provider=SQLOLEDB.1;Persist Security Info=True;" & _‘ "User ID=sa;Password=;Initial Catalog=test;Data Source=yang" Set iConc = New ADODB.Connection iConc.Open iConcstrEnd Sub Private Sub Form_Unload(Cancel As Integer)iConc.CloseSet iConc = NothingEnd Sub 你也可以使用二进制方式读取,但是你可以看到流对象的简单高效.可以参考MS的知识库文章来加强这个知识点:http://support.microsoft.com/default.aspx?scid=kb;EN-US;258038
Public Sub ExceltoSQL(ExcelSheetName As String, ExcelPath As String, SqlTablename As String, SqlDatabasename As String)
MainForm.List1.MousePointer = 11
Dim RsExtoSQL As New ADODB.Recordset
Dim CnExtoSql As New ADODB.ConnectionDim xlBook As Excel.Workbook
Set xlApp = CreateObject("Excel.Application") '创建EXCEL对象
Set xlBook = xlApp.Workbooks.Open(ExcelPath) '打开已经存在的EXCEL工件簿文件
xlApp.Worksheets(ExcelSheetName).Activate
Dim i As Integer
Dim MM As Integer
Dim nn As Integer
Dim jj As Integer
Dim Trans As String
i = 1
Dim Fieldname() As Variant
ReDim Fieldname(0) As VariantCnExtoSql.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=chang;PWD=assetok;Initial Catalog=" & SqlDatabasename & ";Data Source=192.168.0.250"
CnExtoSql.Open
Do While xlApp.Cells(1, i).Value <> "" And Not IsNull(xlApp.Cells(1, i).Value)
If Fieldname(UBound(Fieldname)) <> "" Then ReDim Preserve Fieldname(UBound(Fieldname) + 1)
Fieldname(UBound(Fieldname)) = Replace(xlApp.Cells(1, i).Value, "£¨", "(")
Fieldname(UBound(Fieldname)) = Replace(Fieldname(UBound(Fieldname)), "£©", ")")
i = i + 1
Loop
Dim strSQL As String
Dim nno As Integer
For nno = 0 To UBound(Fieldname)
strSQL = strSQL & "[" & Fieldname(nno) & "] text not null,"
Next
On Error Resume Next
CnExtoSql.Execute "drop table [" & SqlTablename & "]"
On Error GoTo 0
CnExtoSql.Execute "create table [" & SqlTablename & "] ( " & Left(strSQL, Len(strSQL) - 1) & ")"RsExtoSQL.Open "select * from " & SqlTablename, CnExtoSql, adOpenDynamic, adLockOptimistic
MM = 2 'ÐÐ
nn = 1 'ÁÐ
Do While ExtoAcCheckTable(MM, i - 1)
RsExtoSQL.AddNew
Do While nn <> i
If xlApp.Cells(MM, nn).Value = "" Or IsNull(xlApp.Cells(MM, nn).Value) Then
xlApp.Cells(MM, nn).Value = "?"
End If
Trans = CStr(xlApp.Cells(MM, nn).Value)
If Left(Trans, 1) = "." Then
RsExtoSQL.Fields.Item(nn - 1).Value = "0" & Trans
Else
RsExtoSQL.Fields.Item(nn - 1).Value = Replace(Trans, "£¨", "(")
RsExtoSQL.Fields.Item(nn - 1).Value = Replace(RsExtoSQL.Fields.Item(nn - 1).Value, "£©", ")")
End If
' RsExtoSQL.Fields.Item(nn - 1).Value = xlApp.Cells(MM, nn).Value
nn = nn + 1
Loop
MM = MM + 1
nn = 1
Loop
RsExtoSQL.Update
Set xlApp = Nothing
xlBook.Close (False)
If CnExtoSql.State <> adStateClosed Then CnExtoSql.Close
MainForm.List1.MousePointer = 0
End Sub
Private Function ExtoAcCheckTable(NumberRow As Integer, NumberCol As Integer) As Boolean
Dim ab As Integer, ac As Integer, YesNo As Boolean
YesNo = False
For ab = 0 To 5 '判断后5行是否有值
For ac = 1 To NumberCol
If xlApp.Cells(NumberRow, ac).Value <> "" And Not IsNull(xlApp.Cells(NumberRow, ac).Value) Then
YesNo = True
GoTo Wanle
End If
Next
NumberRow = NumberRow + 1
Next
Wanle:
If YesNo = True Then ExtoAcCheckTable = True
If YesNo = False Then ExtoAcCheckTable = False
End Function
haohaohappy(爱要冒险,工作要拼) 首先多謝兩位指點,我一個朋友用delphi做了一個程序他可以對任意文件保存至MS SQL的數據庫中同時讀取文件進只系統用相關聯的程序就可以打開,不知VB中是否也可以呢?
怎么还问啊?
Leftie(左手,为人民币服务)的方法适用于任意文件,楼主看过了吗?
只要这两句修改为相应的文件就可以了:
.LoadFromFile App.Path + "\test.jpg"
.SaveToFile App.Path & "\test1.jpg"