首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c# oledb add增添数据到数据库,参数未声明

2011-12-22 
c# oledb add添加数据到数据库,参数未声明olecommand.CommandText insert into pic(p_name,p_sex,p_age

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); 

热点排行