转为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 把任意类型的文件保存到SQL Server http://dotnet.aspx.cc/ShowDetail.aspx?id=EY1XLDYV-PIDF-43LO-1WFL-FMY5ALE1F635
private System.Windows.Forms.PictureBox pictureBox1;
private System.Data.SqlClient.SqlCommand cmdExecute;
private System.Data.SqlClient.SqlConnection connEmployee;
private Bitmap image;
private byte[] buffer;private void ShowMyImage(string fileToDisplay, int xSize, int ySize)
{
// Sets up an image object to be displayed.
if (image != null)
{
image.Dispose();
} // Stretches the image to fit the pictureBox.
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage ;
image = new Bitmap(fileToDisplay);
pictureBox1.ClientSize = new Size(xSize, ySize);
pictureBox1.Image = (Image) image ;
} private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "请选择照片";
openFileDialog.ReadOnlyChecked = true;
openFileDialog.ShowReadOnly = true;
openFileDialog.Filter = "所有图片文件|*.*|JPEG(*.JPG;*.JPEG;*.JPEe;*.JFIF)|*.JPG;*.JPEG;*.JPEe;*.JFIF|位图文件(*.BMP)|*.BMP|GIF(*.GIF)|*.GIF"; string pictureName = "";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
pictureName = openFileDialog.FileName;
this.ShowMyImage(pictureName, 112, 130);
} if (pictureName != "")
{
FileStream stream = new FileStream(pictureName, FileMode.Open, FileAccess.Read);
buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
stream.Close();
} this.cmdExecute.CommandText = @"INSERT INTO Photo_Test(Photo) values(@Photo)";
this.cmdExecute.Parameters.Add("@Photo", SqlDbType.Image, buffer.Length, "Photo");
this.cmdExecute.Parameters["@Photo"].Value = buffer;
if (this.connEmployee.State == ConnectionState.Closed)
this.connEmployee.Open();
this.cmdExecute.ExecuteNonQuery();
} private void button2_Click(object sender, System.EventArgs e)
{
SqlDataAdapter da = new SqlDataAdapter("Select * from Photo_Test WHERE ID = 20", this.connEmployee);
DataSet ds = new DataSet();
da.Fill(ds);
DataTable dt = ds.Tables[0];
byte[] b = (byte[])dt.Rows[0][1]; if(b.Length > 0)
{
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
image=new Bitmap(stream);
stream.Close();
this.pictureBox1.Image = 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
把任意类型的文件保存到SQL Server
http://dotnet.aspx.cc/ShowDetail.aspx?id=EY1XLDYV-PIDF-43LO-1WFL-FMY5ALE1F635
那样做
可不可以用VS2003的命令提字符来编译?
会不会出错