如何用SQL语句往数据库的表里加图像文件???

SevenChina 2004-04-18 10:37:58
如何用SQL语句往数据库的表里加图像文件???

谢谢了!
...全文
72 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mmqingfeng 2004-04-18
  • 打赏
  • 举报
回复
这是显示图像:
string sql = "SELECT Name, Picture FROM yourTable Where Name='yourName'";

SqlCommand selectCommand = new SqlCommand(sql,_connection);

try
{
sqlConnection.Open();

SqlDataReader reader = selectCommand.ExecuteReader();

if(reader.Read())
{
byte[] data = (byte[])reader.GetSqlBinary(1);

MemoryStream sm = new MemoryStream(data);
Bitmap bmp = new Bitmap(sm);
this.panel.BackgroundImage = bmp;
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);

}
finally
{
_connection.Close();
}
mmqingfeng 2004-04-18
  • 打赏
  • 举报
回复
这是读文件:
FileStream st = File.OpenRead(filePath);
byte[] data= new byte[st.Length];
st.Read(data,0,(int)st.Length-1);


mmqingfeng 2004-04-18
  • 打赏
  • 举报
回复
_updateCommand = new SqlCommand(_sqlUpdate,_connection);
_updateCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar, 10, "Name"));
_updateCommand.Parameters.Add(new SqlParameter("@Picture", SqlDbType.Image,0, "Picture"));



this._updateCommand.Parameters["@Name"].Value = name;
this._updateCommand.Parameters["@Picture"].Value = data;

bool result = true;

try
{
if(_connection.State!= ConnectionState.Open)
{
this._connection.Open();
}
int row = (int)_updateCommand.ExecuteNonQuery();

result = row>0;

}
catch (Exception e)
{

MessageBox.Show(e.Message);
result = false;
}
finally
{
_connection.Close();
}
return result;
CMIC 2004-04-18
  • 打赏
  • 举报
回复
http://www.csdn.net/Develop/Read_Article.asp?Id=12172
SevenChina 2004-04-18
  • 打赏
  • 举报
回复
谢谢了,结贴!

110,533

社区成员

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

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

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