SQL Command "类型为SQL CE"的未处理异常

本文关键字:SQL quot 未处理 异常 类型 Command CE | 更新日期: 2023-09-27 17:49:23

我要尝试在c#中执行非查询命令,但它不会起作用,有人能告诉我我在哪里做的错误。对于那些试图帮助我的人,我试图获得文件修改时间,创建时间和最后访问时间,并将它们放在数据库中。Allfiles是保存路径的字符串数组通过for循环,我打算将它们放入数据库

    cmd = new SqlCeCommand(" IF EXISTS (SELECT * FROM FileInfo WHERE fpath='" + Allfiles[i] + "') " +
                           " UPDATE FileInfo SET fpath='" + Allfiles[i] + 
                           "',laccesst='" + File.GetLastAccessTime(Allfiles[i]) + 
                           "',lmodt='" + File.GetLastWriteTime(Allfiles[i]) + 
                           "',ctime='" + File.GetCreationTime(Allfiles[i]) + 
                           "' WHERE fpath='"+ Allfiles[i] + "'" +
                           " ELSE " +
                           " INSERT INTO FileInfo (fpath, laccesst, lmodt, ctime) VALUES ('" + Allfiles[i] + "','
    " + File.GetLastAccessTime(Allfiles[i]) + "','" + File.GetLastWriteTime(Allfiles[i]) + "','" + File.GetCreationTime(Allfiles[i]) + "')", cn);

例外是:

System.Data.SqlServerCe.SqlCeException was unhandled
  HResult=-2147467259
  Message=There was an error parsing the query. [ Token line number = 1,Token line offset = 2,Token in error = IF ]
  Source=SQL Server Compact ADO.NET Data Provider
  ErrorCode=-2147467259
  NativeError=25501
  StackTrace:
       at System.Data.SqlServerCe.SqlCeCommand.ProcessResults(Int32 hr)
       at System.Data.SqlServerCe.SqlCeCommand.CompileQueryPlan()
       at System.Data.SqlServerCe.SqlCeCommand.ExecuteCommand(CommandBehavior behavior, String method, ResultSetOptions options)
       at System.Data.SqlServerCe.SqlCeCommand.ExecuteNonQuery()
       at directoryDatabase.Program.Main(String[] args) in c:'Users'Alban'Documents'Visual Studio 2012'Projects'directoryDatabase'directoryDatabase'Program.cs:line 36
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

SQL Command "类型为SQL CE"的未处理异常

我想你忘记把Begin..End放在IF..ELSE部分了。

cmd = new SqlCeCommand(" IF EXISTS (SELECT * FROM FileInfo WHERE fpath='" + Allfiles[i] + "') " +
                       " BEGIN " + 
                       "UPDATE FileInfo SET fpath='" + Allfiles[i] + 
                       "',laccesst='" + File.GetLastAccessTime(Allfiles[i]) + 
                       "',lmodt='" + File.GetLastWriteTime(Allfiles[i]) + 
                       "',ctime='" + File.GetCreationTime(Allfiles[i]) + 
                       "' WHERE fpath='"+ Allfiles[i] + "'" +
                       " END "
                       " ELSE " +
                       " BEGIN " +
                       " INSERT INTO FileInfo (fpath, laccesst, lmodt, ctime) VALUES ('" + Allfiles[i] + "','
    " + File.GetLastAccessTime(Allfiles[i]) + "','" + File.GetLastWriteTime(Allfiles[i]) + "','" + File.GetCreationTime(Allfiles[i]) + "')" +
                       " END", cn);