0

我有这个代码:

SqlCeConnection connection = new SqlCeConnection(conString);
connection.Open();
connection.CreateCommand();
cmd.CommandText = "CREATE TABLE Settings (id int IDENTITY PRIMARY KEY, host nvarchar(50), port int, rk_host nvarchar(50), rk_port int, stationcode int, guesttypecode int)";
cmd.ExecuteNonQuery();
cmd = connection.CreateCommand();
cmd.CommandText = "INSERT INTO Settings ([host], [port], [rk_host], [rk_port], [stationcode], [guesttypecode]) VALUES ('" + Settings.host + "', '" + Settings.rk_host + "', '" + Settings.rk_port + "', '" + Settings.port + "', '" + Settings.stationcode + "', '" + Settings.guesttypecode + "')";
cmd.ExecuteNonQuery();
connection.Close();

在第二个ExecuteNonQuery我有一个例外:"Data conversion failed. [ OLE DB status value (if known) = 2 ]"

有谁能够帮助我?

4

2 回答 2

1

在 Int 类型列上,您应该从查询中删除单引号

cmd.CommandText = "INSERT INTO Settings ([host], [port], [rk_host], [rk_port], [stationcode], [guesttypecode]) VALUES ('" + Settings.host + "', '" + Settings.rk_host + "', " + Settings.rk_port + ", '" + Settings.port + "', " + Settings.stationcode + ", " + Settings.guesttypecode + ")";
于 2012-10-22T14:09:12.937 回答
0

第一个建议是永远不要这样写你的查询,因为这会导致SQL Injection。其次,您正在阅读您的设置并将它们插入您的表格而不考虑它们的类型。您需要进行类型转换(例如int.Parse(Settings.port)

于 2012-10-22T14:10:22.120 回答