2012-07-19 116 views
-2

我想执行这个语句来插入一些数据到数据库。插入值到SQL数据库

com = new SqlCeCommand("INSERT INTO [cars table]" 
     + "([Car reg no],[MOT expiry date], Mileage,[Other information])" 
     + "VALUES (" + m._INSERTCarRegNo + "," + m._INSERTExpiryDate + "," 
     + m._INSERTMileage + "," + m._INSERTotherInformation 
     + ")",this.returnConnection); 

以上是我的声明,但我得到一个错误,但错误是无法解析字符串。谁能帮我吗?

谢谢

+2

您可能想要考虑防范SQL注入。 =) – 2012-07-19 09:01:25

+2

您可能也想查找参数化查询。您的代码目前是一个错误和一个潜在的安全威胁。 – 2012-07-19 09:03:37

+0

@TonyHopkinson我懒洋洋地在“bug fest” – 2012-07-19 11:25:59

回答

1

您忘记了字符串值的引号。

com = new SqlCeCommand("INSERT INTO [cars table]" 
    + "([Car reg no],[MOT expiry date], Mileage,[Other information])" 
    + "VALUES ('" + m._INSERTCarRegNo + "','" + m._INSERTExpiryDate + "','" 
    + m._INSERTMileage + "','" + m._INSERTotherInformation 
    + "')",this.returnConnection); 

(你不需要为数值单引号)

1

始终使用Parameters避免此类问题,SQL注入。

var [email protected]"INSERT INTO [cars table] 
     ([Car reg no],[MOT expiry date], Mileage,[Other information]) 
      VALUES (@Carregno,@MOTexpirydate,@Mileage,@Otherinformation])"; 

com = new SqlCeCommand(sql,this.returnConnection); 
com.Parameters.AddWithValue("@Carregno",m._INSERTCarRegNo); 
.. 
1

这将是一个更清洁的解决方案(而不是建立自己的SQL字符串)...

com = 
    new SqlCeCommand(
    "INSERT INTO [cars table] " + 
    "([Car reg no],[MOT expiry date], Mileage,[Other information] " + 
    "VALUES " + 
    "(@reg, @expiry, @mileage, @otherinfo)" 
    , this.returnConnection); 

com.Parameters.AddWithValue("@reg" , m._INSERTCarRegNo); 
com.Parameters.AddWithValue("@expiry" , m._INSERTExpiryDate); 
com.Parameters.AddWithValue("@mileage" , m._INSERTMileage); 
com.Parameters.AddWithValue("@otherinfo" , m._INSERTotherInformation); 
0

你在你的SQL语法错误;检查与您的MySQL服务器版本相对应的手册,以便在第1行的key,created,activations,used,hotelid)values(b,1-29-13, 3,1,11)附近使用正确的语法。

+0

任何一个告诉我如何解决他的那种错误..? – 2013-01-30 11:31:21

+0

查询是: - mysql_query(“插入到引脚(键,创建,激活,使用,hotelid)值($ key,$ created,$ actv,$ usepin,$ hotelid)”); – 2013-01-30 11:32:41