string t = "123123";
string b = "fdsafda";
int id = 14;
byte[] img = new byte[]{0};设已经有 OleDbConnection Connstring Sql = @"Update TreeView Set Title=@sTitle,Des=@vContent,Img=@Image Where ID=@Id";
OleDbCommand odc = new OleDbCommand(Sql,Conn);
OleDbParameter p1 = new OleDbParameter("@Id",OleDbType.Integer,9);
OleDbParameter p2 = new OleDbParameter("@sTitle",OleDbType.LongVarWChar,100);
OleDbParameter p3 = new OleDbParameter("@vContent",OleDbType.LongVarWChar);
OleDbParameter p4 = new OleDbParameter("@Image",OleDbType.LongVarBinary);
p1.Value = id;
p2.Value = t;
p3.Value = b;
p4.Value = img;
odc.Parameters.Add(p1);
odc.Parameters.Add(p2);
odc.Parameters.Add(p3);
odc.Parameters.Add(p4);
Conn.Open();
int r = odc.ExecuteNonQuery();r总为0;
更新不到相应的记录
我确定记录是绝对存在的!
我曾试过
string Sql = @"Update TreeView Set Title='123123' Where ID=@Id";
是可以更新.但不知为什么使用参数的话就更新不了,如:
string Sql = @"Update TreeView Set Title=@sTitle Where ID=@Id";Title是文本字段
Des  是备注
Img  是Ole对象[存图片用的]
ID   是自动编号求高手解答解答.
图片必须存到数据库里的..无办法,一定要用传参.

解决方案 »

  1.   

    你試一下,ID是ACCESS里的關鍵字,需要加[]  你用[ID]試一下.
      

  2.   

    就是把你的string Sql = @"Update TreeView Set Title=@sTitle Where ID=@Id";
    改為 string Sql = @"Update TreeView Set Title=@sTitle Where [ID] =@Id";
    就OK了
      

  3.   

    To:dragonfly001()
       不行.还是一个鸟样~
      

  4.   

    用OleDb时参数写法是问号(?),在SqlClient中用@加参数名,应该这样写:
    string Sql = @"Update TreeView Set Title=? Where ID=?";
      

  5.   

    这个是我在我的机器上面作的测试,好像没有什么问题,你看一下.
    (三个button ,一个文本输入,一个picturebox) private void Form1_Load(object sender, System.EventArgs e)
    {
    this.button2.Enabled = false;
    this.button2.Text = "上传至库";
    this.button1.Text = "上传文件";
    this.button3.Text = "根据ID显示图片"; this.strSqlConn = @"Data Source=""F:\C#\ImageToDb\bin\Debug\db.mdb"";Provider=""Microsoft.Jet.OLEDB.4.0"";";
    } private void button2_Click(object sender, System.EventArgs e)
    {
    if (this.textBox1.Text != string.Empty )
    {
    try
    {
    fs = new FileStream( this.filename,FileMode.Open );
    bytes = new byte[(int)fs.Length];
    int intFile = fs.Read( bytes,0,bytes.Length);
    if (intFile>0)
    {
    conn = new OleDbConnection( this.strSqlConn );
    this.strSqlCmd = "Insert into tab (Img,title,des) values (@ph,@title,@des)" ;
    cmd = new OleDbCommand( strSqlCmd,conn );
    conn.Open();
    cmd.Parameters.Add("@ph",OleDbType.Binary);
    cmd.Parameters.Add("@title",OleDbType.VarChar);
    cmd.Parameters.Add("@des",OleDbType.LongVarWChar);
    cmd.Parameters["@title"].Value = "标题";
    cmd.Parameters["@ph"].Value = this.bytes;
    cmd.Parameters["@des"].Value = "这个是备注说明字段";
    int intS = cmd.ExecuteNonQuery();
    if (intS<0)
    {
    MessageBox.Show("更新失败");
    }
    MessageBox.Show("成功更新");
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show( ex.Message );
    }
    finally
    {
    conn.Close();
    fs.Flush();
    fs.Close();
    }
    }
    else
    {
    MessageBox.Show("请输入准考证号");
    }
    } private void button3_Click(object sender, System.EventArgs e)
    {
    try
    {
    conn = new OleDbConnection( this.strSqlConn);
    this.strSqlCmd = "Select img From tab Where id = " + this.textBox1.Text + "";
    conn.Open();
    cmd = new OleDbCommand( strSqlCmd ,conn );
    object obj = cmd.ExecuteScalar();
    this.bytes = (byte[])obj;
    using ( MemoryStream ms = new MemoryStream( bytes,0,bytes.Length ))
    {
    Bitmap bmp = new Bitmap( ms );
    this.pictureBox1.Image = bmp;
    }
    }
    catch(Exception ex)
    {
    MessageBox.Show(ex.Message,"出错了!",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
    finally
    {
    conn.Close();
    }
    }