设为首页 收藏本站
查看: 772|回复: 0

[经验分享] SQL Server 中图片和文件的存取(C#.NET)

[复制链接]

尚未签到

发表于 2016-11-5 00:08:50 | 显示全部楼层 |阅读模式
读取文件夹,将文件直接保存到数据库中
前台文件
DSC0000.gif DSC0001.gif
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->1<% DSC0002.gif @PageLanguage="C#"AutoEventWireup="true"CodeFile="main.aspx.cs"Inherits="CommPage_main"%>
2 DSC0003.gif
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{
20 DSC0004.gif STSharedST=newSTShared();
21
22protectedvoidPage_Load(objectsender,EventArgse)
23 DSC0005.gif DSC0006.gif {
24
25 DSC0007.gif }
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 DSC0008.gif }
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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-295699-1-1.html 上篇帖子: 关于SQL Server事务日志的问题汇总 下篇帖子: 把图片保存到sql server数据库里
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表