请问:如何利用C#将图片存入数据库并读出来显示? 如题 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 是winform的哪位大虾能够帮个忙.小弟感鸡他 先通过流的方式转变成二进制流,再把byte[]存储 using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Data.SqlClient;using System.IO;namespace InsertImg_Csharp_{ /// <summary> /// Form1 的摘要说明。 /// </summary> public class Form1 : System.Windows.Forms.Form { internal System.Windows.Forms.OpenFileDialog OpenFileDialog1; internal System.Data.SqlClient.SqlCommand SqlCommand1; internal System.Data.SqlClient.SqlConnection SqlConnection1; internal System.Windows.Forms.PictureBox PictureBox1; internal System.Windows.Forms.Button Button5; internal System.Windows.Forms.Button Button4; internal System.Windows.Forms.Button Button3; internal System.Windows.Forms.Button Button2; internal System.Windows.Forms.Button Button1; internal System.Windows.Forms.TextBox TextBox1; internal System.Windows.Forms.Label Label1; internal System.Windows.Forms.Label Label2; private System.Windows.Forms.TextBox txtDelName; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.OpenFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.SqlCommand1 = new System.Data.SqlClient.SqlCommand(); this.SqlConnection1 = new System.Data.SqlClient.SqlConnection(); this.PictureBox1 = new System.Windows.Forms.PictureBox(); this.Button5 = new System.Windows.Forms.Button(); this.Button4 = new System.Windows.Forms.Button(); this.Button3 = new System.Windows.Forms.Button(); this.Button2 = new System.Windows.Forms.Button(); this.Button1 = new System.Windows.Forms.Button(); this.TextBox1 = new System.Windows.Forms.TextBox(); this.Label1 = new System.Windows.Forms.Label(); this.Label2 = new System.Windows.Forms.Label(); this.txtDelName = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // SqlCommand1 // this.SqlCommand1.CommandText = "dbo.[sp_InsertPhoto]"; this.SqlCommand1.CommandType = System.Data.CommandType.StoredProcedure; this.SqlCommand1.Connection = this.SqlConnection1; this.SqlCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, false, ((System.Byte)(10)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null)); this.SqlCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50)); this.SqlCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@image", System.Data.SqlDbType.VarBinary, 2147483647)); // // SqlConnection1 // this.SqlConnection1.ConnectionString = "workstation id=DUAN;packet size=4096;user id=sa;data source=DUAN;persist security" + " info=True;initial catalog=logDB"; // // PictureBox1 // this.PictureBox1.Location = new System.Drawing.Point(145, 62); this.PictureBox1.Name = "PictureBox1"; this.PictureBox1.Size = new System.Drawing.Size(451, 239); this.PictureBox1.TabIndex = 12; this.PictureBox1.TabStop = false; // // Button5 // this.Button5.Location = new System.Drawing.Point(312, 312); this.Button5.Name = "Button5"; this.Button5.Size = new System.Drawing.Size(102, 35); this.Button5.TabIndex = 17; this.Button5.Text = "删除该记录"; this.Button5.Click += new System.EventHandler(this.Button5_Click); // // Button4 // this.Button4.Location = new System.Drawing.Point(568, 308); this.Button4.Name = "Button4"; this.Button4.Size = new System.Drawing.Size(87, 35); this.Button4.TabIndex = 16; this.Button4.Text = "退出"; this.Button4.Click += new System.EventHandler(this.Button4_Click); // // Button3 // this.Button3.Location = new System.Drawing.Point(448, 312); this.Button3.Name = "Button3"; this.Button3.Size = new System.Drawing.Size(95, 35); this.Button3.TabIndex = 15; this.Button3.Text = "显示记录"; this.Button3.Click += new System.EventHandler(this.Button3_Click); // // Button2 // this.Button2.Location = new System.Drawing.Point(8, 312); this.Button2.Name = "Button2"; this.Button2.Size = new System.Drawing.Size(120, 35); this.Button2.TabIndex = 14; this.Button2.Text = "添加到数据库"; this.Button2.Click += new System.EventHandler(this.Button2_Click); // // Button1 // this.Button1.Location = new System.Drawing.Point(397, 24); this.Button1.Name = "Button1"; this.Button1.Size = new System.Drawing.Size(115, 28); this.Button1.TabIndex = 13; this.Button1.Text = "浏览图片…"; this.Button1.Click += new System.EventHandler(this.Button1_Click); // // TextBox1 // this.TextBox1.Location = new System.Drawing.Point(153, 27); this.TextBox1.Name = "TextBox1"; this.TextBox1.Size = new System.Drawing.Size(208, 21); this.TextBox1.TabIndex = 11; this.TextBox1.Text = ""; // // Label1 // this.Label1.Location = new System.Drawing.Point(24, 24); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(96, 26); this.Label1.TabIndex = 9; this.Label1.Text = "姓名:"; // // Label2 // this.Label2.Location = new System.Drawing.Point(24, 80); this.Label2.Name = "Label2"; this.Label2.Size = new System.Drawing.Size(96, 21); this.Label2.TabIndex = 10; this.Label2.Text = "图片"; // // txtDelName // this.txtDelName.Location = new System.Drawing.Point(168, 320); this.txtDelName.Name = "txtDelName"; this.txtDelName.Size = new System.Drawing.Size(128, 21); this.txtDelName.TabIndex = 18; this.txtDelName.Text = ""; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(672, 366); this.Controls.Add(this.txtDelName); this.Controls.Add(this.PictureBox1); this.Controls.Add(this.Button5); this.Controls.Add(this.Button4); this.Controls.Add(this.Button3); this.Controls.Add(this.Button2); this.Controls.Add(this.Button1); this.Controls.Add(this.TextBox1); this.Controls.Add(this.Label1); this.Controls.Add(this.Label2); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Button1_Click(object sender, System.EventArgs e) { OpenFileDialog1.InitialDirectory = @"d:\"; OpenFileDialog1.DefaultExt = "gif"; OpenFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg"; OpenFileDialog1.ShowDialog(); PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName); this.TextBox1.Text=this.OpenFileDialog1.FileName; } private SqlConnection cn; private SqlCommand cmd; private bool InsertIMG(byte[] buffer) { bool bl=true; try { cn=new SqlConnection(SqlConnection1.ConnectionString); cn.Open(); cmd=new SqlCommand("sp_InsertPhoto", cn); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text; cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer; cmd.ExecuteNonQuery(); } catch { bl=false; } finally { cn.Close(); } return bl; } private void showIMG(string s) { cn =new SqlConnection(SqlConnection1.ConnectionString); cn.Open(); string str= "SELECT photo FROM Photos WHERE name='"+ s + "'"; cmd=new SqlCommand(str, cn); byte[] b=(byte[])cmd.ExecuteScalar(); if (b!=null && b.Length > 0) { MemoryStream stream=new MemoryStream(b, true); stream.Write(b, 0, b.Length); DrawToScale(new Bitmap(stream)); stream.Close(); } MessageBox.Show("b==null,数据库中没有该条记录"); cn.Close(); } private void DrawToScale(System.Drawing.Image img) { this.PictureBox1.Image=img; } private void Button4_Click(object sender, System.EventArgs e) { this.Close(); } //删除 private void Button5_Click(object sender, System.EventArgs e) { cn =new SqlConnection(SqlConnection1.ConnectionString); cn.Open(); cmd=new SqlCommand("delete from photos where name='" + this.txtDelName.Text + "'", cn); cmd.ExecuteNonQuery(); MessageBox.Show("Image deleted"); cn.Close(); } //插入 private void Button2_Click(object sender, System.EventArgs e) { FileStream st=new FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read); string s= TextBox1.Text; BinaryReader mbr = new BinaryReader(st); byte[] buffer=new byte[st.Length]; mbr.Read(buffer, 0,(int)st.Length); st.Close(); this.InsertIMG(buffer); } //查询 private void Button3_Click(object sender, System.EventArgs e) { this.showIMG(this.TextBox1.Text); } }} CREATE TABLE Photos ( [name] varchar(50), [photo] image NULL)GOSQL SERVER 中的部分CREATE PROCEDURE sp_InsertPhoto @name AS VARCHAR(200), @image AS IMAGE ASINSERT INTO Photos ([name], [photo])VALUES (@name, @image)GOdrop proc sp_InsertPhoto win7下用PATH类的getfullname()得到的文件路径是C盘某某文件下的路径问题 我用的VS 2008, 为什么在属性栏无法输入中文? #中API去掉标题和边框的问题 急!找不到类型或命名空间名称“DataContractAttribute”? 关与webBrowser选中的问题 数据库建表的问题 水晶报表问题 事件处理怎么一直为NULL?? 请大侠帮忙看看这个问题:DataGrid中,点击单元格选中整行,为什么总是偶尔选中? UP有分+++++++++++++++++++++++ 简单问题 谁有DevExpress XtraGrid这个控件?
哪位大虾能够帮个忙.小弟感鸡他
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;namespace InsertImg_Csharp_
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
internal System.Windows.Forms.OpenFileDialog OpenFileDialog1;
internal System.Data.SqlClient.SqlCommand SqlCommand1;
internal System.Data.SqlClient.SqlConnection SqlConnection1;
internal System.Windows.Forms.PictureBox PictureBox1;
internal System.Windows.Forms.Button Button5;
internal System.Windows.Forms.Button Button4;
internal System.Windows.Forms.Button Button3;
internal System.Windows.Forms.Button Button2;
internal System.Windows.Forms.Button Button1;
internal System.Windows.Forms.TextBox TextBox1;
internal System.Windows.Forms.Label Label1;
internal System.Windows.Forms.Label Label2;
private System.Windows.Forms.TextBox txtDelName;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.OpenFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SqlCommand1 = new System.Data.SqlClient.SqlCommand();
this.SqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.PictureBox1 = new System.Windows.Forms.PictureBox();
this.Button5 = new System.Windows.Forms.Button();
this.Button4 = new System.Windows.Forms.Button();
this.Button3 = new System.Windows.Forms.Button();
this.Button2 = new System.Windows.Forms.Button();
this.Button1 = new System.Windows.Forms.Button();
this.TextBox1 = new System.Windows.Forms.TextBox();
this.Label1 = new System.Windows.Forms.Label();
this.Label2 = new System.Windows.Forms.Label();
this.txtDelName = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// SqlCommand1
//
this.SqlCommand1.CommandText = "dbo.[sp_InsertPhoto]";
this.SqlCommand1.CommandType = System.Data.CommandType.StoredProcedure;
this.SqlCommand1.Connection = this.SqlConnection1;
this.SqlCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, false, ((System.Byte)(10)), ((System.Byte)(0)), "", System.Data.DataRowVersion.Current, null));
this.SqlCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 50));
this.SqlCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@image", System.Data.SqlDbType.VarBinary, 2147483647));
//
// SqlConnection1
//
this.SqlConnection1.ConnectionString = "workstation id=DUAN;packet size=4096;user id=sa;data source=DUAN;persist security" +
" info=True;initial catalog=logDB";
//
// PictureBox1
//
this.PictureBox1.Location = new System.Drawing.Point(145, 62);
this.PictureBox1.Name = "PictureBox1";
this.PictureBox1.Size = new System.Drawing.Size(451, 239);
this.PictureBox1.TabIndex = 12;
this.PictureBox1.TabStop = false;
//
// Button5
//
this.Button5.Location = new System.Drawing.Point(312, 312);
this.Button5.Name = "Button5";
this.Button5.Size = new System.Drawing.Size(102, 35);
this.Button5.TabIndex = 17;
this.Button5.Text = "删除该记录";
this.Button5.Click += new System.EventHandler(this.Button5_Click);
//
// Button4
//
this.Button4.Location = new System.Drawing.Point(568, 308);
this.Button4.Name = "Button4";
this.Button4.Size = new System.Drawing.Size(87, 35);
this.Button4.TabIndex = 16;
this.Button4.Text = "退出";
this.Button4.Click += new System.EventHandler(this.Button4_Click);
//
// Button3
//
this.Button3.Location = new System.Drawing.Point(448, 312);
this.Button3.Name = "Button3";
this.Button3.Size = new System.Drawing.Size(95, 35);
this.Button3.TabIndex = 15;
this.Button3.Text = "显示记录";
this.Button3.Click += new System.EventHandler(this.Button3_Click);
//
// Button2
//
this.Button2.Location = new System.Drawing.Point(8, 312);
this.Button2.Name = "Button2";
this.Button2.Size = new System.Drawing.Size(120, 35);
this.Button2.TabIndex = 14;
this.Button2.Text = "添加到数据库";
this.Button2.Click += new System.EventHandler(this.Button2_Click);
//
// Button1
//
this.Button1.Location = new System.Drawing.Point(397, 24);
this.Button1.Name = "Button1";
this.Button1.Size = new System.Drawing.Size(115, 28);
this.Button1.TabIndex = 13;
this.Button1.Text = "浏览图片…";
this.Button1.Click += new System.EventHandler(this.Button1_Click);
//
// TextBox1
//
this.TextBox1.Location = new System.Drawing.Point(153, 27);
this.TextBox1.Name = "TextBox1";
this.TextBox1.Size = new System.Drawing.Size(208, 21);
this.TextBox1.TabIndex = 11;
this.TextBox1.Text = "";
//
// Label1
//
this.Label1.Location = new System.Drawing.Point(24, 24);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(96, 26);
this.Label1.TabIndex = 9;
this.Label1.Text = "姓名:";
//
// Label2
//
this.Label2.Location = new System.Drawing.Point(24, 80);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(96, 21);
this.Label2.TabIndex = 10;
this.Label2.Text = "图片";
//
// txtDelName
//
this.txtDelName.Location = new System.Drawing.Point(168, 320);
this.txtDelName.Name = "txtDelName";
this.txtDelName.Size = new System.Drawing.Size(128, 21);
this.txtDelName.TabIndex = 18;
this.txtDelName.Text = "";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(672, 366);
this.Controls.Add(this.txtDelName);
this.Controls.Add(this.PictureBox1);
this.Controls.Add(this.Button5);
this.Controls.Add(this.Button4);
this.Controls.Add(this.Button3);
this.Controls.Add(this.Button2);
this.Controls.Add(this.Button1);
this.Controls.Add(this.TextBox1);
this.Controls.Add(this.Label1);
this.Controls.Add(this.Label2);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void Button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog1.InitialDirectory = @"d:\";
OpenFileDialog1.DefaultExt = "gif";
OpenFileDialog1.Filter = "Bmp Files(*.bmp)|*.bmp|Gif Files(*.gif)|*.gif|Jpg Files(*.jpg)|*.jpg";
OpenFileDialog1.ShowDialog();
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName);
this.TextBox1.Text=this.OpenFileDialog1.FileName;
}
private SqlCommand cmd;
private bool InsertIMG(byte[] buffer)
{
bool bl=true;
try
{
cn=new SqlConnection(SqlConnection1.ConnectionString);
cn.Open();
cmd=new SqlCommand("sp_InsertPhoto", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text;
cmd.Parameters.Add("@image", SqlDbType.Image).Value = buffer;
cmd.ExecuteNonQuery();
}
catch
{
bl=false;
}
finally
{
cn.Close();
}
return bl;
} private void showIMG(string s)
{
cn =new SqlConnection(SqlConnection1.ConnectionString);
cn.Open();
string str= "SELECT photo FROM Photos WHERE name='"+ s + "'";
cmd=new SqlCommand(str, cn); byte[] b=(byte[])cmd.ExecuteScalar();
if (b!=null && b.Length > 0)
{
MemoryStream stream=new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
DrawToScale(new Bitmap(stream));
stream.Close();
}
MessageBox.Show("b==null,数据库中没有该条记录");
cn.Close();
} private void DrawToScale(System.Drawing.Image img)
{
this.PictureBox1.Image=img;
} private void Button4_Click(object sender, System.EventArgs e)
{
this.Close();
} //删除
private void Button5_Click(object sender, System.EventArgs e)
{
cn =new SqlConnection(SqlConnection1.ConnectionString);
cn.Open();
cmd=new SqlCommand("delete from photos where name='" + this.txtDelName.Text + "'", cn);
cmd.ExecuteNonQuery();
MessageBox.Show("Image deleted");
cn.Close();
} //插入
private void Button2_Click(object sender, System.EventArgs e)
{
FileStream st=new FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read);
string s= TextBox1.Text;
BinaryReader mbr = new BinaryReader(st);
byte[] buffer=new byte[st.Length];
mbr.Read(buffer, 0,(int)st.Length);
st.Close();
this.InsertIMG(buffer);
} //查询
private void Button3_Click(object sender, System.EventArgs e)
{
this.showIMG(this.TextBox1.Text);
} }
}
[name] varchar(50),
[photo] image NULL
)
GO
SQL SERVER 中的部分
CREATE PROCEDURE sp_InsertPhoto
@name AS VARCHAR(200),
@image AS IMAGE
AS
INSERT INTO Photos ([name], [photo])
VALUES (@name, @image)
GOdrop proc sp_InsertPhoto