SQL Server 中图片和文件的存取(C#.NET)

读取文件夹,将文件直接保存到数据库中
前台文件

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="main.aspx.cs"Inherits="CommPage_main"%>
2
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<htmlxmlns="http://www.w3.org/1999/xhtml">
6<headrunat="server">
7<title>无标题页</title>
8</head>
9<body>
10<formid="form1"runat="server">
11<div>
12<tablestyle="border:0;width:100%;text-align:right;">
13<tr>
14<tdstyle="height:26px"></td>
15<tdstyle="height:26px"></td>
16<tdstyle="height:26px"><asp:ButtonID="btnSaveToDataBase"runat="server"Height="24px"OnClick="btnSaveToDataBase_Click"Text="图片保存到数据库中"Width="176px"/></td>
17</tr>
18</table>
19</div>
20
21</form>
22</body>
23</html>
24




CS文件


<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1usingSystem;
2usingSystem.Data;
3usingSystem.Configuration;
4usingSystem.Collections;
5usingSystem.Web;
6usingSystem.Web.Security;
7usingSystem.Web.UI;
8usingSystem.Web.UI.WebControls;
9usingSystem.Web.UI.WebControls.WebParts;
10usingSystem.Web.UI.HtmlControls;
11
12usingSystem.Data.SqlClient;
13usingSystem.IO;
14usingSystem.Drawing;
15usingSystem.Drawing.Imaging;
16usingSuntownBase;
17
18publicpartialclassCommPage_main:System.Web.UI.Page
19{
20STSharedST=newSTShared();
21
22protectedvoidPage_Load(objectsender,EventArgse)
23{
24
25}

26
27protectedvoidbtnSaveToDataBase_Click(objectsender,EventArgse)
28{
29inti=0;//文件计数
30
31string[]dirs=Directory.GetFiles(@"D:\aaa\images");
32if(dirs.Length!=0)
33{
34try
35{
36//Response.Write("共有"+dirs.Length+"个文件!");
37foreach(stringdirindirs)
38{
39//Response.Write("<p>"+dir);
40//如果文件类型不匹配,则重新循环
41if((dir.ToLower()).IndexOf(".jpg")==-1&&(dir.ToLower()).IndexOf(".gif")==-1)
42{
43continue;
44}

45else
46{
47i++;//文件计数加1
48}

49
50//C:\Inetpub\wwwroot\CadFormula\images\item_12.jpg
51
52//FileInfofi=newFileInfo(openFileDialog1.PostedFile.FileName);
53
54FileInfofi=newFileInfo(dir);
55intimgdatalen=(int)fi.Length;
56byte[]imgdata=newbyte[imgdatalen];
57Streamimgdatastream=fi.OpenRead();
58intn=imgdatastream.Read(imgdata,0,imgdatalen);
59stringConnectionString="server=.;database=image;uid=sa;pwd=admin";
60SqlConnectionconn=newSqlConnection(ConnectionString);
61
62stringsql="insertinto[image](picture)values(@imgdata)";
63
64SqlCommandcmd=newSqlCommand(sql,conn);
65
66SqlParameterparamData=newSqlParameter("@imgdata",SqlDbType.Image);
67paramData.Value=imgdata;
68cmd.Parameters.Add(paramData);
69
70conn.Open();
71cmd.ExecuteNonQuery();
72conn.Close();
73}

74}

75catch(Exceptionerr)
76{
77Response.Write("出现错误:"+err.Message.ToString());
78}

79finally
80{
81if(i==0)
82{
83ST.MessageBox(Page,"没有匹配的文件!","确定");
84}

85else
86{
87ST.MessageBox(Page,"成功上传"+i.ToString()+"个文件!","确定");
88}

89//最后删除此文件夹下的文件
90}

91}

92}

93}

94


===============================================
下面是读取文件
前台:

<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="test2.aspx.cs"Inherits="CommPage_test2"%>
2
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<htmlxmlns="http://www.w3.org/1999/xhtml">
6<headrunat="server">
7<title>无标题页</title>
8</head>
9<body>
10<formid="form1"runat="server">
11<div>
12<imgid=ImgSpan1border=0src="testimg/select.jpg"><br>
13<imgid=imgborder=0>
14<br/>
15<inputtype=buttonvalue=顺转90度onclick="document.all.ImgSpan.style.filter='progid:DXImageTransform.Microsoft.BasicImage(Rotation=1)'"id="Button3">
16<inputtype=buttonvalue=顺转180度onclick="document.all.ImgSpan.style.filter='progid:DXImageTransform.Microsoft.BasicImage(Rotation=2)'">
17<inputtype=buttonvalue=逆转90度onclick="document.all.ImgSpan.style.filter='progid:DXImageTransform.Microsoft.BasicImage(Rotation=3)'">
18<inputtype=buttonvalue=恢复角度onclick="document.all.ImgSpan.style.filter='progid:DXImageTransform.Microsoft.BasicImage(Rotation=0)'">
19<br/>
20<br/>
21<br/>
22<asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="图片保存到数据库中"Height="24px"Width="176px"/><br/>
23<br/>
24<asp:ButtonID="Button2"runat="server"OnClick="Button2_Click"Text="显示图片"/><br/>
25<br/>
26<br/>
27</div>
28</form>
29</body>
30</html>
31
32
33<scriptlanguage="javascript"<span
分享到:
评论

你可能感兴趣的:(sql,C++,c,.net,server,C#)