wildBlue
wildBlue

Reputation: 19

Illegal Characters in SQlite Connection (C#)

I'm new to SQL/SQLite, but when I try to run this method to search an SQlite DB, it fails on con.Open() saying "System Argument Exception: Illegal Characters in Path". I have tried removing the space between Data and Source and I have tried making the C: lower case as well. I am using SQLite found in Nuget Package Manager

private void searchDB(string search1, string search2)
{
    //build connection to sqlite
    string cs = "Data Source=C:\flowData.db;Version=3;";
    using (SQLiteConnection con = new SQLiteConnection(cs))
    {
        con.Open();

        string stm = "SELECT * FROM  locationData";

        using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
        {
            using (SQLiteDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    string poll = rdr["FacilityName"].ToString();
                    if (poll.Contains(search1) || poll.Contains(search2))
                    {
                        searchResult.Items.Add(poll);
                    }
                }
            }
        }
        con.Close();
    }
}

Error: Error

Upvotes: 1

Views: 547

Answers (1)

ChaosPandion
ChaosPandion

Reputation: 78262

It looks like you need to escape the backslash.

string cs = "Data Source=C:\\flowData.db;Version=3;";

You can also use the verbatim string

string cs = @"Data Source=C:\flowData.db;Version=3;";

The original string has a \f character in it which is an illegal path character.

Upvotes: 1

Related Questions