请问在C#中如何向数据库插入图片?
如题!数据库就用sql2005!!在数据库中图片的存储形式好像是以二进制的形式存储的,存进去后,看不出图的样子?现在我想例如在.NET中,用一个控件把一个图片存进数据库,然后在用另一个控件再把图片读出来显示!
请问该怎么做?是不是要用到特殊控件??
------解决方案--------------------//写
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
Add( "Test ", "2.jpg ");
}
public static void Add(string categoryName, string filePath)
{
// byte [] photo = GetPhoto(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte [] photo = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
SqlConnection cn = new SqlConnection( "Data Source = (local);Integrated Security = SSPI;Initial Catalog=Northwind ");
SqlCommand cmd = new SqlCommand( "INSERT INTO Categories(CategoryName, Picture) VALUES (@CategoryName, @Picture) ", cn);
cmd.Parameters.Add( "@CategoryName ", SqlDbType.NVarChar, 15).Value = categoryName;
cmd.Parameters.Add( "@Picture ", SqlDbType.Image, photo.Length).Value = photo;
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
}