阅读:2135回复:0
如何把图象写入ACESS数据库中
<P> 我想把图象插入ACESS数据库中,在数据库中设置了三个字段,其中image是OLE类型,插入时报错,错误提示是INSERT into 语句的语法错误....但是如果我不插入图象,只插入StudentID, Name字符串类型有没有错误,期待大家回复.</P>
<P> byte[]image_bytes =null;<BR> OpenFileDialog myDialog = new OpenFileDialog();<BR> myDialog.ShowDialog();<BR> if(myDialog.FileName.Trim()!="")<BR> {<BR> pictureBox1.Image = System.Drawing.Bitmap.FromFile(myDialog.FileName);<BR> }<BR> </P> <P> Stream mystream = myDialog.OpenFile();<BR> leng =(int)mystream.Length;<BR> image_bytes = new byte[leng];<BR> mystream.Read(image_bytes,0,leng);<BR> mystream.Close();</P> <P>string CString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + @"\S11.MDB";<BR> MyCon=new OleDbConnection(CString);<BR> try//创建连接对象<BR> {<BR> MyCon.Open();//打开连接}<BR> }</P> <P> catch(Exception ee)<BR> {<BR> MessageBox.Show(ee.Message);<BR> <BR> } <BR> try<BR> {<BR> string SqlCmd = "INSERT INTO st(StudentID, Name, image) VALUES (@StudentID, @Name, @image)";<BR> MyCom = new OleDbCommand(SqlCmd,MyCon);<BR> MyCom.Parameters.Add("@StudentID", OleDbType.VarChar,8).Value = "11010187";<BR> MyCom.Parameters.Add("@Name", OleDbType.VarChar,8).Value = "杜威";<BR> MyCom.Parameters.Add("@image",OleDbType.Binary,leng).Value = image_bytes;<BR> MyCom.ExecuteNonQuery();<BR> MessageBox.Show("成功");<BR> } <BR> catch(Exception ee)<BR> {<BR> <BR> MessageBox.Show(ee.Message);<BR> }<BR></P> |
|