转为byte类型后存入Image字段。 byte[] imagebytes=null; FileStream fs=new FileStream(Image_path,FileMode.Open); BinaryReader br=new BinaryReader(fs); imagebytes=br.ReadBytes(br.Length); SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image); parInput22.Direction=ParameterDirection.Input; cmd.Parameters["@员工图片"].Value=imagebytes; cmd.ExecuteNonQuery(); 数据库中操作图片 How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158 DataGrid显示图片(物理路径式和Stream流式)和添加图片到数据库 http://singlepine.cnblogs.com/articles/288027.html
向数据库里写图片文件 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(); } public static byte [] GetPhoto(string 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(); return photo; } }
再从数据库里读出来并重命名using System; using System.IO; using System.Data; using System.Data.SqlClient;class BLOBDemo { [STAThread] static void Main(string[] args) { SqlConnection cn = new SqlConnection("Data Source = (local);Integrated Security = SSPI;Initial Catalog=Northwind"); SqlCommand cmd = new SqlCommand("Select CategoryID,Picture FROM Categories", cn); FileStream fs; BinaryWriter bw; //缓冲区大小 const int bufferSize = 100; byte [] outByte = new byte[bufferSize]; //GetBytes返回的字节数量 long retval; //BLOB输出的起始位置 long startIndex = 0; int CategoryID = 1; cn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess); while(dr.Read()) { CategoryID = dr.GetInt32(0); fs = new FileStream("Test" + CategoryID + ".bmp", FileMode.OpenOrCreate, FileAccess.Write); bw = new BinaryWriter(fs); startIndex = 0;
byte[] imagebytes=null;
FileStream fs=new FileStream(Image_path,FileMode.Open);
BinaryReader br=new BinaryReader(fs);
imagebytes=br.ReadBytes(br.Length);
SqlParameter parInput22=cmd.Parameters.Add("@员工图片",SqlDbType.Image);
parInput22.Direction=ParameterDirection.Input;
cmd.Parameters["@员工图片"].Value=imagebytes;
cmd.ExecuteNonQuery();
数据库中操作图片
How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;EN-US;309158
DataGrid显示图片(物理路径式和Stream流式)和添加图片到数据库
http://singlepine.cnblogs.com/articles/288027.html
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();
} public static byte [] GetPhoto(string 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(); return photo;
}
}
using System.IO;
using System.Data;
using System.Data.SqlClient;class BLOBDemo
{
[STAThread]
static void Main(string[] args)
{
SqlConnection cn = new SqlConnection("Data Source = (local);Integrated Security = SSPI;Initial Catalog=Northwind");
SqlCommand cmd = new SqlCommand("Select CategoryID,Picture FROM Categories", cn); FileStream fs;
BinaryWriter bw; //缓冲区大小
const int bufferSize = 100;
byte [] outByte = new byte[bufferSize];
//GetBytes返回的字节数量
long retval;
//BLOB输出的起始位置
long startIndex = 0; int CategoryID = 1; cn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess); while(dr.Read())
{
CategoryID = dr.GetInt32(0); fs = new FileStream("Test" + CategoryID + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
bw = new BinaryWriter(fs); startIndex = 0;
do
{
retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
// Console.WriteLine(retval.ToString());
bw.Write(outByte);
bw.Flush();
startIndex += bufferSize;
}while(retval == bufferSize); bw.Write(outByte, 0, (int)retval - 1);
bw.Flush(); bw.Close();
fs.Close();
} dr.Close();
cn.Close();
}
}