sqldatasource控件处理image类型数据

今天做程序的时候,因为要用SqlDatasource绑定存储过程来实现添加数据。这个控件用过不下100遍了,今天才发现Image类型字段需要特别处理。

SqlDataSource控件如果是通过向导来做的话,会自动生成如下代码:
  <asp:SqlDataSource ID= "studentDataSource" runat= "server" ConnectionString= "<%$ ConnectionStrings:StdITBaseOAConnectionString %>"
    SelectCommand= "SELECT Student.* FROM Student" InsertCommand= "spStudentRecordAdd" 
                InsertCommandType= "StoredProcedure" >
    <InsertParameters>
                        <asp:Parameter Name= "StudentName" Type= "String" />
                        <asp:Parameter Name= "StudentID" Type= "Int32" />
                        <asp:Parameter Name= "Photo" Type= "Object" />
                </InsertParameters>
  </asp:SqlDataSource>
运行时会出现“sql_variant类型不能转换为Image类型的异常”,一看就是类型不匹配的异常,可惜的是Parameter 没有Image类型。这时,请手工去掉这行:
<asp:Parameter Name= "Photo" Type= "Object" />
在此,这个Type="Object"将会产生一个sql_variant类型的参数。然而,该sql_variants类 型不能用来存储图像或varbinary(MAX)数据类型,因为该sql_variant的内在数据大小不能超过8,000个字节。(如果你试图使用 Type="Object",然后试图保存超过8,000字节大小的二进制数据,那么,系统将抛出一个异常并显示消息"Parameter '@ImageData' exceeds the size limit for the sql_variant datatype";如果你试图添加不到8,000字节大小的二进制数据,那么,该异常将显示消息"Implicit conversion from data type sql_variant to varbinary(max) is not allowed")。

当然,我们肯定不能不管这个字段,下面是我试验成功的一种做法,仅供有同样疑惑的同学参考:
InBlock.gif         protected  void studentDataSource_Inserting( object sender, SqlDataSourceCommandEventArgs e)
InBlock.gif        {
InBlock.gif                FileUpload fu = FormView1.FindControl( "FileUpload1"as FileUpload;
InBlock.gif                 byte[] imageData =  new  byte[0];
InBlock.gif                 if (fu !=  null && fu.HasFile)
InBlock.gif                {
InBlock.gif                        imageData = fu.FileBytes;
InBlock.gif                }
InBlock.gif                System.Data.SqlClient.SqlParameter uploadData =  new System.Data.SqlClient.SqlParameter( "@Photo", System.Data.SqlDbType.Image);
InBlock.gif                uploadData.Value = imageData;
InBlock.gif                e.Command.Parameters.Add(uploadData);
InBlock.gif        }
代码中使用了FormView控件,同学组要根据实际情况来理解。这段代码里最核心的部分是构建一个SqlParameter,对应 Photo,并加到Insert Command中。

另外上面的
studentDataSource_Inserting方法是SqlDataSource控件的Inserting事件的实现。一开始我用FormView的Inserting事件,发现那里面无法得到Command对象,只能对参数的DefaultValue进行赋值。所以才想到了SqlDataSource

问题虽然解决了,但是我还是建议不要将图片保存到数据库字段中。另外,可能还有其它的方法吧,欢迎各位讨论。









本文转自 王杰瑞 51CTO博客,原文链接:http://blog.51cto.com/wangjierui/109595,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值