SQL2000图片如何存储及读取

Freeedom 2008-07-12 03:22:49
请问,SQL2000图片如何存储及读取啊?
我前台用的是Visual Studio 2005.
...全文
710 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
suchabbs 2008-07-14
  • 打赏
  • 举报
回复
mark
Freeedom 2008-07-12
  • 打赏
  • 举报
回复
谢谢各位了!先结帖!不懂再开帖问!
hery2002 2008-07-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Freeedom 的回复:]
我在网上搜索到,说有两种方法,一种是保存为二进制数据,一种是保存图片路径.
能稍微解释下这两种方法么?哪种方法更快?
[/Quote]
一种是保存为二进制数据
-- 这种方式以二进制流方式保存到数据库中,文件的读取和保存直接来源于数据库,
-- 在读取和保存时相对较慢,因为中间需要和二进制流进行相互转换.
-- 数据库文件相对较大,存取数据量较大,存储比较占数据库空间.
-- 但是这种方式比较安全,便于管理和维护,
-- 对于图片的操作,如复制,转移等都很方便和快捷.
一种是保存图片路径.
-- 存取数据量小,读取速度较快,
-- 但是这种实体和文件分离的方式,服务器端还的有文件夹来保存这些图片(上传),
-- 所以一旦文件夹不小心丢失,所有图片就找不回了.

例子上面已经给了很多了,
这里例一个C#的,参考 :)
.NET在SQL Server中的图片存取技术
http://www.cnblogs.com/stewen/archive/2005/12/20/300587.html
tianhuo_soft 2008-07-12
  • 打赏
  • 举报
回复
SQL版是CSDN最难强分的地方

强人太多了 ~!
tianhuo_soft 2008-07-12
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 Freeedom 的回复:]
我在网上搜索到,说有两种方法,一种是保存为二进制数据,一种是保存图片路径.
能稍微解释下这两种方法么?哪种方法更快?
[/Quote]

我觉得二进制数据更好一些

其实这个要看客户端是什么模式 如果是B/S当然是 保存路径了
要是C/S还是二进制比较号
tianhuo_soft 2008-07-12
  • 打赏
  • 举报
回复

SqlCommand.CommandText="select Image from pp";
Connection.Open();
SqlDataReader sdr=SqlCommand.ExecuteReader();
int i=0;
while(sdr.Read())
{
byte[] b=(byte[])sdr[0];
MemoryStream ms=new MemoryStream(b,0,b.Length);
Image ima=Image.FromStream(ms);
ima.Save("D:\\xx\\"+(i++).ToString()+".jpg");
ima.Dispose();
}




var
strmFiles : TFileStream;
bedin
strmFiles := TFilestream.create('C:\temp\003.jpg',fmCreate);
TBlobField(query1.fieldbyname('字段名称')).saveToStream(strmFiles);
strmFiles.free;
end;

使用之前要连接数据库:
其中'C:\temp\003.jpg'为你保存的路径和文件名称
query1是TQuery控件


tianhuo_soft 2008-07-12
  • 打赏
  • 举报
回复

用image类型

方法:
1、建立过程
CREATE PROCEDURE sp_textcopy (
@srvname varchar (30),
@login varchar (30),
@password varchar (30),
@dbname varchar (30),
@tbname varchar (30),
@colname varchar (30),
@filename varchar (30),
@whereclause varchar (40),
@direction char(1))
AS
DECLARE @exec_str varchar (255)
SELECT @exec_str =
'textcopy /S ' + @srvname +
' /U ' + @login +
' /P ' + @password +
' /D ' + @dbname +
' /T ' + @tbname +
' /C ' + @colname +
' /W "' + @whereclause +
'" /F ' + @filename +
' /' + @direction
EXEC master..xp_cmdshell @exec_str

2、建表和初始化数据
create table 表名 (编号 int,image列名 image)
go
insert 表名 values(1,0x) -- 必须的,且不是null
insert 表名 values(2,0x) -- 必须的,且不是null
go

3、读入
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','I' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','I' --注意条件是 编号=2

go

4、读出成文件
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','O' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','O' --注意条件是 编号=2
go

如果报textcopy不是可执行文件的话,你就到
C:\Program Files\Microsoft SQL Server\MSSQL\Binn
目录下拷备 textcopy.exe到:
C:\Program Files\Microsoft SQL Server\80\Tools\Binn


Freeedom 2008-07-12
  • 打赏
  • 举报
回复
我在网上搜索到,说有两种方法,一种是保存为二进制数据,一种是保存图片路径.
能稍微解释下这两种方法么?哪种方法更快?
中国风 2008-07-12
  • 打赏
  • 举报
回复
方法:
1、建立过程
CREATE PROCEDURE sp_textcopy (
@srvname varchar (30),
@login varchar (30),
@password varchar (30),
@dbname varchar (30),
@tbname varchar (30),
@colname varchar (30),
@filename varchar (30),
@whereclause varchar (40),
@direction char(1))
AS
DECLARE @exec_str varchar (255)
SELECT @exec_str =
'textcopy /S ' + @srvname +
' /U ' + @login +
' /P ' + @password +
' /D ' + @dbname +
' /T ' + @tbname +
' /C ' + @colname +
' /W "' + @whereclause +
'" /F ' + @filename +
' /' + @direction
EXEC master..xp_cmdshell @exec_str

2、建表和初始化数据
create table 表名 (编号 int,image列名 image)
go
insert 表名 values(1,0x) -- 必须的,且不是null
insert 表名 values(2,0x) -- 必须的,且不是null
go

3、读入
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','I' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','I' --注意条件是 编号=2

go

4、读出成文件
sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\图片.bmp','where 编号=1','O' --注意条件是 编号=1

sp_textcopy '你的服务器名','sa','你的密码','库名','表名','image列名','c:\bb.doc','where 编号=2','O' --注意条件是 编号=2
go

************如果报textcopy不是可执行文件的话,你就到
C:\Program Files\Microsoft SQL Server\MSSQL\Binn
目录下拷备 textcopy.exe到:
C:\Program Files\Microsoft SQL Server\80\Tools\Binn


liangCK 2008-07-12
  • 打赏
  • 举报
回复
  Dim   con   As   New   SqlConnection   _   
("Server=YourServer;uid=<username>;pwd=<strong 解password>;database=northwind")
Dim da As New SqlDataAdapter _
("Select * From MyImages", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()

da.MissingSchemaAction = MissingSchemaAction.AddWithKey

Dim fs As New FileStream _
("C:\winnt\Gone Fishing.BMP", FileMode.OpenOrCreate, _
FileAccess.Read)
Dim MyData(fs.Length) As Byte
fs.Read(MyData, 0, fs.Length)
fs.Close()
con.Open()
da.Fill(ds, "MyImages")
Dim myRow As DataRow
myRow = ds.Tables("MyImages").NewRow()

myRow("Description") = "This would be description text"
myRow("imgField") = MyData
ds.Tables("MyImages").Rows.Add(myRow)
da.Update(ds, "MyImages")

fs = Nothing
MyCB = Nothing
ds = Nothing
da = Nothing

con.Close()
con = Nothing
MsgBox ("Image saved to database")

Double-click Button2, and then add the following code to the Button2_Click event handler:
Note You must change uid <username> and pwd =<strong password> to the correct values before you run this code. Make sure that User ID has the appropriate permissions to perform this operation on the database.
Dim con As New SqlConnection _
("Server=YourServer;uid=<username>;pwd=<strong password>;database=northwind")
Dim da As New SqlDataAdapter _
("Select * From MyImages", con)
Dim MyCB As SqlCommandBuilder = New SqlCommandBuilder(da)
Dim ds As New DataSet()

con.Open()
da.Fill(ds, "MyImages")
Dim myRow As DataRow
myRow = ds.Tables("MyImages").Rows(0)

Dim MyData() As Byte
MyData = myRow("imgField")
Dim K As Long
K = UBound(MyData)

Dim fs As New FileStream _
("C:\winnt\Gone Fishing2.BMP", FileMode.OpenOrCreate, _
FileAccess.Write)
fs.Write(MyData, 0, K)
fs.Close()

fs = Nothing
MyCB = Nothing
ds = Nothing
da = Nothing

con.Close()
con = Nothing
MsgBox ("Image retrieved")
liangCK 2008-07-12
  • 打赏
  • 举报
回复
微软的答案:

VB6向SQL SERVER存入图象:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;258038

VB.NET向SQL SERVER存入图象:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;308042

C#向SQL SERVER存入图象:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158

34,596

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧