从SQLSERVER数据库中读数据出来是怎么做的呢?谢谢

htys3 2005-03-30 09:36:32
protected System.Web.UI.WebControls.Image myimg;

SqlCommand cmdGetContent = new SqlCommand("SELECT 图片 FROM 图片表 where 编号 = '" + bh + "' order by 编号 DESC",myConnection);
if (cmdGetContent.ExecuteScalar()!=null)
{
dadGetContent.SelectCommand = cmdGetContent;
dadGetContent.Fill(dstGetContent, "content");
dr6=dstGetContent.Tables["content"].Rows[0];
myimg.??????????????=dr5["图片"]
}
...全文
179 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
htys3 2005-04-01
  • 打赏
  • 举报
回复
ArmyGirl()我是从其它库里头倒到SQLSERVER中的,数据格式是IMAGE格式的,图片格式也限于JPG的,谢谢的回答哦
ArmyGirl 2005-04-01
  • 打赏
  • 举报
回复
我的问题也搞定啦,存任何格式的图片都没有问题的,你还有什么问题吗?搞定了吧。
ArmyGirl 2005-03-31
  • 打赏
  • 举报
回复
然后把你的IMAGE控件绑定到该页面上就可以啦。

和你讨论一下,你把图片(任何格式?)存进数据库里了吗?存进去以后是不是字段中还是<Binary>?还是变成别的啦?
我目前也在搞这个啊,存储没问题,可是image字段始终是<Binary>,读出的时候是DBNULL,很郁闷啊
ArmyGirl 2005-03-31
  • 打赏
  • 举报
回复
哈哈 你是想在web控件上直接显示image吧,据目前我看到的各种资料说不行啊。
具体实现方法只有再建一个.aspx页面专门读出byte[]的二进制数据流
实现方法如下
一,上传并存入SqlServer
数据库结构
create table test
{
id identity(1,1),
FImage image
}
相关的存储过程
Create proc UpdateImage
(
@UpdateImage Image
)
As
Insert Into test(FImage) values(@UpdateImage)
GO

在UpPhoto.aspx文件中添加如下:
<input id="UpPhoto" name="UpPhoto" runat="server" type="file">
<asp:Button id="btnAdd" name="btnAdd" runat="server" Text="上传"></asp:Button>

然后在后置代码文件UpPhoto.aspx.cs添加btnAdd按钮的单击事件处理代码:
private void btnAdd_Click(object sender, System.EventArgs e)
{
//获得图象并把图象转换为byte[]
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);
cmd.CommandType=CommandType.StoredProcedure;

cmd.Parameters.Add("@UpdateImage",SqlDbType.Image);
cmd.Parameters["@UpdateImage"].Value=PhotoArray;

//如果你希望不使用存储过程来添加图片把上面四句代码改为:
//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();
}

二,从SqlServer中读取并显示出来
在需要显示图片的地方添加如下代码:
<asp:image id="imgPhoto" runat="server" ImageUrl="ShowPhoto.aspx"></asp:image>

ShowPhoto.aspx主体代码:
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
SqlConnection conn=new SqlConnection()
conn.ConnectionString="Data Source=localhost;Database=test;User Id=sa;Pwd=sa";

string strSql="select * from test where id=2";//这里假设获取id为2的图片
SqlCommand cmd=new SqlCommand()
reader.Read();
Response.ContentType="application/octet-stream";
Response.BinaryWrite((Byte[])reader["FImage"]);
Response.End();
reader.Close();
}
}

zhangguochen 2005-03-31
  • 打赏
  • 举报
回复
UP
htys3 2005-03-31
  • 打赏
  • 举报
回复
UP
htys3 2005-03-30
  • 打赏
  • 举报
回复
SqlCommand cmdGetContent = new SqlCommand("SELECT 图片 FROM 图片表 where 编号 = '" + bh + "' order by 编号 DESC",myConnection);

SqlDataAdapter da = new SqlDataAdapter(cmdGetContent);
DataSet ds = new DataSet();
da.Fill(ds, "BLOBTest");
int c = ds.Tables["BLOBTest"].Rows.Count;
if(c>0)
{ //BLOB is read into Byte array, then used to construct MemoryStream,
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])(ds.Tables["BLOBTest"].Rows[c - 1]["BLOBData"]);
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
System.Drawing.Image image1 =Image.FromStream(stmBLOBData);
Label1.Size = new Size(image1.Width, image1.Height);
Label1.Image = image1;
}
中间的Label1是这样定义的吗,怎么没见着.Image 和.Size 属性呢,急啊~~~~~~~~~~~~~~~~~高手帮帮忙呢
protected System.Web.UI.WebControls.Label Label1;

htys3 2005-03-30
  • 打赏
  • 举报
回复
读类型为image的数据,谢谢

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧