微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sqlserver 存储图片

 

 1

/**//// <summary>
 2

/// 上传图片
 3

/// </summary>

 4

private void UploadFile()
 5

{
 6

    
/**////得到用户上传文件
 7

    string strFilePathName = loFile.PostedFile.FileName;
 8

    
string strFileName = Path.GetFileName(strFilePathName);
 9

    
int FileLength = loFile.PostedFile.ContentLength;
10


11

    
if(FileLength<=0)
12

        
return;
13


14

    
/**////上传文件
15

    try
16

    

{
17

        
18

        
/**////图象文件临时储存Byte数组
19

        Byte[] FileByteArray = new Byte[FileLength];
20


21

        
/**////建立数据流对像
22

        Stream StreamObject = loFile.PostedFile.InputStream; 
23


24

        
/**////读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
25

        StreamObject.Read(FileByteArray,0,FileLength); 
26


27

        
/**////建立sql Server链接
28

        string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
29

        sqlConnection Con 
= new sqlConnection(strCon);
30

        String sqlCmd 
= "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
31

        sqlCommand Cmdob
= new sqlCommand(sqlCmd, Con);
32

        Cmdobj.Parameters.Add(
"@Image",sqlDbType.Binary, FileLength).Value = FileByteArray;
33

        Cmdobj.Parameters.Add(
"@ContentType"sqlDbType.VarChar,50).Value = loFile.PostedFile.ContentType; //记录文件类型
34

        
35

        
/**////把其它单表数据记录上传
36

        Cmdobj.Parameters.Add("@ImageDescription",200).Value = tbDescription.Text;
37

        
38

        
/**////记录文件长度,读取时使用
39

        Cmdobj.Parameters.Add("@ImageSize"sqlDbType.BigInt,8).Value = FileLength;
40

        Con.open();
41

        Cmdobj.ExecuteNonQuery(); 
42

        Con.Close();
43


44

        
/**////跳转页
45

        Response.Redirect("ShowAll.aspx");
46

    }

47

    
catch(Exception ex)
48

    

{
49

        
throw ex;
50

    }

51

}

二.从数据库中读取图片

/**/ /// <summary>
 2

/// 显示图片
 3

/// </summary>

 4

private   void  ShowImages()
 5

{
 6

    
/**////ID为图片ID
 7

    int ImgID = Convert.ToInt32(Request.QueryString["ID"]);  
 8

    
 9

    
/**////建立数据库连接
10

    string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
11

    sqlConnection Con 
= new sqlConnection(strCon);
12

    String sqlCmd 
= "SELECT * FROM ImageStore WHERE ImageID = @ImageID";
13

    sqlCommand Cmdob
= new sqlCommand(sqlCmd, Con);
14

    Cmdobj.Parameters.Add(
"@ImageID"sqlDbType.Int).Value = ImgID;
15

    
16

    Con.open();
17

    sqlDataReader sqlReader 
= Cmdobj.ExecuteReader();
18

    sqlReader.Read(); 
19


20

    
/**////设定输出文件类型
21

    Response.ContentType = (string)sqlReader["ImageContentType"];
22

    
23

    
/**////输出图象文件二进制数制
24

    Response.OutputStream.Write((byte[])sqlReader["ImageData"], 0, (int)sqlReader["ImageSize"]); 
25

    Response.End();
26


27

    Con.Close();    
28

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐