我有一个数据库需要向其中的一个表里的一个 字段写入图片,请问该怎么做?
最好能提供asp.net(使用vb.net)源码和解释,因为我是一个菜鸟!!!
最好能提供asp.net(使用vb.net)源码和解释,因为我是一个菜鸟!!!
解决方案 »
- DiscuzNT 二次開發
- session取值问题
- datagrid超级链接列传参
- 发表文章时如何添加多张图片并存储在数据库中
- 请问:怎样在GridView中最简单有效的实现添加记录的功能?
- 哪位大侠做过活动目录,请进!!!!!!
- 针对国内用户使用paypal合适吗?及其安全问题
- button 奇怪了!非常人所见!!!!!!
- 使用treeview 控件时为什么有的客户端无法显示?
- asp.net怎样刷新页面
- DataGrid的Edit状态中,想通过RadioButtonList的选择值改变同行的其他控件状态,怎么做?
- c++写的dll在asp页面里面可以调用。那把asp页面升级到aspx,请问可以同样调用这个dll吗(弄好了,送个500。。。谢谢你的回复)
http://dotnet.aspx.cc/ShowDetail.aspx?id=J9UBRVER-L3VB-49M3-GOU1-Z6C2PVR6FZ3K
http://community.csdn.net/Expert/topic/3713/3713493.xml?temp=.1171076
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;namespace AccessUpload
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button but;
protected System.Web.UI.HtmlControls.HtmlInputFile File;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.but.Click += new System.EventHandler(this.but_Click);
this.show.Click += new System.EventHandler(this.show_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void but_Click(object sender, System.EventArgs e)
{
if(File.PostedFile!=null && File.PostedFile.ContentLength!=0)
{
string filename = File.PostedFile.FileName.Substring(File.PostedFile.FileName.LastIndexOf("\\")+1); //文件名称
string filetype = File.PostedFile.ContentType; //文件类型
int filesize = File.PostedFile.ContentLength; //文件大小
byte[] filedata = new byte[filesize]; //文件内容
File.PostedFile.InputStream.Read(filedata,0,filesize);
string conStr = "Provider = Microsoft.Jet.OLEDB.4.0;Data Source =" + Server.MapPath("Data/db1.mdb");
OleDbConnection con = new OleDbConnection(conStr);
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText = "Insert into photo (filename,filetype,filesize,filedata) Values (@filename,@filetype,@filesize ,@filedata )";
cmd.Parameters.Add("@filename",OleDbType.VarChar,50).Value = filename;
cmd.Parameters.Add("@filetype",OleDbType.VarChar,50).Value = filetype;
cmd.Parameters.Add("@filesize",OleDbType.BigInt,8).Value = filesize;
cmd.Parameters.Add("@filedata",OleDbType.Binary,filesize).Value = filedata;
con.Open();
try
{
cmd.ExecuteNonQuery();
}
catch(OleDbException exp){Response.Write(exp.Message);}
con.Close();
//上传成功
}
else
{
Response.Write("请选择文件");
} } }
}
HttpPostedFile upPhoto=UpPhoto.PostedFile;
int upPhotoLength=upPhoto.ContentLength;
byte[] PhotoArray=new Byte[upPhotoLength];
Stream PhotoStream=upPhoto.InputStream;
PhotoStream.Read(PhotoArray,0,upPhotoLength); //连接数据库
SqlConnection conn=new SqlConnection();
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa"; SqlCommand cmd=new SqlCommand("UpdateImage",conn);
string strSql="Insert into test(FImage) values(@FImage)";
SqlCommand cmd=new SqlCommand(strSql,conn);
cmd.Parameters.Add("@FImage",SqlDbType.Image);
cmd.Parameters["@FImage"].Value=PhotoArray; conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
http://dotnet.aspx.cc/ShowDetail.aspx?id=2A5DD7C6-A45A-48AB-A2E8-342A29F17506
http://www.cnblogs.com/rippleyong/archive/2004/08/10/31977.aspx
--------------------------------------------------------------------------------字段太小而不能接受所要添加的数据的数量。试着插入或粘贴较少的数据。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.OleDb.OleDbException: 字段太小而不能接受所要添加的数据的数量。试着插入或粘贴较少的数据。源错误:
行 52: cmd.Parameters(0).Value = bindata
行 53: conn.Open()
行 54: cmd.ExecuteNonQuery()
行 55: conn.Close()
行 56:
源文件: F:\Inetpub\wwwroot\WebApplication1\SaveImage.aspx.vb 行: 54 堆栈跟踪:
[OleDbException (0x80040e57): 字段太小而不能接受所要添加的数据的数量。试着插入或粘贴较少的数据。]
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr) +43
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) +174
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) +92
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) +65
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) +112
System.Data.OleDb.OleDbCommand.ExecuteNonQuery() +67
WebApplication1.SaveImage.Button1_Click(Object sender, EventArgs e) in F:\Inetpub\wwwroot\WebApplication1\SaveImage.aspx.vb:54
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1277
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573