表名 Book_Info    
列分别为:Book_ID(varchar), Book_Name(varchar),Writer(varchar),Press(varchar),Price(float)
我想插入一条语句:str="Insert into Book_Info(Book_ID,Book_Name,Writer,Press,Price)"
    " VALUES('m_BookID','m_BookName','m_Write','m_Press', m_Price ) ";其中:  m_BookID ,  m_BookName, m_Write ,m_Press 均为CSting类型。  m_Price 为long类型,一运行报错:在此上下文中不允许使用m_Price 。此处允许使用常量,表达式或变量。不允许使用列名。 我这里的m_Price 就是一个变量。
把m_Price 加上单引号变成str="Insert into Book_Info(Book_ID,Book_Name,Writer,Press,Price)"
    " VALUES('m_BookID','m_BookName','m_Write','m_Press', ‘m_Price’ ) ";又提示:将数据类型vachar转换成float出错。
无语了,应该怎么改?????求指点。

解决方案 »

  1.   


    怎么才能看到啊 不知道怎么搞啊。  
    第一个错误提示就是:在此上下文中不允许使用m_Price 。此处允许使用常量,表达式或变量。
    第二个错误提示是:将数据类型vachar转换成float时出错。
      

  2.   

    str="Insert into Book_Info(Book_ID,Book_Name,Writer,Press,ltrim(Price))"
                " VALUES('m_BookID','m_BookName','m_Write','m_Press', ‘m_Price’ ) ";
      

  3.   

    insert into
      Book_Info(Book_ID,Book_Name,Writer,Press,cast(Price as varchar(10))
    select 
     'm_BookID','m_BookName','m_Write','m_Press', ‘m_Price’ 
      

  4.   

    str="Insert into Book_Info(Book_ID,Book_Name,Writer,Press,Price)"+
                " VALUES('m_BookID','m_BookName','m_Write','m_Press', m_Price) ";首先不说语句,就两个字符串都没加吧,如果你后面的是变量
    m_Price
    你用参数形式看看,你的点点可能不对
      

  5.   

    看楼主的str的形式,应该不是在SQL里定义的字符串吧,说说你用的开发语言吧.
      

  6.   

    ‘m_Price’ 这是两边的‘’是中文的吧,换成英文的
      

  7.   

    str="Insert into Book_Info(Book_ID,Book_Name,Writer,Press,Price)"
                " VALUES('m_BookID','m_BookName','m_Write','m_Press', "+m_Price+" ) ";
      

  8.   

     不用 “+”号吧  加上去编译不通过   m_Price我是和控件相关联的变量
      

  9.   

    VC++ 6.0啊   m_Price我是和控件相关联的变量   整个语句为:CString str;
    str="Insert into Book_Info(Book_ID,Book_Name,Writer,Press,Price)"
                " VALUES('m_BookID','m_BookName','m_Write','m_Press', ‘m_Price’ ) ";
     m_db.ExecuteSQL(str);
    其中m_db为Cdatabase类
      

  10.   

    CString str;
    str="Insert into Book_Info(Book_ID,Book_Name,Writer,Press,Price)
               VALUES('m_BookID','m_BookName','m_Write','m_Press', m_Price ) ";
     m_db.ExecuteSQL(str);这样应该可以吧,我也没试过!可能就是符号的问题而已!
      

  11.   

     
    CString str; 
    str="Insert into Book_Info(Book_ID,Book_Name,Writer,Press,Price)"+
         "VALUES("+ "'"+m_BookID+"','"+ m_BookName+"','+ m_Write+"','"+m_Press+", "+ m_Price  +") ";m_db.ExecuteSQL(str); 
    试试,
    在SQL中char型的要加''的。在C#中利用""模拟
      

  12.   

    str="Insert into Book_Info(Book_ID,Book_Name,Writer,Press,Cast(ltrim(rtrim(Price)) as varchar(200)))"
                " VALUES('m_BookID','m_BookName','m_Write','m_Press', m_Price ) ";
      

  13.   

    Price float型楼主 应该是要取 m_Price 的值 往里面传吧 ,其他的也应该是取值,而不是把字符串传进去吧str=@"Insert into Book_Info(Book_ID,Book_Name,Writer,Press,Price)"
    +@"VALUES('{0}','{1}','{2}','{3}',{4}) ";str=string.Format(str,m_BookID,m_BookName,m_Write,m_Press,m_Price);这个str 应该才是要执行的SQL 吧