当前位置:Linux教程 - Linux文化 - 请问,怎样把图片直接存到sql2000中?

请问,怎样把图片直接存到sql2000中?


>>> 此贴的回复 >> http://singlepine.cnblogs.com/articles/288027.html

>>> 此贴的回复 >> 用参数,参数指向一个byte数组。

>>> 此贴的回复 >> 用流和字节数组的形式保存到数据库。

>>> 此贴的回复 >> /// /// 保存图片 /// ///

///

private void button2_Click(object sender, System.EventArgs e) { try { System.IO.FileInfo fs = new System.IO.FileInfo(this.filename); if (!fs.Exists) { MessageBox.Show("无效的文件名!"); return; } //创建一字节数组,用来存储图像文件.(数组的长度是图像文件的长度) byte[] Content=new byte[fs.Length]; //打开文件并用他初始化一个文件流对象 FileStream ImageFileStream=fs.OpenRead(); //将文件内容写入字节数组 ImageFileStream.Read(Content,0,Content.Length); //关闭文件流 ImageFileStream.Close();

//创建一个Sql命令对象,用来插入记录 System.Data.OleDb.OleDbCommand mycmd = new System.Data.OleDb.OleDbCommand(updatesql,DB.OleConn()); mycmd.CommandType = CommandType.StoredProcedure; mycmd.Parameters.Add("@image",System.Data.OleDb.OleDbType.VarBinary); mycmd.Parameters.Add("@bh",OleDbType.VarChar,20);

mycmd.Parameters["@image"].Value = Content; mycmd.Parameters["@bh"].Value =bh;

//打开数据库连接 if (mycmd.Connection.State!=ConnectionState.Open) { mycmd.Connection.Open(); } //执行 Sql 语句 mycmd.ExecuteNonQuery(); //关闭数据库连接 mycmd.Connection.Close();

MessageBox.Show("图像文件 " + fs.FullName + " 成功上传到数据库!"); this.filename =""; myds.Tables[0].Clear(); myda.Fill(myds,"A"); //mybind.Position =0; //this.Showimage(); } catch (Exception ex) { MessageBox.Show(ex.Message); }

this.button2.Enabled = false; }