从数据库读图片

sunny906 2008-12-20 05:09:34
各位大虾,请看下列代码:
string str = "Data Source=(local);database=student;user id= ;password= ";
string sql = "select pic from picc";
byte[] bytes = null;
SqlConnection con = new SqlConnection(str);
con.Open();

SqlDataAdapter sda=new SqlDataAdapter(sql,con);
DataSet ds = new DataSet();
sda.Fill(ds);

int n = ds.Tables[0].Rows.Count;
for (int i = 0; i < n; i++)
{
bytes=(byte[])ds.Tables[0].Rows[i][0]; //这条语句有没有问题?
}

MemoryStream ms = new MemoryStream(bytes);
Bitmap bm = new Bitmap(ms); //这条语句老报错,提示:“参数无效” pictureBox1.Image = bm;

另:怎么读出数据库里的图片并显示在listview上?代码该如何写?向各位大虾请教,如解决,另送50分,不够再加!!
...全文
111 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
tommir3 2008-12-21
  • 打赏
  • 举报
回复
是啊,我以前也在这犯的错误,@photo写成"@photo"
结果就是错误,因为存进去的不是二进制了,
成字符串了
sunny906 2008-12-21
  • 打赏
  • 举报
回复
明白了
string sql = "insert into picc values('"+bytes+"')"; net5i说得对这条语句有问题

net5i,谢谢你!你太尽心了!
也谢谢大家的帮忙
net5i 2008-12-20
  • 打赏
  • 举报
回复
搂主这一句:
string sql = "insert into picc values('"+bytes+"')";
应该不能插入图片到数据库成功的,必须作为二进制数据参数传入带参数的SQL语句
sunny906 2008-12-20
  • 打赏
  • 举报
回复
存图片:string str = "Data Source=(local);database=student;user id= ;password= ";

SqlConnection con=new SqlConnection(str);
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{

string file = openFileDialog1.FileName;
textBox1.Text = file;
FileStream fs = new FileStream(file,FileMode.Open);
byte []bytes=new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
bytes = br.ReadBytes(Convert.ToInt32(fs.Length));
string sql = "insert into picc values('"+bytes+"')";
con.Open();
SqlCommand cmd = new SqlCommand(sql,con);
cmd.ExecuteNonQuery();
con.Close();
con.Dispose();

读图片:
string str = "Data Source=(local);database=student;user id= ;password= ";
string sql = "select pic from picc";
byte[] bytes = null;
SqlConnection con = new SqlConnection(str);
con.Open();

SqlDataAdapter sda=new SqlDataAdapter(sql,con);
DataSet ds = new DataSet();
sda.Fill(ds);

int n = ds.Tables[0].Rows.Count;
for (int i = 0; i < n; i++)
{
bytes=(byte[])ds.Tables[0].Rows[i][0];
}

MemoryStream ms = new MemoryStream(bytes);
Bitmap bm = new Bitmap(ms); //不管ms对象放哪都提示:“参数无效” pictureBox1.Image = bm;


猿敲月下码 2008-12-20
  • 打赏
  • 举报
回复
如果图片字段类型是Image的 那么存进去的时候应该是图片类型应该是Binary 用存储过程应该是SqlDbType.Binary 大小设置大一点10000好了
net5i 2008-12-20
  • 打赏
  • 举报
回复
贴一下代码算了,搂主就不用去下载我那个链接了:

图片保存代码:
FileStream fs = new FileStream("E:\\a.png", FileMode.Open);
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, (int)fs.Length);
fs.Close();

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=****;Database=****;User ID=****;Pwd=****";
conn.Open();
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.CommandText = "insert into photo_t values(@photo)";

SqlParameter param = new SqlParameter();
param.ParameterName = "photo";
param.SqlDbType = SqlDbType.Image;
param.Value = bytes;
com.Parameters.Add(param);

com.ExecuteNonQuery();
conn.Close();


读取代码:
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Server=****;Database=****;User ID=****;Pwd=****";
conn.Open();
SqlCommand com = new SqlCommand();
com.Connection = conn;
com.CommandText = "select photo from photo_t where id=1";

object obj = com.ExecuteScalar();

conn.Close();

byte[] bytes = obj as byte[];
MemoryStream ms = new MemoryStream();
ms.Write(bytes,0,bytes.Length);
Image img = Image.FromStream(ms);
ms.Close();
this.pictureBox1.Image = img;
猿敲月下码 2008-12-20
  • 打赏
  • 举报
回复
把你保存的图片的代码发下 还有数据库里面的图片字段类型应该是Image
net5i 2008-12-20
  • 打赏
  • 举报
回复
这是因为搂主的图像保存时,就没有保存对,也就是说保存的并不是图像的字节数据,所以读取时肯定有问题
搂主按照3楼说的代码链接做,还不能成功?
sunny906 2008-12-20
  • 打赏
  • 举报
回复
关键是ms这个对象,不管作为谁的参数,都会提示:“参数无效”
怎么会这样呢?
sunny906 2008-12-20
  • 打赏
  • 举报
回复
to:thc1987
试过了,还是报错:“参数无效”
是不是我少加了什么
猿敲月下码 2008-12-20
  • 打赏
  • 举报
回复
有问题
    for (int i = 0; i < n; i++) 
{
bytes=(byte[])ds.Tables[0].Rows[i][0]; //这条语句有没有问题?
}


你这里是遍历取出好多图片 要显示只能显示一张才对

bytes=(byte[])ds.Tables[0].Rows[0][0];//先取出一张试试看

把最后两条语句合并成一条: pictureBox1.Image = Image.FromStream(ms);
koukoujiayi 2008-12-20
  • 打赏
  • 举报
回复
我用DataList读图片
<asp:DataList ID="DataList1" runat="server" DataKeyField="id" DataSourceID="ObjectDataSource1">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" Height="60px" ImageUrl='<%# Eval("id","DisplayPhoto.aspx?ID={0}") %>'
Width="50px" />
</ItemTemplate>
</asp:DataList>

DisplayPhoto.aspx文件代码:
protected void Page_Load(object sender, EventArgs e)
{
int Id = Convert.ToInt32(Request.QueryString["ID"]);
SqlConnection myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbaseConnectionString"].ConnectionString);
myConn.Open();
string sqlStr = "SELECT imagePhoto from yourTable WHERE id = " + Id;
SqlCommand myComm = new SqlCommand(sqlStr, myConn);
SqlDataReader myReader = myComm.ExecuteReader();
if (myReader.Read())
{
byte[] myB = (byte[])myReader["imagePhoto"];
Response.ContentType = "image/jpg";
Response.BinaryWrite(myB);
}
}
ProjectDD 2008-12-20
  • 打赏
  • 举报
回复
可以读,我读过

用varbinary 或 image 数据类型

读的时候,用System.Drawing.ImageConverter类做类型转换,具体情况,我记得不太清楚,你在查查System.Drawing中的类
net5i 2008-12-20
  • 打赏
  • 举报
回复
关于在数据库中保存和读取文档、图像等操作,搂主可以参考我的一个代码资源:
http://download.csdn.net/source/873865

里面是一个代码片断,演示的是把Word文档保存到SQL Server中,搂主可以改成图片
sunny906 2008-12-20
  • 打赏
  • 举报
回复
Bitmap bm = new Bitmap(ms,true); 试过了,还是那个错
MemoryStream ms = new MemoryStream(bytes,true); 错误依旧
LovingAlison 2008-12-20
  • 打赏
  • 举报
回复
Bitmap bm = new Bitmap(ms,true);

111,094

社区成员

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

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

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