日期:2014-05-18  浏览次数:20811 次

c# oledb add添加数据到数据库,参数未声明
olecommand.CommandText = "insert into pic(p_name,p_sex,p_age,p_filename) 
  values (@pname,@psex,@page,@pfilename)";
   
  olecommand.Parameters.Add("@pname", OleDbType.VarChar, 10);
  olecommand.Parameters.Add("@psex", OleDbType.VarChar, 2);
  olecommand.Parameters.Add("@page", OleDbType.Integer, 8);
  olecommand.Parameters.Add("@pfilename", OleDbType.VarChar, 50);
  olecommand.Prepare();
  olecommand.Parameters["@pname"].Value = tname.Text.Trim ();
  olecommand.Parameters["@psex"].Value = tsex.Text.Trim();
  olecommand.Parameters["@page"].Value = tage.Text.Trim();
  olecommand.Parameters["@pfilename"].Value = filename;

提示 未能准备语句,必须声明变量:@pname....等
找了很多都没有弄懂,书上也没有找到类似的说明,请帮忙 !


------解决方案--------------------
这样就可以

C# code
olecommand.CommandText = "insert into pic(p_name,p_sex,p_age,p_filename) values (?,?,?,?)";
olecommand.Parameters.AddWithValue("?", tname.Text.Trim()); 
olecommand.Parameters.AddWithValue("?",  tsex.Text.Trim()); 
olecommand.Parameters.AddWithValue("?", tage.Text.Trim());
olecommand.Parameters.AddWithValue("?", filename);