byte[] data=myRow[2].ToString();
FileStream fs=new FileStream("e:\test.doc",FileMode.Create,FileAccess.ReadWrite);
fs.Write(data,0,data.Length);
fs.Close();
FileStream fs=new FileStream("e:\test.doc",FileMode.Create,FileAccess.ReadWrite);
fs.Write(data,0,data.Length);
fs.Close();
http://dotnet.aspx.cc/ShowDetail.aspx?id=J9UBRVER-L3VB-49M3-GOU1-Z6C2PVR6FZ3Kusing System.IO;
public class Form1 : System.Windows.Forms.Form
{
SqlClient.SqlConnection cn; public Form1()
{
base.New();
InitializeComponent();
} protected override void Dispose(bool disposing)
{
if (disposing) {
if (!((components == null))) {
components.Dispose();
}
}
base.Dispose(disposing);
}
private System.ComponentModel.IContainer components;
internal System.Windows.Forms.Label Label1;
internal System.Windows.Forms.Label Label2;
internal System.Windows.Forms.TextBox TextBox1;
internal System.Windows.Forms.PictureBox PictureBox1;
internal System.Windows.Forms.Button Button1;
internal System.Windows.Forms.Button Button2;
internal System.Windows.Forms.OpenFileDialog OpenFileDialog1;
internal System.Data.SqlClient.SqlConnection SqlConnection1;
internal System.Data.SqlClient.SqlCommand SqlCommand1;
internal System.Windows.Forms.Button Button3;
internal System.Windows.Forms.Button Button4;
internal System.Windows.Forms.Button Button5; [System.Diagnostics.DebuggerStepThrough()]
private void InitializeComponent()
{
this.Label1 = new System.Windows.Forms.Label();
this.Label2 = new System.Windows.Forms.Label();
this.TextBox1 = new System.Windows.Forms.TextBox();
this.PictureBox1 = new System.Windows.Forms.PictureBox();
this.Button1 = new System.Windows.Forms.Button();
this.Button2 = new System.Windows.Forms.Button();
this.OpenFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.SqlCommand1 = new System.Data.SqlClient.SqlCommand();
this.Button3 = new System.Windows.Forms.Button();
this.Button4 = new System.Windows.Forms.Button();
this.Button5 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.Label1.Location = new System.Drawing.Point(15, 19);
this.Label1.Name = "Label1";
this.Label1.Size = new System.Drawing.Size(80, 24);
this.Label1.TabIndex = 0;
this.Label1.Text = "姓名:";
this.Label2.Location = new System.Drawing.Point(11, 64);
this.Label2.Name = "Label2";
this.Label2.Size = new System.Drawing.Size(80, 20);
this.Label2.TabIndex = 1;
this.Label2.Text = "图片";
this.TextBox1.Location = new System.Drawing.Point(75, 16);
this.TextBox1.Name = "TextBox1";
this.TextBox1.Size = new System.Drawing.Size(173, 20);
this.TextBox1.TabIndex = 2;
this.TextBox1.Text = "";
this.PictureBox1.Location = new System.Drawing.Point(68, 48);
this.PictureBox1.Name = "PictureBox1";
this.PictureBox1.Size = new System.Drawing.Size(376, 222);
this.PictureBox1.TabIndex = 3;
this.PictureBox1.TabStop = false;
this.Button1.Location = new System.Drawing.Point(278, 13);
this.Button1.Name = "Button1";
this.Button1.Size = new System.Drawing.Size(96, 26);
this.Button1.TabIndex = 4;
this.Button1.Text = "浏览图片…";
this.Button2.Location = new System.Drawing.Point(57, 277);
this.Button2.Name = "Button2";
this.Button2.Size = new System.Drawing.Size(100, 32);
this.Button2.TabIndex = 5;
this.Button2.Text = "添加到数据库";
this.SqlConnection1.ConnectionString = "Data Source=.;Initial Catalog=mxh;User Id=sa;Password=;";
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, ((byte)(10)), ((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));
this.Button3.Location = new System.Drawing.Point(265, 277);
this.Button3.Name = "Button3";
this.Button3.Size = new System.Drawing.Size(79, 32);
this.Button3.TabIndex = 6;
this.Button3.Text = "显示记录";
this.Button4.Location = new System.Drawing.Point(362, 277);
this.Button4.Name = "Button4";
this.Button4.Size = new System.Drawing.Size(72, 32);
this.Button4.TabIndex = 7;
this.Button4.Text = "退出";
this.Button5.Location = new System.Drawing.Point(167, 277);
this.Button5.Name = "Button5";
this.Button5.Size = new System.Drawing.Size(85, 32);
this.Button5.TabIndex = 8;
this.Button5.Text = "删除该记录";
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(491, 324);
this.Controls.AddRange(new System.Windows.Forms.Control[]{this.PictureBox1, this.Button5, this.Button4, this.Button3, this.Button2, this.Button1, this.TextBox1, this.Label1, this.Label2});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
} private void Button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog1.InitialDirectory = "d:\\pic";
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);
} 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, System.Convert.ToInt32(st.Length));
st.Close();
InsertImage(buffer, s);
} public object InsertImage(ref object buffer, object str)
{
cn = new SqlClient.SqlConnection(SqlConnection1.ConnectionString);
cn.Open();
SqlClient.SqlCommand cmd = new SqlClient.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();
MsgBox("Image inserted");
cn.Close();
} private void ShowImage(string s)
{
cn = new SqlClient.SqlConnection(SqlConnection1.ConnectionString);
cn.Open();
string str = "SELECT photo FROM Photos WHERE name='" + s + "'";
SqlClient.SqlCommand cmd = new SqlClient.SqlCommand(str, cn);
TextBox1.Text = s;
byte[] b;
b = cmd.ExecuteScalar();
if ((b.Length > 0)) {
MemoryStream stream = new MemoryStream(b, true);
stream.Write(b, 0, b.Length);
DrawToScale(new Bitmap(stream));
stream.Close();
}
cn.Close();
} private void DrawToScale(Image bmp)
{
PictureBox1.Image = new Bitmap(bmp);
} private void Button3_Click(object sender, System.EventArgs e)
{
string i = InputBox("请输入名字:");
ShowImage(i);
} private void Button4_Click(object sender, System.EventArgs e)
{
this.Dispose();
} private void Button5_Click(object sender, System.EventArgs e)
{
cn = new SqlClient.SqlConnection(SqlConnection1.ConnectionString);
cn.Open();
string s = InputBox("请输入要删除的名字:");
SqlClient.SqlCommand cmd = new SqlClient.SqlCommand("delete from photos where name='" + s + "'", cn);
cmd.ExecuteNonQuery();
MsgBox("Image deleted");
cn.Close();
}
}
FileStream fs=new FileStream(this.textBox2.Text.Trim(),FileMode.Open,FileAccess.Read); //将word文件存入数据库
byte[] data=new byte[fs.Length];
fs.Read(data,0,Convert.ToInt32(fs.Length));//文件读到byte[]
fs.Close();
this.sqlCommand1.Parameters["@文件名"].Value=data;