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....等
找了很多都没有弄懂,书上也没有找到类似的说明,请帮忙 !

解决方案 »

  1.   

    这样就可以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); 
      

  2.   

    在此上下文中不允许使用 'pname'。此处只允许使用常量、表达式或变量。不允许使用列名。...olecommand.CommandText = "insert into pic(p_name,p_sex,p_age,p_filename)
                                            values (pname,psex,page,pfilename)";
    olecommand.ExecuteNonQuery();就是简单的从textbox中得到值保存到pname,然后插入到数据库.......
    请问有什么直接的方法吗,像这样?
    不必到数据中建存储过程等..