【发布时间】:2017-09-08 19:15:36
【问题描述】:
我正在开发一个程序,这是我项目的一部分:在这段代码中,我需要从 sqlite3 DB 文件中接收 filename 和 tweet 并将其存储到文件的扩展属性中。所以这是代码:
using Microsoft.WindowsAPICodePack.Shell;
using System;
using System.IO;
using System.Data.SQLite;
class Program
{
public static void Main()
{
var sqName = Path.Combine(Environment.GetFolderPath("~valid path~"));
SQLiteConnection m_dbConnection;
m_dbConnection =
new SQLiteConnection("Data Source="+ sqName + ";Version=3;");
m_dbConnection.Open();
string sql = "SELECT * FROM TweetText";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
reader.Read();
ShellFile.FromFilePath(reader["Filename"]).Properties.System.Comment.Value = reader["Tweet"];
}
}
当我尝试构建它时,VS 2017 说我在 ShellFile.FromFilePath(reader["Filename"]).Properties.System.Comment.Value = reader["Tweet"] 上有错误,错误代码是:
CS1503 参数 1:无法从 'object' 转换为 'string'。
但有趣的是,如果我用如下错误替换该行,它会起作用:
Console.Write(reader["Filename"]);
Console.Write(reader["Tweet"]);
所以我的问题是,为什么 C# 在第一个代码中将 reader["Filename"] 识别为对象,而在第二个代码中将 reader["Filename"] 识别为字符串?我该如何解决这个问题?
【问题讨论】: