我正在挣扎.我查询了我的db,它返回一列数据,我需要将其设置为List.这是我正在使用的,我收到一个关于将void转换为字符串的错误.
public static void GetImportedFileList()
{
using (SQLiteConnection connect = new SQLiteConnection(@"Data Source=C:\Documents and Settings\js91162\Desktop\CMMData.db3"))
{
connect.Open();
using (SQLiteCommand fmd = connect.CreateCommand())
{
SQLiteCommand sqlComm = new SQLiteCommand(@"SELECT DISTINCT FileName FROM Import");
SQLiteDataReader r = sqlComm.ExecuteReader();
while (r.Read())
{
string FileNames = (string)r["FileName"];
List<string> ImportedFiles = new List<string>();
}
connect.Close();
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后在申请中
List<string> ImportedFiles = GetImportedFileList() // Method that gets the list of files from the db
foreach (string file in files.Where(fl => !ImportedFiles.Contains(fl)))
Run Code Online (Sandbox Code Playgroud)
小智 29
public static List<string> GetImportedFileList(){
List<string> ImportedFiles = new List<string>();
using (SQLiteConnection connect = new SQLiteConnection(@"Data Source=C:\Documents and Settings\js91162\Desktop\CMMData.db3")){
connect.Open();
using (SQLiteCommand fmd = connect.CreateCommand()){
fmd.CommandText = @"SELECT DISTINCT FileName FROM Import";
fmd.CommandType = CommandType.Text;
SQLiteDataReader r = fmd.ExecuteReader();
while (r.Read()){
ImportedFiles.Add(Convert.ToString(r["FileName"]));
}
}
}
return ImportedFiles;
}
Run Code Online (Sandbox Code Playgroud)
我在你的代码中修改过的东西:
ImportedFiles整个方法的范围内.connect.Close();因为连接对象包含在using块中.Convert.ToString而不是(String)前者将处理所有数据类型转换为字符串.我在这里遇到过这个编辑:
您正在创建一个新的命令对象,sqlComm而不是使用fmd由连接对象创建的命令对象.