System.Web.HttpPostedFile UpFiles = this.UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFiles.ContentLength ;
Byte[] FileArr = new byte[FileLength];
Stream StreamObject = UpFiles.InputStream; //建立数据流对像 //读取图象文件数据,FileArr为数据储存体,0为数据指针位置、FileLength为数据长度
StreamObject.Read(FileArr,0,FileLength); //建立数据库连接
System.Data.SqlClient.SqlConnection Conn = new SqlConnection
("Data Source=servername;Initial Catalog=test;User ID=sa;Pwd=;");
string StrSql = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
System.Data.SqlClient.SqlCommand Cmd = new SqlCommand(StrSql,Conn);
Cmd.Parameters.Add("@Image",SqlDbType.Binary,FileLength).Value = FileArr;
Cmd.Parameters.Add("@ContentType",SqlDbType.VarChar,50).Value = UpFiles.ContentType ;
Cmd.Parameters.Add("@ImageDescription",SqlDbType.VarChar,200).Value = this.txtDescription.Text ;
Cmd.Parameters.Add("@ImageSize",SqlDbType.BigInt,8).Value = UpFiles.ContentLength ;
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
FileLength = UpFiles.ContentLength ;
Byte[] FileArr = new byte[FileLength];
Stream StreamObject = UpFiles.InputStream; //建立数据流对像 //读取图象文件数据,FileArr为数据储存体,0为数据指针位置、FileLength为数据长度
StreamObject.Read(FileArr,0,FileLength); //建立数据库连接
System.Data.SqlClient.SqlConnection Conn = new SqlConnection
("Data Source=servername;Initial Catalog=test;User ID=sa;Pwd=;");
string StrSql = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
System.Data.SqlClient.SqlCommand Cmd = new SqlCommand(StrSql,Conn);
Cmd.Parameters.Add("@Image",SqlDbType.Binary,FileLength).Value = FileArr;
Cmd.Parameters.Add("@ContentType",SqlDbType.VarChar,50).Value = UpFiles.ContentType ;
Cmd.Parameters.Add("@ImageDescription",SqlDbType.VarChar,200).Value = this.txtDescription.Text ;
Cmd.Parameters.Add("@ImageSize",SqlDbType.BigInt,8).Value = UpFiles.ContentLength ;
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
如能帮组,不胜感激!!高分!
http://weiw.com/article/list.asp?id=4
多附件上传到数据库(例)
http://weiw.com/article/list.asp?id=3
你们可能想错了,我的意思是用 file fild 按扭,实现先打开电脑上的一个文件————》
保存到sql数据库中————》在另一个页面中显示
能否给出你完整的代码?
System.Web.HttpPostedFile UpFiles = this.UP_FILE.PostedFile;
中的UP_FILE就是file fild 控件
是不是还需要建立一个 button 按牛啊!!
以下是我完整的代码?
可是为什么老是出现错误啊!!
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;namespace zcltk
{
/// <summary>
/// WebForm2 的摘要说明。
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputFile file1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面 } #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.HttpPostedFile UpFiles = this.file1.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
int FileLength = UpFiles.ContentLength ;
Byte[] FileArr = new byte[FileLength];
Stream StreamObject = UpFiles.InputStream; //建立数据流对像 //读取图象文件数据,FileArr为数据储存体,0为数据指针位置、FileLength为数据长度
StreamObject.Read(FileArr,0,FileLength); //建立数据库连接
System.Data.SqlClient.SqlConnection Conn = new SqlConnection
("Data Source=192.188.170.56;Initial Catalog=电力局系统;User ID=sa;Pwd=sa");
string StrSql = "INSERT INTO ImageStore (ImageData, ImageContentType, ImageDescription, ImageSize) VALUES (@Image, @ContentType, @ImageDescription, @ImageSize)";
System.Data.SqlClient.SqlCommand Cmd = new SqlCommand(StrSql,Conn);
Cmd.Parameters.Add("@Image",SqlDbType.Binary,FileLength).Value = FileArr;
Cmd.Parameters.Add("@ContentType",SqlDbType.VarChar,50).Value = UpFiles.ContentType ;
Cmd.Parameters.Add("@ImageDescription",SqlDbType.VarChar,200).Value =
//this.txtDescription.Text ;
Cmd.Parameters.Add("@ImageSize",SqlDbType.BigInt,8).Value = UpFiles.ContentLength ;
Conn.Open();
Cmd.ExecuteNonQuery();
Conn.Close();
}
}
}
错误信息:
“/zcltk”应用程序中的服务器错误。
--------------------------------------------------------------------------------分析器错误
说明: 在分析向此请求提供服务所需资源时出错。请检查下列特定分析错误详细信息并适当地修改源文件。 分析器错误信息: 未能加载类型“zcltk.Global”。源错误:
行 1: <%@ Application Codebehind="Global.asax.cs" Inherits="zcltk.Global" %> 源文件: e:\inetpub\wwwroot\zcltk\global.asax 行: 1
--------------------------------------------------------------------------------
我的天啊!!
怎么了
!!
帮帮我啊!!
大哥:
现在我可以把他存到数据库里面了如何取出来啊!!
谢谢你的支持!解决完后,马上给你分!!
下面是错误信息:“/zcltk”应用程序中的服务器错误。
--------------------------------------------------------------------------------将截断字符串或二进制数据。语句已终止。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 将截断字符串或二进制数据。语句已终止。源错误:
行 81:
行 82: Conn.Open();
行 83: Cmd.ExecuteNonQuery();
行 84: Conn.Close();
行 85:
源文件: e:\inetpub\wwwroot\zcltk\webform2.aspx.cs 行: 83 堆栈跟踪:
[SqlException: 将截断字符串或二进制数据。
语句已终止。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +643
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +206
zcltk.WebForm2.Button1_Click(Object sender, EventArgs e) in e:\inetpub\wwwroot\zcltk\webform2.aspx.cs:83
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() +1263
大哥这条语句是什么意思啊!!
他是不是文件的内容啊!!我按照你的说法做了,该了字段的长度,成功了!!
dragonsnake(dragon)
大哥:我按照你的方法做了,现在我在做显示。有一个问题:
选种的文件在原页面上显示,我乡弹出另外一个窗体显示,该怎么办!!
下面市我的代码:System.Data.SqlClient.SqlConnection Conn = new SqlConnection
("Data Source=;Initial Catalog=电力局系统;User ID=sa;Pwd=sa");
string StrSql = "select ImageData, ImageContentType, ImageDescription, ImageSize from ImageStore where ID="+2+"";
System.Data.SqlClient.SqlCommand Cmd = new SqlCommand(StrSql,Conn);
Conn.Open();
SqlCommand MyCommand = new SqlCommand (StrSql, Conn);
SqlDataReader dr =MyCommand.ExecuteReader();
if(dr.Read())
{
Response.ContentType = (dr["ImageContentType"].ToString());
Response.BinaryWrite((byte[])dr["ImageData"]);
}
Conn.Close();
}