1

In a C# application I've the following SQLiteDatabase:

"CREATE TABLE FOLDER(FolderPath TEXT," + "User TEXT," + "ParentPath TEXT," +
  "TimestampFolder DATETIME DEFAULT CURRENT_TIMESTAMP," + "TimestampParent DATETIME," 
+ "Present TEXT DEFAULT 'True'," + [...] )

During this prepared statement:

SQLiteCommand command = new SQLiteCommand(null, SQLconnection);
command.CommandText = "BEGIN TRANSACTION; "+ "INSERT INTO FOLDER (FolderPath, User, ParentPath, TimestampParent, Present)" + "VALUES (@uCpath, @uname, @pp, @date, @bool)" + "END TRANSACTION;";

SqlParameter unameParam = new SqlParameter("@uname", SqlDbType.Text, 100);
SqlParameter uCpathParam = new SqlParameter("@uCpath", SqlDbType.Text, 300);
SqlParameter uSpathParam = new SqlParameter("@uSpath", SqlDbType.Text, 300);
SqlParameter ppParam = new SqlParameter("@pp", SqlDbType.Text, 100);
SqlParameter dateParam = new SqlParameter("@date", SqlDbType.DateTime);
SqlParameter boolParam = new SqlParameter("@bool", SqlDbType.Text, 100);

unameParam.Value = user.Username;
uCpathParam.Value = user.ClientPath;
uSpathParam.Value = user.ServerPath;
ppParam.Value = null;
dateParam.Value = DateTime.MinValue.ToString("yyyy-MM-dd HH:mm:ss");
boolParam.Value = true.ToString();

command.Parameters.Add(unameParam);
command.Parameters.Add(uCpathParam);
command.Parameters.Add(uSpathParam);
command.Parameters.Add(ppParam);
command.Parameters.Add(dateParam);
command.Parameters.Add(boolParam);

command.Prepare();
command.ExecuteNonQuery();

The following run time exception is generated: Impossible the casting of 'System.Data.SqlClient.SqlParameter' object on 'System.Data.SQLite.SQLiteParameter'type.

I googled and I tried to change SqlDbType.DateTime with SqlDbType.DateTime2 or SqlDbType.Date or SqlDbType.TImestamp as well, but the same exception is launched. Any suggestion? Thanks

1
  • 1
    Change SqlParameter unameParam = ... to SQLiteParameter unameParam = ...... Commented May 2, 2016 at 8:53

1 Answer 1

2

You are mixing classes between different database types.

If you're connecting to a SQLite database, you must use the corresponding classes but you're using SqlParameter which is from the SQL Server class hierarchy.

Simply replace SqlParameter with SQLiteParameter in your code and it should work much better.

Sign up to request clarification or add additional context in comments.

2 Comments

Well done. I've just another related question. If I do so, the error: Error CS1503 Argument 2: cannot convert from 'System.Data.SqlDbType' to 'System.Data.DbType' appeared. So I followed the suggestion and I changed SqlDbType with DbType, but DbType has not the field Text. How can I do? Thanks a lot
SQLite does not have a Text data type, just use string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.