2015-05-17 34 views
0

我是想在C#中执行命令nonquery,但它只是将无法工作有人可以告诉我,我做了错误“类型的SQL CE未处理的异常”。对于那些试图帮助我的人,我试图获得文件修改时间创建时间和上次访问时间并将它们放入数据库中。 Allfiles是一个字符串数组,我保存的路径,并用一个for循环,我打算把它们放在数据库SQL命令

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: 
+0

什么是异常消息完全?分享您的堆栈跟踪。你应该总是使用参数化查询。这种字符串连接对于SQL注入攻击是开放的。 http://bobby-tables.com/ –

+0

请张贴满异常(包括堆栈跟踪) –

+0

我想这一定有什么关系'''Allfiles'''变量。你可以把你的查询放入字符串变量,并在这里发布结果查询。必须有上查询,一些错字等 –

回答

0

我想你忘了把Begin..EndIF..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); 
+0

不强制 –

+1

的SQL Server Compact不支持IF EXISTS – Steve

+0

啊你去那里,我的错误。还有另外一个类似的文章太所陈述SQL CE不支持'''IF''' http://stackoverflow.com/questions/4652867/if-not-exists-fails-on-sql-ce –