当前位置:编程学习 > C#/ASP.NET >>

怎么样把图片的路径插入到access表中?急……???(1)

最近做一个电子相册,要求有浏览插入删除等功能,原来我想把图片储存到数据库中,可是那样不会,就把路径储存到数据库中,可是我怎么就插不进去,我用c#做前台的access做后台,我把我的代码传来,大家帮我看一下?小弟在此先谢了!!!!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
namespace wqt
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.TextBox textBox2;
  private System.Windows.Forms.TextBox textBox3;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Button button2;
  private System.Windows.Forms.TextBox textBox4;
  private System.Windows.Forms.OpenFileDialog openFileDialog1;
  private System.Windows.Forms.SaveFileDialog saveFileDialog1;
  private string filename="未命名.jpg";
  private System.Data.OleDb.OleDbCommand oleDbCommand1;
  private System.Data.OleDb.OleDbConnection oleDbConnection1;
--------------------编程问答-------------------- My god,你这个还早得很呢。
把图片的完整路径当成字符串存在Access中不就行了吗?

string path =图片路径;
string sql="Insert YourTable values(@Field1,@Field2,@Field3,@PicturePath)";
oleDbCommand1.CommandText=sql;
oleDbCommand1.Parameters.Add("@PicturePath",OleDbType.Char).Value=path;
.......

--------------------编程问答-------------------- 顶楼上的! --------------------编程问答-------------------- 大侠,我按照你的提示改了,可是还是不行出现这样的错误[img=http://sz.photo.store.qq.com/rurl2=17bf4e473a06e0983dc5efd08335e5ace91842a46aa2de6ece0365017f571608c247df96f1c5fe5ea113997867ee0c41354bb8c6e0d7c4fa774907a54a103c6dfead1f66e74283a49b988f3cb647d81152b32ed0][/img]难道是数据库的问题[img=http://sz.photo.store.qq.com/rurl2=58378e1c9d316f50de109b87cdd58c399bde21ed8477269eb3894a990df8840c4b80fc7b6276876a870e299269036286483df4248eac6fa62b4610d4c0c89c25103a1cb81837e583223880f7e4ecfdbea300063a][/img] --------------------编程问答-------------------- 跟着学一把! --------------------编程问答--------------------
修改的代码:
private void button2_Click(object sender, System.EventArgs e)
{
string bh = textBox1.Text;
string mc = textBox2.Text;
string wz = textBox3.Text;
string sm = textBox4.Text;
string sqlString = "Insert into 图片(编号,名称,位置,说明) values( @bh, @mc,@wz, @sm)";
oleDbCommand1.CommandText=sqlString; 
oleDbCommand1.Parameters.Add("@PicturePath",OleDbType.Char).Value=wz; 

OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source = Score.mdb");
OleDbCommand comm = new OleDbCommand(sqlString,conn);
conn.Open();
try
{
comm.ExecuteNonQuery();
MessageBox.Show("插入成功!");
}
catch (Exception ex)
{
MessageBox.Show("插入出错!错误原因为:\n"+ex.Message+"\n");
}
conn.Close();


} --------------------编程问答-------------------- 自己在好好看看,是不是数据库字段没设计好
估计是你数据库没有那个字段.
本来是个很容易的事,怎么就这么复杂? --------------------编程问答-------------------- 估计是文件明太长了。
修改下数据库的存储字段长度的定义,access默认好像是50,改成255(最大)好了 --------------------编程问答-------------------- 简单弄的复杂了。 --------------------编程问答-------------------- string sqlString = "Insert into 图片(编号,名称,位置,说明) values( @bh, @mc,@wz, @sm)"; 
oleDbCommand1.CommandText=sqlString; 
oleDbCommand1.Parameters.Add("@PicturePath",OleDbType.Char).Value=wz; 

你在Insert Into语句中设置了四个参数,但你实际上只加入了一个参数的值

oleDbCommand1.Parameters.Add("@PicturePath",OleDbType.Char).Value=wz; 


--------------------编程问答-------------------- 写图片路径时,写的是绝对路径还是相对路径,我想把图片和程序放在同一个文件夹中,使其拷到另一台计算机中也能用
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,