从sql中检索图像 [英] retrive images from sql

查看:70
本文介绍了从sql中检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在sql中存储图像&如何从sql中检索图像?

解决方案

尝试 Google [ ^ ]

以下是谷歌的一些链接

< a href =http://chiragrdarji.wordpress.com/2007/03/05/storing-and-retrieving-image-in-sql-server/> http://chiragrdarji.wordpress.com/2007/03/ 05 / storage-and -ving-image-in-sql-server / [ ^ ]

使用标记过程和C#.net 从SQL Server存储和检索图像[ ^ ]

< a href =http://www.c-sharpcorner.com/uploadfile/e628d9/inserting-retrieving-images-from-sql-server-database-without-using-stored-procedures/> http:// www。 c-sharpcorner.com/uploadfile/e628d9/in serting-retrieval-images-from-sql-server-database-without-using-stored-procedures / [ ^ ]


  CREATE   TABLE  [dbo]。[ImageData] 

[ImageID] [ int ] IDENTITY 1 1 NOT NULL
[ImageData] [ image ] NULL
CONSTRAINT [PK_ImageData] PRIMARY KEY CLUSTERED

[ImageID] ASC

WITH (PAD_INDEX = OFF ,STATISTICS_NORECOMPUTE = OFF ,IGNORE_DUP_KEY = OFF
ALLOW_ROW_LOCKS = ON ,ALLOW_PAGE_LOCKS = ON ON [ PRIMARY ]

ON [ PRIMARY ] TEXTIMAGE_ON [ PRIMARY ]







在这个例子中,我将使用四(4)个存储过程调用ReadAllImage,ReadAllImageIDs,ReadImage,SaveImage并使用下面的SQL脚本来创建这些过程。



  CREATE   proc  [dbo]。[ReadAllImage] 作为 
SELECT * FROM ImageData
GO





  CREATE   proc  [dbo]。[ReadAllImageIDs]  as  
SELECT ImageID FROM ImageData
GO







 创建 < span class =code-keyword> proc  [dbo]。[ReadImage]  @ imgId   int   as  
SELECT ImageData FROM ImageData
WHERE ImageID = @ imgId
GO







CREATE proc [dbo]。[SaveImage] @ img image as
INSERT INTO ImageData(ImageData)
VALUES @ img
GO



< br $>


 FileStream FS =新FileStream(@ fop.FileName,FileMode.Open,FileAccess.Read); 
byte [] img = new byte [FS.Length];
FS.Read(img,0,Convert.ToInt32(FS.Length));

if(con.State == ConnectionState.Closed)
con.Open();
SqlCommand cmd = new SqlCommand(SaveImage,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@ img,SqlDbType.Image).Value = img;
cmd.ExecuteNonQuery();
loadImageIDs();





 SqlConnection con =  new  SqlConnection(DBHandler.GetConnectionString()); 
SqlCommand cmd = new SqlCommand( ReadImage ,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add( @ imgId,SqlDbType.Int).Value =
Convert.ToInt32(cmbImageID.SelectedValue.ToString());
SqlDataAdapter adp = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
尝试
{
if (con.State == ConnectionState .Closed)
con.Open();
adp.Fill(dt);
if (dt.Rows.Count > 0
{
MemoryStream ms = new MemoryStream(( byte [])dt.Rows [ 0 ] [ 的ImageData]);
picImage.Image = Image.FromStream(ms);
picImage.SizeMode = PictureBoxSizeMode.StretchImage;
picImage.Refresh();
}
}
catch (例外情况)
{
MessageBox.Show(ex.Message, 错误
MessageBoxButtons.OK,MessageBoxIcon.Error);
}
最后
{
如果(con .State == ConnectionState.Open)
con.Close();
}


您好,



看看这里:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=129 [ ^ ]


How to store images in sql & How to retrive images from sql?

解决方案

Try Google[^]
Here are a few links from google
http://chiragrdarji.wordpress.com/2007/03/05/storing-and-retrieving-image-in-sql-server/[^]
Storing and Retrieving Images from SQL Server Using Strored Procedures and C#.net[^]
http://www.c-sharpcorner.com/uploadfile/e628d9/inserting-retrieving-images-from-sql-server-database-without-using-stored-procedures/[^]


CREATE TABLE [dbo].[ImageData]
 (
    [ImageID] [int] IDENTITY(1,1) NOT NULL,
    [ImageData] [image] NULL,
 CONSTRAINT [PK_ImageData] PRIMARY KEY CLUSTERED
 (
    [ImageID] ASC
 )
 WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF,
 ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
 )
 ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]




In this example I am going to use Four(4) Stored Procedures call ReadAllImage, ReadAllImageIDs, ReadImage, SaveImage and use below SQL scripts to create those Procedures.

CREATE proc [dbo].[ReadAllImage] as
SELECT * FROM ImageData
GO



CREATE proc [dbo].[ReadAllImageIDs] as
SELECT ImageID FROM ImageData
GO




CREATE proc [dbo].[ReadImage] @imgId int as
SELECT ImageData FROM ImageData
WHERE ImageID=@imgId
GO




CREATE proc [dbo].[SaveImage] @img image as
INSERT INTO ImageData(ImageData)
VALUES (@img)
GO




FileStream FS = new FileStream(@fop.FileName, FileMode.Open, FileAccess.Read);
    byte[] img = new byte[FS.Length];
    FS.Read(img, 0, Convert.ToInt32(FS.Length));

    if (con.State == ConnectionState.Closed)
      con.Open();
    SqlCommand cmd = new SqlCommand("SaveImage", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@img", SqlDbType.Image).Value = img;
    cmd.ExecuteNonQuery();
    loadImageIDs();



SqlConnection con = new SqlConnection(DBHandler.GetConnectionString());
    SqlCommand cmd = new SqlCommand("ReadImage", con);
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.Add("@imgId", SqlDbType.Int).Value = 
              Convert.ToInt32(cmbImageID.SelectedValue.ToString());
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    try
    {
        if (con.State == ConnectionState.Closed)
            con.Open();
        adp.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["ImageData"]);
            picImage.Image = Image.FromStream(ms);
            picImage.SizeMode = PictureBoxSizeMode.StretchImage;
            picImage.Refresh();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", 
              MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        if (con.State == ConnectionState.Open)
            con.Close();
    }


Hi,

Have a look here:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=129[^]


这篇关于从sql中检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆