Data Access - Read and Write Images from a Databasehttp://download.microsoft.com/download/6/4/7/6474467e-b2b7-40ea-a478-1d3296e78adf/CSharp.msi上面是很有名的101例子中的一个示例
using System; using System.Text; using System.IO; using System.Data; using System.Data.SqlClient;namespace SQLbuf { /// <summary> /// Class1 的摘要说明。 /// </summary> class Class1 { //bpublic System.Data.SqlClient.SqlDataReader myDataReader; /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main(string[] args) { // // TODO: 在此处添加代码以启动应用程序 // try { Stream file; file = File.OpenRead(".\\jj.jpg"); BinaryReader sr = new BinaryReader(file);
int n = (int)file.Length; //Console.Write(sr.ReadBytes(n).ToString()); byte[] buf = new byte[n]; buf = sr.ReadBytes(n); sr.Close(); string connstr = "server=rd-yb;database=wwzy;uid=sa;pwd=mps"; SqlConnection connection = new SqlConnection(connstr); string strSQL = "insert into nn(body)values(@buf)"; SqlCommand command = new SqlCommand(strSQL,connection); buf=@buf;
using System.Text;
using System.IO;
using System.Data;
using System.Data.SqlClient;namespace SQLbuf
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Class1
{
//bpublic System.Data.SqlClient.SqlDataReader myDataReader;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
try
{
Stream file;
file = File.OpenRead(".\\jj.jpg");
BinaryReader sr = new BinaryReader(file);
int n = (int)file.Length;
//Console.Write(sr.ReadBytes(n).ToString());
byte[] buf = new byte[n];
buf = sr.ReadBytes(n);
sr.Close(); string connstr = "server=rd-yb;database=wwzy;uid=sa;pwd=mps";
SqlConnection connection = new SqlConnection(connstr); string strSQL = "insert into nn(body)values(@buf)";
SqlCommand command = new SqlCommand(strSQL,connection); buf=@buf;
SqlParameter paramData = new SqlParameter( "@buf", SqlDbType.Image );
paramData.Value = buf;
command.Parameters.Add( paramData );
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
//connection.Close();
Console.Write("OK"); string sql="SELECT * FROM nn" ;
command.CommandText = sql;
command.Connection = connection; SqlDataReader myDataReader = command.ExecuteReader();
if(myDataReader.Read() == true)
{
Stream file1;
file1 = File.OpenWrite(".\\jjd.jpg");
byte[] mydata=((byte[])myDataReader["body"]); Console.Write(mydata.ToString());
BinaryWriter sw = new BinaryWriter(file1);
sw.Write(mydata);
sw.Close(); }
Console.ReadLine(); }
catch(System.Exception e)
{
Console.Write(e.Message);
}
}
}
}
//将图片保存到数据库中
if(this.picPhoto.Image==null)
{
m_DataRow[MyTools.g_PhotoField]=DBNull.Value;
}
else
{
try
{
MemoryStream ms = new MemoryStream ();
picPhoto.Image.Save (ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte [] myData = new Byte [ms.Length ];
ms.Position = 0;
ms.Read (myData,0,Convert.ToInt32 (ms.Length ));
m_DataRow[MyTools.g_PhotoField] = myData; }
catch(System.Exception ee)
{
MessageBox.Show(ee.Message);
}
}//else//读取图象
if(this.m_DataRow[MyTools.g_PhotoField]!=DBNull.Value)
{
try
{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])m_DataRow[MyTools.g_PhotoField];
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
this.picPhoto.Image= Image.FromStream(stmBLOBData);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
this.picPhoto.Image= null;
}