【发布时间】:2014-10-24 23:42:36
【问题描述】:
我正在通过 WCF 服务连接到紧凑型 SQL 数据库服务器,并且除了 Command.ExecuteNonQuery() 之外,我不断收到以下信息。我试过解决这个问题,但就是不知道出了什么问题。
例外:
发生“System.Data.SqlServerCe.SqlCeException”类型的异常 在 System.Data.SqlServerCe.dll 中但未在用户代码中处理
代码:
//The connectionString can be found in the properties table of the database
string connString = "Data Source=C:\\Users\\User\\documents\\visual studio 2012\\Projects\\ADO_LINQ\\ADO_LINQ\\App_Data\\MyDatabase.sdf;Persist Security Info = False";
SqlCeConnection myConnection = new SqlCeConnection(connString);
myConnection.Open();
// Create the query
string myQuery = "INSERT INTO Player " +
" VALUES (" + registrationID + "," +
firstName + ", " +
lastName + ", " +
phoneNumber + ", " +
address + ", " +
dateOfBirth + ");";
//Initialuze the command
SqlCeCommand myCommand = new SqlCeCommand(myQuery, myConnection);
//Run the command
myCommand.ExecuteNonQuery();
//Close the connection
myConnection.Close();
【问题讨论】:
-
参数。使用参数。
-
您必须在查询中指定字段名称。 INSERT INTO(field1, field2,...fieldN) VALUES(valueField1, valueField2, ..., valueFIeldN。另外有些值需要放在标签''之间。
-
另外,尝试放置一个 try/catch 块并输出真正的异常详细信息。
标签: c# database wcf ado.net sql-server-ce