文件插入IMAGE字段:首先将文件转换成二进制流,然后再Insert
private byte[] FileToByte(string filePath)
{//将文档转换成二进制流
//创建字节空间
byte[] ib = new Byte[60000];
//获取上传的图片的路径
string strfilepath = filePath;
//转换成文件流
FileStream fs = new FileStream(strfilepath, FileMode.Open, FileAccess.Read);
//将文件内容读进字节数组
fs.Read(ib, 0, 60000);
return ib;
}
private byte[] FileToByte(string filePath)
{//将文档转换成二进制流
//创建字节空间
byte[] ib = new Byte[60000];
//获取上传的图片的路径
string strfilepath = filePath;
//转换成文件流
FileStream fs = new FileStream(strfilepath, FileMode.Open, FileAccess.Read);
//将文件内容读进字节数组
fs.Read(ib, 0, 60000);
return ib;
}
解决方案 »
- C#自制组件,怎么添加事件?
- 为何方法也可以有属性呢?比如 i.GetType().Name,这种写法,GetType()是方法,但是Name是属性呀?
- 探讨探讨 petshop4.0 !!!
- 如何获取一个容器中同一类型控件的集合?
- 刷新树,滚动条向下跑了,怎么让它保持原状态,以便在刚才编辑的节点处,再继续进行操作!
- [求助]PropertyGrid在IE中出现奇怪的问题!
- 请教问题!
- 如何取得datagrid中textbox的值?
- [在线]如果用C#将一个大图片缩小并保存?(asp.net)
- 请教C#高手,使用C#是否可以实现对象的移动(A主机—>B主机),来者有分。
- 如何将EXCEl表导入到C#应用?(就是在C#中打印EXCEL表)
- 正则表达式
把图片转成byte[],然后用
insert into 表名(Image字段名) values (@Image)
这样的方法,把byte[]作为参数就可以新增。修改也一样查询:(byte[])datareader["ImageField"]得到数据,然后转成Image显示。
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "图像文件 (*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string fullpath = openFileDialog1.FileName;//文件路径
FileStream fs = new FileStream(fullpath,FileMode.Open);
Byte[] imagebytes=new byte[fs.Length];
BinaryReader br = new BinaryReader(fs);
imagebytes = br.ReadBytes(Convert.ToInt32(fs.Length));
SqlConnection sqlconnection = new SqlConnection(ConfigurationManager.ConnectionStrings["Football_Vs.Properties.Settings.footballConnectionString"].ConnectionString);
sqlconnection.Open();
SqlCommand com = new SqlCommand("update TeamImages set teamimage=@ImageList where teamname='" + TeamCombo.Text + "'", sqlconnection);
com.Parameters.Add("ImageList",SqlDbType.Image);
com.Parameters["ImageList"].Value = imagebytes;
com.ExecuteNonQuery();
MessageBox.Show("上传成功");
sqlconnection.Close();
displayPhoto();
}
} private void displayPhoto()//显示图片
{
byte[] imagebytes = null;
DBOperate dbop = new DBOperate();
string selectStr = "select teamimage from teamimages where teamname='" + TeamCombo.Text + "'";
SqlDataReader dr = dbop.displayTeam(selectStr);
dr.Read();
if (dr.HasRows)
{
switch (dr[0].ToString())
{
case "":
pictureBox1.Image = null;
break;
default:
imagebytes = (byte[])dr.GetValue(0);
dr.Close();
MemoryStream ms = new MemoryStream(imagebytes);
Bitmap bmpt = new Bitmap(ms);
pictureBox1.Image = bmpt;
break;
}
}
else
{
pictureBox1.Image = null;
}
dr.Close();
}