高分!!如何将access中的ole子段中保存的图片显示到网页上的image控件中???
解决方案 »
- 查询问题
- 向腾讯微博add_pic_t提交图片和文本如何实现啊?
- C# 如何在网页上做一个类似银行或支付宝之类的安全登录控件,可以访问到客户端的U盘
- FileUpload上传限制疑问
- 【GridView和ObjectDataSource绑定出现重复第一条记录】
- 一段相当郁闷的JS了 求解 谢谢
- form验证与cookie共享
- 谁有办法把MSDN提供的URLRewriter里面的RewriterConfig这个节点,移到外面单独变成一个文件,而不要放在Web.Config里
- 求sql字符串替换语句
- 【请教版主】WebBrowser控件有Referer属性吗?如何做一个指向?
- 分布式web应用程序的问题
- ASP.NET在如何调用客户端的exe程序?
public class Main : System.Web.UI.Page {
protected System.Web.UI.HtmlControls.HtmlInputFile UP_FILE;protected System.Web.UI.WebControls.TextBox txtDescription;protected System.Web.UI.WebControls.Label txtMessage;protected System.Int32 FileLength = 0;
protected void Button_Submit(System.Object sender, System.EventArgs e) {System.Web.HttpPostedFile UpFile = UP_FILE.PostedFile;FileLength = UpFile.ContentLength;
try {
if (FileLength == 0) {
txtMessage.Text = "<b>*请选择上传的文件</b>";
} else {
System.Byte[] FileByteArray = new System.Byte[FileLength];System.IO.Stream StreamObject = UpFile.InputStream;StreamObject.Read(FileByteArray,0,FileLength);System.Data.OleDb.OleDbConnection Con = new System.Data.OleDb.OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" +"Integrated Security=SSPI;Initial Catalog=northwind");System.String SqlCmd = "INSERT INTO Images (Image, ContentType, ImageDescription, ByteSize) VALUES (?, ?, ?, ?)";System.Data.OleDb.OleDbCommand OleDbCmdObj = new System.Data.OleDb.OleDbCommand(SqlCmd, Con);OleDbCmdObj.Parameters.Add("@Image", System.Data.OleDb.OleDbType.Binary, FileLength).Value = FileByteArray;OleDbCmdObj.Parameters.Add("@ContentType", System.Data.OleDb.OleDbType.VarChar,50).Value = UpFile.ContentType;OleDbCmdObj.Parameters.Add("@ImageDescription", System.Data.OleDb.OleDbType.VarChar,100).Value = txtDescription.Text;OleDbCmdObj.Parameters.Add("@ByteSize", System.Data.OleDb.OleDbType.VarChar,100).Value = UpFile.ContentLength;Con.Open();OleDbCmdObj.ExecuteNonQuery();Con.Close();txtMessage.Text = "<p><b>* 图片上传成功!</b>";
}
} catch (System.Exception ex) {
txtMessage.Text = ex.Message.ToString();
}}}}
以上代码中,首先建立了一个到SQL Server的数据库链接;然后,建立了一个数据插入的OleDbCommand,注意一点,在建立这个OleDbCommand的时候,我们这里没有使用“@”标志而是使用“?”来代替输入变量。另外,对于SQL.NET而言,因为其提供了一个SqlDbType.Image的数据类型,该类型对数据限制大小为:2,147,483,647,相信绝大部分用户不会上传这样大的文件,因此,可以直接使用以下语句:MySqlCmd.Parameters.Add("@Image", SqlDbType.Image).Value = FileByteArray而在OleDb.NET中,是没有这样类型定义的,我们只能使用OleDbType.Binary类型,这样,对于上传的限制就是8000字节,如果上传文件大于该限制,就会出错,因此,在使用OleDb.NET的时候,我们就必须很清楚的设置上传文件的大小,代码如下:OleDbCmdObj.Parameters.Add("@Image", System.Data.OleDb.OleDbType.Binary, FileLength).Value = FileByteArray