在SQL SERVER 中怎么插入照片?
来源:互联网 发布:狼人杀 守卫 知乎 编辑:程序博客网 时间:2024/05/01 01:39
用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
SQL Server提供一个叫做image的特殊数据类型供我们保存binary data。Binary data可以是图片、文档等。在这篇文章中我们将看到如何在SQL Server中保存和输出图片。
建表
为了试验这个例子你需要一个含有数据的table(你可以在现在的库中创建它,也可以创建一个新的数据库),下面是它的结构:
Column Name
Datatype
Purpose
ID
Integer
identity column Primary key
IMGTITLE
Varchar(50)
Stores some user friendly title to identity the image
IMGTYPE
Varchar(50)
Stores image content type. This will be same as recognized content types of ASP.NET
IMGDATA
Image
Stores actual image or binary data.
保存images进SQL Server数据库
为了保存图片到table你首先得从客户端上传它们到你的web服务器。你可以创建一个web form,用TextBox得到图片的标题,用HTML File Server Control得到图片文件。确信你设定了Form的encType属性为multipart/form-data。
Stream imgdatastream = File1.PostedFile.InputStream;
int imgdatalen = File1.PostedFile.ContentLength;
string imgtype = File1.PostedFile.ContentType;
string imgtitle = TextBox1.Text;
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream.Read(imgdata,0,imgdatalen);
string connstr=
((NameValueCollection)Context.GetConfig
("appSettings"))["connstr"];
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand
("INSERT INTO ImageStore(imgtitle,imgtype,imgdata)
VALUES ( @imgtitle, @imgtype,@imgdata )", connection );
SqlParameter paramTitle = new SqlParameter
("@imgtitle", SqlDbType.VarChar,50 );
paramTitle.Value = imgtitle;
command.Parameters.Add( paramTitle);
SqlParameter paramData = new SqlParameter
( "@imgdata", SqlDbType.Image );
paramData.Value = imgdata;
command.Parameters.Add( paramData );
SqlParameter paramType = new SqlParameter
( "@imgtype", SqlDbType.VarChar,50 );
paramType.Value = imgtype;
command.Parameters.Add( paramType );
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close();
从数据库中输出图片
现在让我们从数据库中取出我们刚刚保存的图片,在这儿,我们将直接将图片输出至浏览器。你也可以将它保存为一个文件或做任何你想做的。
private void Page_Load(object sender, System.EventArgs e)
{
string imgid =Request.QueryString["imgid"];
string connstr=((NameValueCollection)
Context.GetConfig("appSettings"))["connstr"];
string sql="SELECT imgdata, imgtype FROM ImageStore WHERE id = "
+ imgid;
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql, connection);
connection.Open();
SqlDataReader dr = command.ExecuteReader();
if(dr.Read())
{
Response.ContentType = dr["imgtype"].ToString();
Response.BinaryWrite( (byte[]) dr["imgdata"] );
}
connection.Close();
}
在上面的代码中我们使用了一个已经打开的数据库,通过datareader选择images。接着用Response.BinaryWrite代替Response.Write来显示image文件。
希望您喜欢这些文章,如有任何意见和建议请致信webmaster@bipinjoshi.com。
- 在SQL SERVER 中怎么插入照片?
- 如何在SQL Server 中存取照片
- 怎么在sql语句中插入单引号
- 在SQL Server中插入®特殊字符
- 在SQL Server中连接Oracle,完成查询、插入操作
- 怎么在ArcSDE中使用SQL Server分区表
- 在sql server 2000中存储照片和文本的两种方法。
- 在SQL Server中SQL语句的Insert支持一次插入多条记录?
- 在网页中怎么插入天气预报?????急急
- 怎么在网站中插入天气预报
- 在.net中怎么插入视频
- 怎么在Word中插入歌曲
- 在android编程中怎么插入背景图片
- 怎么在MySQL中插入大量记录
- 怎么在ZBrush中插入多网格
- 怎么在iMindMap中插入浮动文本
- SQL Server中如何批量插入记录
- SQL Server中如何批量插入记录
- GPMC 组策略的大管家
- cp命令
- 如何快速上手Netbeans的GUI Form Designer
- Varchar与char的区别
- 生成SHTML代码
- 在SQL SERVER 中怎么插入照片?
- Win32 & .Net Q&A 200509
- Java正则表达式详解
- 球胡麻差
- UML模型的基本概念
- python的数据库接口
- Gracias a la vida
- BitTorrent 官方协议(翻译)
- Bittorrent Protocol Specification v1.0