2023年6月21日发(作者:)
在这里我整理一下关于如何把图片数据(或是其他文件的数据)如何存入数据库,并且实现从数据库读取现实在页面上。(这里用图片作为例子,实现图片的存入、读取然后在显示在一个网页指定位置)
/////////////////////////////////////////////////
插入图片的必要条件
在我们开始上传之前,有两件重要的事我们需要做:
#Form 标记的 enctype 属性应该设置成 enctype= "multipart/form-data "
#需要一个 表单来使用户选择他们要上传的文件,同时我们需要导入 名称空间来处理流对象
同时我们需要对SqlServer做以下的准备:
在SQL Server中建立一个图片存储的数库表,Imgdata Column为图象二进制数据储存字段,imgtypeColumn为图象文件类型记录字段,结构如下:
CREATE TABLE [dbo].[ImageStore]
(
[ImageID] [int] IDENTITY (1, 1) NOT NULL ,
[Imgdata] [image] NULL ,
[imgtype] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
///////////////////////////////////////////////////////
///存入数据库
///////////////////////////////////////////////////////
using System;
using ;
using tions;
using entModel;
using ;
using ent;
using g;
using ;
using nState;
using ;
using trols;
using ntrols;
namespace HNANetAdmin
{
///
/// 图片插入数据库。
/// Author: zhou_min
/// Date: 2002-10-24
///
public class AddImage :
{ protected Button1;
protected putFile File1;
private void Page_Load(object sender, rgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 Web 窗体设计器所必需的。
//
InitializeComponent();
(e);
}
private void InitializeComponent()
{
+= new andler(1_Click);
+= new andler(_Load);
}
#endregion
private void Button1_Click(object sender, rgs e)
{
Stream imgdatastream = tream;//读取数据流
int imgDataLen = tLength;//图片长度
string imgType = tType;//图片类型
byte[] imgData = new byte[imgDataLen];//临时存放图片二进制数组
(imgData,0,imgDataLen);//图片二进制放入临时数组
//调用函数把图片二进制文件插入数据库
InsertImgData(imgType,imgData);
}
private void InsertImgData(string imgType,byte[] imgData)
{
//数据库连接
string connstr= "Data Source=10.2.10.34;database=pubs;uid=sa;pwd=sa; ";
SqlConnection connection = new SqlConnection(connstr); //插入语句
SqlCommand command = new SqlCommand( "INSERT INTO ImageStore(imgtype,imgdata)VALUES ( @imgtype,@imgdata ) ", connection );
//插入参数
SqlParameter paramData = new SqlParameter( "@imgdata
", );
= imgData;
( paramData );
SqlParameter paramType =
", r,50 );
= imgType;
( paramType );
//打开连接
();
//执行插入
eNonQuery();
//关闭连接
();
}
}
}
///////////////////////////////////////////////////////
///取出图片
//////////////////////////////////////////////////////
using System;
using tions;
using entModel;
using ;
using ent;
using g;
using ;
using nState;
using ;
using trols;
using ntrols;
namespace HNANetAdmin
{
///
/// 显示数据库中图片。
///Author: zhou_min
new SqlParameter( "@imgtype
/// Date: 2002-10-24
///
public class DisplayImg :
{
private void Page_Load(object sender, rgs e)
{
if(!Back)
Display();
}
private void Display()
{
string imgid =tring[ "imgid "];//显示图片Id
//数据库连接
string connstr= "Data Source=10.2.10.34;database=pubs;uid=sa;pwd=sa; ";
string sql=
"SELECT imgdata, imgtype FROM ImageStore WHERE id = '
"+ imgid + " ' ";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql, connection);
();
SqlDataReader dr = eReader();
//显示图片
if(())
{
tType = dr[ "imgtype "].ToString();
Write( (byte[]) dr[ "imgdata "] );
}
//关闭连接
();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 Web 窗体设计器所必需的。
//
InitializeComponent();
(e);
} ///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
+= new andler(_Load);
}
#endregion
}
}
////////////////////////////////////////////////
////在一个页面指定显示位置
////////////////////////////////////////////////
从数据库取出来的图片在实现了显示,现在我所做的工作就是要绑定图片到DataList上如下程序:
<%# (em, "cncCode ")%> ' />
可是我运行时提示“Compiler Error Message: CS1010: Newline in constant”
请问一下会在哪里出问题呢,有什么解决方法呢?
--------------------------------------------------------------------
后台的代码如下:
DataRow drHuman = [ "dtRetu "].Rows[0];
drHuman[ "cncCode "] = drHuman[ "cncCode "].ToString().Trim();
urce = drHuman
2023年6月21日发(作者:)
在这里我整理一下关于如何把图片数据(或是其他文件的数据)如何存入数据库,并且实现从数据库读取现实在页面上。(这里用图片作为例子,实现图片的存入、读取然后在显示在一个网页指定位置)
/////////////////////////////////////////////////
插入图片的必要条件
在我们开始上传之前,有两件重要的事我们需要做:
#Form 标记的 enctype 属性应该设置成 enctype= "multipart/form-data "
#需要一个 表单来使用户选择他们要上传的文件,同时我们需要导入 名称空间来处理流对象
同时我们需要对SqlServer做以下的准备:
在SQL Server中建立一个图片存储的数库表,Imgdata Column为图象二进制数据储存字段,imgtypeColumn为图象文件类型记录字段,结构如下:
CREATE TABLE [dbo].[ImageStore]
(
[ImageID] [int] IDENTITY (1, 1) NOT NULL ,
[Imgdata] [image] NULL ,
[imgtype] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL ,
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
///////////////////////////////////////////////////////
///存入数据库
///////////////////////////////////////////////////////
using System;
using ;
using tions;
using entModel;
using ;
using ent;
using g;
using ;
using nState;
using ;
using trols;
using ntrols;
namespace HNANetAdmin
{
///
/// 图片插入数据库。
/// Author: zhou_min
/// Date: 2002-10-24
///
public class AddImage :
{ protected Button1;
protected putFile File1;
private void Page_Load(object sender, rgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 Web 窗体设计器所必需的。
//
InitializeComponent();
(e);
}
private void InitializeComponent()
{
+= new andler(1_Click);
+= new andler(_Load);
}
#endregion
private void Button1_Click(object sender, rgs e)
{
Stream imgdatastream = tream;//读取数据流
int imgDataLen = tLength;//图片长度
string imgType = tType;//图片类型
byte[] imgData = new byte[imgDataLen];//临时存放图片二进制数组
(imgData,0,imgDataLen);//图片二进制放入临时数组
//调用函数把图片二进制文件插入数据库
InsertImgData(imgType,imgData);
}
private void InsertImgData(string imgType,byte[] imgData)
{
//数据库连接
string connstr= "Data Source=10.2.10.34;database=pubs;uid=sa;pwd=sa; ";
SqlConnection connection = new SqlConnection(connstr); //插入语句
SqlCommand command = new SqlCommand( "INSERT INTO ImageStore(imgtype,imgdata)VALUES ( @imgtype,@imgdata ) ", connection );
//插入参数
SqlParameter paramData = new SqlParameter( "@imgdata
", );
= imgData;
( paramData );
SqlParameter paramType =
", r,50 );
= imgType;
( paramType );
//打开连接
();
//执行插入
eNonQuery();
//关闭连接
();
}
}
}
///////////////////////////////////////////////////////
///取出图片
//////////////////////////////////////////////////////
using System;
using tions;
using entModel;
using ;
using ent;
using g;
using ;
using nState;
using ;
using trols;
using ntrols;
namespace HNANetAdmin
{
///
/// 显示数据库中图片。
///Author: zhou_min
new SqlParameter( "@imgtype
/// Date: 2002-10-24
///
public class DisplayImg :
{
private void Page_Load(object sender, rgs e)
{
if(!Back)
Display();
}
private void Display()
{
string imgid =tring[ "imgid "];//显示图片Id
//数据库连接
string connstr= "Data Source=10.2.10.34;database=pubs;uid=sa;pwd=sa; ";
string sql=
"SELECT imgdata, imgtype FROM ImageStore WHERE id = '
"+ imgid + " ' ";
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand(sql, connection);
();
SqlDataReader dr = eReader();
//显示图片
if(())
{
tType = dr[ "imgtype "].ToString();
Write( (byte[]) dr[ "imgdata "] );
}
//关闭连接
();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 Web 窗体设计器所必需的。
//
InitializeComponent();
(e);
} ///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
///
private void InitializeComponent()
{
+= new andler(_Load);
}
#endregion
}
}
////////////////////////////////////////////////
////在一个页面指定显示位置
////////////////////////////////////////////////
从数据库取出来的图片在实现了显示,现在我所做的工作就是要绑定图片到DataList上如下程序:
<%# (em, "cncCode ")%> ' />
可是我运行时提示“Compiler Error Message: CS1010: Newline in constant”
请问一下会在哪里出问题呢,有什么解决方法呢?
--------------------------------------------------------------------
后台的代码如下:
DataRow drHuman = [ "dtRetu "].Rows[0];
drHuman[ "cncCode "] = drHuman[ "cncCode "].ToString().Trim();
urce = drHuman
发布评论