Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# sqlite query results to list<string>

Tags:

c#

sqlite

I'm struggling. I have query against my db that returns a single column of data and I need to set it as List. Here is what I am working with and I am getting an error about converting void to string.

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();
        }
    }
}

Then later in application

List<string> ImportedFiles = GetImportedFileList() // Method that gets the list of files from the db 
foreach (string file in files.Where(fl => !ImportedFiles.Contains(fl))) 
like image 461
Jake Sankey Avatar asked May 26 '10 06:05

Jake Sankey


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Why is C named so?

Quote from wikipedia: "A successor to the programming language B, C was originally developed at Bell Labs by Dennis Ritchie between 1972 and 1973 to construct utilities running on Unix." The creators want that everyone "see" his language. So he named it "C".

What is C language?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.


2 Answers

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;
}

Things i've amended in your code:

  • Put ImportedFiles in scope of the entire method.
  • No need to call connect.Close(); since the connection object is wrapped in a using block.
  • Use Convert.ToString rather then (String) as the former will handle all datatype conversions to string. I came across this Here

Edit:

You were creating a new command object sqlComm instead of using fmd that was created by the connection object.

like image 112
Bablo Avatar answered Oct 13 '22 19:10

Bablo


First of all your return type is void. You need to return a List. Another problem is that you initialize the list inside the loop, so in each pass of the loop you have a new list, and whatsmore, you do not add the string in the list. Your code should probably be more like:

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";
                SQLiteDataReader r = fmd.ExecuteReader();
                while (r.Read()) 
                {
                    string FileNames = (string)r["FileName"];

                    ImportedFiles.Add(FileNames);
                }                    

                connect.Close();
        }
    }
    return ImportedFiles;
}
like image 41
Nikos Steiakakis Avatar answered Oct 13 '22 19:10

Nikos Steiakakis