【发布时间】:2018-05-31 13:24:09
【问题描述】:
我收到“查询值的数量和目标字段的数量不一样”的错误消息。对于下面出现的代码部分 -
public static void SaveCurrentPlaylistToSheet(YouTubeVideo[] CurrentPlaylist)
{
string currentTitle;
using (connection = new OleDbConnection(ConnectionString))
{
connection.Open();
command = new OleDbCommand();
command.Connection = connection;
using (command = connection.CreateCommand())
{
for (int i = 0; i < CurrentPlaylist.Length; i++)
{
currentTitle = Regex.Replace(CurrentPlaylist[i].title, " ", "");
if (NewPlaylist)
{
command.CommandText = string.Format("INSERT INTO [" + ActiveSheetTitle + "] (F1) values({0})", currentTitle);
NewPlaylist = false;
}
else
{
command.CommandText = string.Format("INSERT INTO [" + ActiveSheetTitle + "$] (F1) values({0})", currentTitle);
}
command.ExecuteNonQuery(); // Execute insert query against excel file.
}
}
任何解决问题的帮助将不胜感激。 谢谢, 阿弥陀佛
【问题讨论】:
-
如果您的 currentTitle 变量包含逗号怎么办?连接字符串以形成 sql 命令很糟糕,真的很糟糕。使用参数
-
另外,您需要用单引号将字符串括起来以便查询引擎理解它。
-
我的第一个想法和其他人一样,很有可能您的播放列表存在字符串卫生问题。您是否尝试过在调试器中捕获异常,然后检查命令文本以查看这是否是您的问题?