标题:[求助]如何把图像插入ACCESS数据库
只看楼主
dingsheng_0
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-9-12
 问题点数:0 回复次数:15 
[求助]如何把图像插入ACCESS数据库

我想把图象插入ACESS数据库中,在数据库中设置了三个字段,其中image是OLE类型,插入时报错,错误提示是INSERT into 语句的语法错误....但是如果我不插入图象,只插入StudentID, Name字符串类型有没有错误,期待大家回复.

byte[]image_bytes =null;
OpenFileDialog myDialog = new OpenFileDialog();
myDialog.ShowDialog();
if(myDialog.FileName.Trim()!="")
{
pictureBox1.Image = System.Drawing.Bitmap.FromFile(myDialog.FileName);
}

Stream mystream = myDialog.OpenFile();
leng =(int)mystream.Length;
image_bytes = new byte[leng];
mystream.Read(image_bytes,0,leng);
mystream.Close();

string CString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + @"\S11.MDB";
MyCon=new OleDbConnection(CString);
try//创建连接对象
{
MyCon.Open();//打开连接}
}

catch(Exception ee)
{
MessageBox.Show(ee.Message);

}
try
{
string SqlCmd = "INSERT INTO st(StudentID, Name, image) VALUES (@StudentID, @Name, @image)";
MyCom = new OleDbCommand(SqlCmd,MyCon);
MyCom.Parameters.Add("@StudentID", OleDbType.VarChar,8).Value = "11010187";
MyCom.Parameters.Add("@Name", OleDbType.VarChar,8).Value = "杜威";
MyCom.Parameters.Add("@image",OleDbType.Binary,leng).Value = image_bytes;
MyCom.ExecuteNonQuery();
MessageBox.Show("成功");
}
catch(Exception ee)
{

MessageBox.Show(ee.Message);
}

搜索更多相关主题的帖子: 数据库 ACCESS 图像 
2006-09-12 08:37
chenjin145
Rank: 1
等 级:禁止访问
帖 子:3922
专家分:0
注 册:2006-7-12
得分:0 
MyCom.Parameters.Add("@image",OleDbType.Binary,leng).Value = image_bytes;

去掉再看下

[url=javascript:alert(1);] [div]fdgfdgfdg\" on\"[/div] [/url]
2006-09-12 08:48
dingsheng_0
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-9-12
得分:0 
谢谢版主回复,去掉还是不行.....

2006-09-12 09:33
hoya
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:538
专家分:0
注 册:2006-6-27
得分:0 
leng 改成image看看...

把错误代码贴出来...

妈的...一天能卖一颗就很不错了...
2006-09-12 09:39
dingsheng_0
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-9-12
得分:0 
谢谢三楼, MyCom.Parameters.Add("@image",OleDbType.Binary,leng).Value = image_bytes;
leng 改成image,参数就不正确了.
系统提示是 :
INSERT into 语句的语法错误




2006-09-12 10:25
hoya
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:538
专家分:0
注 册:2006-6-27
得分:0 
把leng去掉,然后格式化image_bytes看看

妈的...一天能卖一颗就很不错了...
2006-09-12 16:30
dingsheng_0
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2006-9-12
得分:0 

谢谢hoya回复,
MyCom.Parameters.Add("@image",OleDbType.Binary) = image_bytes;
还是一样的错误.


2006-09-12 21:03
jockey
Rank: 3Rank: 3
等 级:论坛游民
威 望:8
帖 子:977
专家分:52
注 册:2005-12-4
得分:0 
继续关注....

2006-12-01 12:49
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
得分:0 
把 leng 改成一个具体的值呢?

飘过~~
2006-12-01 20:00
IT浪子
Rank: 4
来 自:Nyist
等 级:贵宾
威 望:13
帖 子:402
专家分:20
注 册:2006-11-24
得分:0 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace db
{
public partial class Form1 : Form
{
byte[] image_bytes = null;
public OleDbConnection MyCom = null;
private string fullname;

public Form1()
{
InitializeComponent();
}


public void button1_Click(object sender, EventArgs e)
{

OpenFileDialog myDialog = new OpenFileDialog();
myDialog.ShowDialog();
this.fullname = myDialog.FileName;
if (myDialog.FileName.Trim() != "")
{
pictureBox1.Image = System.Drawing.Bitmap.FromFile(myDialog.FileName);
}

}

private void button2_Click(object sender, EventArgs e)
{
string CString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + Application.StartupPath + @"\S11.mdb";

MyCom= new OleDbConnection(CString);
MyCom.Open();

FileStream mystream = File.OpenRead(this.fullname);
image_bytes = new byte[mystream.Length];
mystream.Read(image_bytes, 0, image_bytes.Length);
mystream.Close();

string SqlCmd = "INSERT INTO st(StudentID, Name,myimage) VALUES ('" +this.textBox1.Text + "','" + this.textBox2.Text + "',@image)";
OleDbCommand cmd = new OleDbCommand(SqlCmd, MyCom);
//cmd.Parameters.Add("@StudentID", OleDbType.VarChar, 8).Value = this.textBox1.Text;
//cmd.Parameters.Add("@Name", OleDbType.VarChar, 8).Value = this.textBox2.Text;
cmd.Parameters.Add("@image", OleDbType.Binary).Value = image_bytes;
try
{

cmd.ExecuteNonQuery();
MessageBox.Show("成功");
}
catch (Exception ee)
{

MessageBox.Show(ee.Message);
}

finally
{
MyCom.Close();
}
}
}
}
调试了一会,不过还是调试出来了,你看看吧,有什么问题在贴出来了


你不能让所有人都满意,因为不是所有人都是人!
2006-12-01 20:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-89527-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.172354 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved