如何将一个EXCEL文件作为二进制文件存入数据库,再把它读取打开?
来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 20:59 点击:次
下面的代码把数据库中的image类型字段还原为excel文件。
数据库用的是sql server自带的pubs
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mstream As ADODB.Stream
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=sa;" _
& "Initial Catalog=pubs;Data Source=MyServer"
cn.OpenSet rs = New ADODB.Recordset
rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.Write rs.Fields("logo").Value
mstream.SaveToFile "c:publogo.xls", adSaveCreateOverWrite
rs.Close
cn.Close
下面的代码把一Excel保存到数据库中Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=sa;" _
& "Initial Catalog=pubs;Data Source=MyServer"
Set rs = New ADODB.Recordset
rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic
rs.AddNew
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.LoadFromFile txtEXcelPath
rs.Fields("pub_id") = "……" 在pubs库中,这个字段是主键,需要修改一个有效值
rs.Fields("logo").Value = mstream.Read
rs.Update
rs.Close
cn.Close
该代码来自MSDN
数据库用的是sql server自带的pubs
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim mstream As ADODB.Stream
Set cn = New ADODB.Connection
cn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=sa;" _
& "Initial Catalog=pubs;Data Source=MyServer"
cn.OpenSet rs = New ADODB.Recordset
rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.Write rs.Fields("logo").Value
mstream.SaveToFile "c:publogo.xls", adSaveCreateOverWrite
rs.Close
cn.Close
下面的代码把一Excel保存到数据库中Set cn = New ADODB.Connection
cn.Open "Provider=SQLOLEDB.1;Persist Security Info=True;User ID=sa;Password=sa;" _
& "Initial Catalog=pubs;Data Source=MyServer"
Set rs = New ADODB.Recordset
rs.Open "Select * from pub_info", cn, adOpenKeyset, adLockOptimistic
rs.AddNew
Set mstream = New ADODB.Stream
mstream.Type = adTypeBinary
mstream.Open
mstream.LoadFromFile txtEXcelPath
rs.Fields("pub_id") = "……" 在pubs库中,这个字段是主键,需要修改一个有效值
rs.Fields("logo").Value = mstream.Read
rs.Update
rs.Close
cn.Close
该代码来自MSDN
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>