使用asp代码导出,数据库中的二进制图片导出为本地JPG图片
此方法可以把以二进制存储在数据库中的图片通过ADODB.Stream以图片格式保存到电脑硬盘中,非常实用且精典,值得收藏!
<%
db="img.mdb"
Set conn = Server.CreateObject("ADODB.Connection")
connstr="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(""&db&"")
conn.Open connstr
set rs=server.createobject("ADODB.recordset")
sql="select * from img"
rs.open sql,conn,1,1
do while not rs.eof
call SaveToFile(rs("pic"),rs("id")&".gif")
rs.movenext
loop
rs.close
set rs=nothing
set conn=nothing
Sub SaveToFile(ByVal strBody,ByVal Filename)
'***********************
'存储内容到文件
'by 天涯才 QQ:23969
'[email protected]
'2005-08-11
'winXP+IIS5.5测试通过
'***********************
Dim objStream
Set objStream = Server.CreateObject("ADODB.Stream")
If Err.Number=-2147221005 Then
Response.Write "<div align='center'>非常遗憾,您的主机不支持ADODB.Stream,不能使用本程序</div>"
Err.Clear
Response.End
End If
With objStream
.Type = 2
.Open
.Position = objStream.Size
.WriteText = strBody
.SaveToFile Server.MapPath(Filename),2
.Close
End With
Set objStream = Nothing
End Sub
%>
注意:后来用PS打不开,报错。而且用IE也不识别。
解决的办法:用ACDSee 打开,选中图片右键=>转换=>在框内选择JPG格式=>确定即可!
② SQL数据库 二进制图片如何导出成文件
SQL数据库 二进制图片如何导出成文件
1.将图片以二进制存入数据库
//保存图片到数据库
protected void Button1_Click(object sender, EventArgs e)
{
//图片路径
string strPath = "~/photo/03.JPG";
string strPhotoPath = Server.MapPath(strPath);
//读取图片
FileStream fs = new System.IO.FileStream(strPhotoPath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
//存入
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
string strComm = " INSERT INTO personPhoto(personName, personPhotoPath, personPhoto) ";
strComm += " VALUES('wangwu', '" + strPath + "', @photoBinary )";
SqlCommand myComm = new SqlCommand(strComm, myConn);
myComm.Parameters.Add("@photoBinary", SqlDbType.Binary,photo.Length);
myComm.Parameters["@photoBinary"].Value = photo;
myConn.Open();
myComm.ExecuteNonQuery();
myConn.Close();
}
2.读取二进制图片在页面显示
//读取图片
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
string strComm = " SELECT personPhoto FROM personPhoto WHERE personName='wangwu' ";
SqlCommand myComm = new SqlCommand(strComm, myConn);
myConn.Open();
SqlDataReader dr = myComm.ExecuteReader();
while (dr.Read())
{
byte[] photo = (byte[])dr["personPhoto"];
this.Response.BinaryWrite(photo);
}
dr.Close();
myConn.Close();
或
SqlConnection myConn = new SqlConnection("Data Source=127.0.0.1;Initial Catalog=TestDB;User ID=sa;Password=sa");
SqlDataAdapter myda = new SqlDataAdapter(" SELECT personPhoto FROM personPhoto WHERE personName='11' ", myConn);
DataSet myds = new DataSet();
myConn.Open();
myda.Fill(myds);
myConn.Close();
byte[] photo = (byte[])myds.Tables[0].Rows[0]["personPhoto"];
this.Response.BinaryWrite(photo);
3.设置Image控件显示从数据库中读出的二进制图片
③ 怎样把SQL数据库里的图片导出到指定的文件夹
首先可以还原或附加这个数据库的备份,然后就找到存放图片的字段读出来,
通过。net、java等语言编程的程序读出这个字段的数据保存到文件夹就OK了.
④ 如何从SQL中导出照片
在MS SQL SERVER 安装目录下有个可执行文件叫 TEXTCOPY.EXE
可对 MS SQL SERVER 中的文本或图像数据进行输入输出.
不过你可以在MS-DOS方式下执行text /? 得到它的描述。
下面是这个工具的描述:
Copies a single text or image value into or out of SQL Server. The val
ue
is a specified text or image 'column' of a single row (specified by th
e
"where clause") of the specified 'table'.
If the direction is IN (/I) then the data from the specified 'file' is
copied into SQL Server, replacing the existing text or image value. If
the
direction is OUT (/O) then the text or image value is copied from
SQL Server into the specified 'file', replacing any existing file.
TEXTCOPY [/S ][sqlserver]] [/U [login]] [/P ][password]]
[/D ][database]] [/T table] [/C column] [/W"where clause"]
[/F file] [{/I | /O}] [/K chunksize] [/Z] [/?]
/S sqlserver The SQL Server to connect to. If 'sqlserver' is n
ot
specified, the local SQL Server is used.
/U login The login to connect with. If 'login' is not spec
ified,
a trusted connection will be used.
/P password The password for 'login'. If 'password' is not
specified, a NULL password will be used.
/D database The database that contains the table with the tex
t or
image data. If 'database' is not specified, the d
efault
database of 'login' is used.
/T table The table that contains the text or image value.
/C column The text or image column of 'table'.
/W "where clause" A complete where clause (including the WHERE keyw
ord)
that specifies a single row of 'table'.
/F file The file name.
/I Copy text or image value into SQL Server from 'fi
le'.
/O Copy text or image value out of SQL Server into '
file'.
/K chunksize Size of the data transfer buffer in bytes. Minimu
m
value is 1024 bytes, default value is 4096 bytes.
/Z Display debug information while running.
/? Display this usage information and exit.
You will be prompted for any required options you did not specify.
为此, 可写一个存储过程,调用这个命令
CREATE PROCEDURE sp_text (
@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 =
'text /S ' + @srvname +
' /U ' + @login +
' /P ' + @password +
' /D ' + @dbname +
' /T ' + @tbname +
' /C ' + @colname +
' /W "' + @whereclause +
'" /F ' + @filename +
' /' + @direction
EXEC master..xp_cmdshell @exec_str
下面是一个拷贝图像到SQL Server的pubs数据库的例子, 表名pub_info, 字段名
logo,图像文件名picture.bmp,保存到pub_id='0736'记录 sp_text @srvn
ame = 'ServerName',
@login = 'Login',
@password = 'Password',
@dbname = 'pubs',
@tbname = 'pub_info',
@colname = 'logo',
@filename = 'c:\picture.bmp',
@whereclause = " WHERE pub_id='0736' ",
@direction = 'I'
你看看 调用成拷贝就行
⑤ 怎么样批量导出sql数据库中的图片
打开数据库SQL server ,右击数据库选择“任务” “生成脚本”
选择你要导出的数据库,点击下一步
将编写数据脚本选项选择为true,点击下一步
选择表,点击下一步
选择你要导出表的数据
选择将脚本保存到“新建查询”窗口,点击下一步,
点击完成,之后就不要操作,直到生成脚本成功后,点击关闭按钮。
只要在其他数据库中直接运行,同样的一张表就在另一个数据库中生成了。