使用INSERT INTO语句获取语法错误

时间:2018-06-05 08:11:11

标签: c# excel oledbcommand

我收到“INSERT INTO语句中的C#语法错误”错误 对于下面这部分代码:

    $pdo = $service->pdo;
    $sql = "SELECT a.id, a.name, a.surname, a.job, b.start_date, b.end_date
    FROM table1 a
    INNER JOIN table2 b on b.id = a.id
    WHERE a.job = '{$job}'
    AND
    STR_TO_DATE(b.`start_date`, '%d-%m-%Y') = '{$date}'
    GROUP BY a.id ";
    return $pdo->query($sql)->fetchAll(PDO::FETCH_OBJ);

连接字符串详细信息为:

public static void SaveCurrentPlaylistToSheet(YouTubeVideo[] CurrentPlaylist, string playlistTitle)
{
    ActiveSheetTitle = "Test"; //playlistTitle;
    string currentTitle;

    if (FileCreation)
    {
        NewPlaylist = true;
    }

    using (conn = new OleDbConnection(ConnectionString))
    {
        conn.Open();
        try
        {
            cmd = new OleDbCommand();
            cmd.Connection = conn;

            currentTitle = "Test";

            for (int i = 0; i < CurrentPlaylist.Length; i++)
            {
                //currentTitle = Regex.Replace(CurrentPlaylist[i].title, " ", "");
                //currentTitle = currentTitle.Replace(",", "");

                using (cmd = conn.CreateCommand())
                {
                    if (NewPlaylist)
                    {
                        NewPlaylist = false;
                        cmd.CommandText = "INSERT INTO [" + ActiveSheetTitle + "$] (F1) VALUES (?)";
                    }
                    else
                    {
                        cmd.CommandText = "INSERT INTO [" + ActiveSheetTitle + "] (F1) VALUES (?)";
                    }
                    cmd.Parameters.Add("?", OleDbType.VarChar).Value = currentTitle;

                    cmd.ExecuteNonQuery(); 
                }
            }
        }
        catch (Exception ex)
        {
            string filePath = @"C:\Dropbox\amitai\Programming\C# projects\Youtube Playlists\Error.txt";

            using (StreamWriter writer = new StreamWriter(filePath, true))
            {
                writer.WriteLine("Message :" + ex.Message + "<br/>" + Environment.NewLine + "StackTrace :" + ex.StackTrace +
                   "" + Environment.NewLine + "Date :" + DateTime.Now.ToString());
                writer.WriteLine(Environment.NewLine + "-----------------------------------------------------------------------------" + Environment.NewLine);
            }
        }

        cmd = null;
        conn.Close();
        cmd.Dispose();
        conn.Dispose();
    }

    ActiveSheetTitle = null;
}

和“myPath”从以下位置检索:

ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=
                    " + MyPath + ";" +
                    "Mode=ReadWrite;" + "Mode=ReadWrite;" + //ReadOnly=false" +
                    "Extended Properties=\"Excel 12.0;HDR=NO; IMEX =1;\"";        

通过谷歌搜索我找到了这个错误的解决方案,使用a联系 保留关键字作为参数或接收一些空间输入,因此出于测试目的,我将currentTitle和ActiveSheetTitle设置为“Test”,但仍然存在错误。

任何帮助解决问题的人都将不胜感激。

1 个答案:

答案 0 :(得分:-1)

感谢所有帮助。 借助有关该问题的评论 我已经发现自那以后出现异常 该文件在该阶段不存在。 希望这会帮助遇到类似问题的人。