尝试以编程方式使用SQLite .import命令时出现语法错误

时间:2018-08-29 17:44:09

标签: c# sql .net sqlite

我尝试了this question的答案中的代码。

我尝试了以下代码:

   static void Main(string[] args)
    {
        System.IO.File.WriteAllLines("myFile.csv",
            new[]
            {
                "a,b,c",
                "1,2,3",
                "4,5,6",
                "7,8,9"
            });

        const string cmd = @"create table myTable(a text, b text, c text);
                            .separator ','
                            .import  myFile.csv myTable";

        using (var conn = new SQLiteConnection("Data Source=test.db;Version=3;"))
        {
            conn.Open();

            var cmdExec = new SQLiteCommand(cmd, conn);
            cmdExec.ExecuteNonQuery();
        }
}

但是,出现以下错误:

SQL logic error
near ".": syntax error

堆栈跟踪:

System.Data.SQLite
at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain)
   at System.Data.SQLite.SQLiteCommand.BuildNextCommand()
   at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index)
   at System.Data.SQLite.SQLiteDataReader.NextResult()
   at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave)
   at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery(CommandBehavior behavior)
   at System.Data.SQLite.SQLiteCommand.ExecuteNonQuery()
   at Reporting_Testing_Facility.Program.Main(String[] args) in C:\Users\SUE2MTP\Documents\Visual Studio 2015\Projects\Reporting Testing Facility\Reporting Testing Facility\Program.cs:line 127
   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()

是否可以解决此问题?还是只能在命令行中使用此命令?

0 个答案:

没有答案