开发者

Sqlite - Insert gives error - Invalid cast exception - "Invalid cast from 'DateTime' to 'Int32'."

var idParam = new SQLiteParameter("@idParam", SqlDbType.Text) { Value = insertData.ID };
var userIdParam = new SQLiteParameter("@userIdParam", SqlDbType.VarCha开发者_运维百科r) { Value = insertData.Uid };
var applicationNameParam = new SQLiteParameter("@applicationNameParam", SqlDbType.VarChar) { Value = insertData.Application };
var eventNameParam = new SQLiteParameter("@eventNameParam", SqlDbType.VarChar) { Value = insertData.EventName };
var clientTokenParam = new SQLiteParameter("@clientTokenParam", SqlDbType.VarChar) { Value = insertData.ClientToken };
var versionParam = new SQLiteParameter("@versionParam", SqlDbType.VarChar) { Value = insertData.Version };
var dateTimeParam = new SQLiteParameter("@dateTimeParam", SqlDbType.DateTime) { Value = insertData.DateTime };
var dataParam = new SQLiteParameter("@dataParam", SqlDbType.Text) { Value = insertData.Data };

SQLiteCommand command = new SQLiteCommand("INSERT INTO DATAOUT (id, uid, application, eventname, clienttoken, version, datetime, data)" +
    " VALUES ('@idParam','@userIdParam','@applicationNameParam','@eventNameParam','@clientTokenParam','@versionParam',@dateTimeParam,'@dataParam')",Connection);
command.Parameters.Add(idParam);
command.Parameters.Add(userIdParam);
command.Parameters.Add(applicationNameParam);
command.Parameters.Add(eventNameParam);
command.Parameters.Add(clientTokenParam);
command.Parameters.Add(versionParam);
command.Parameters.Add(dateTimeParam);
command.Parameters.Add(dataParam);

command.ExecuteNonQuery();

I am trying to debug this INSERT but I am not having much luck. Why is sqllite trying to cast my datetime to an int?? The database schema is expecting a datetime object. Is there a way to see the insert command text as it is sent to the db?

Here is a look at the Types that I am trying to insert:

public string Uid { get; private set; }
public string Application { get; private set; }
public string EventName { get; private set; }
public string ClientToken { get; private set; }
public string Version { get; private set; }
public DateTime DateTime { get; private set; }
public string Data { get; private set; }

Here is the an example of the insert:

INSERT INTO DATAOUT (id, uid, application, eventname, clienttoken, version, datetime, data) VALUES (' 00000000-0000-0000-0000-000000000000','123abcd','My Application','','Test Client','1.0.0','1/1/2000','[{"id":"alpha_123","question":"ronunciations in pre-classical times or in non-Attic dialects. For det","answer":"nunciations "},{"id":"beta_456","question":"official documents such as laws an","answer":"or modif"},{"id":"gamma_789","question":" maintained or modified slightly to fit Greek phonology; thus, ?","answer":"iation of Attic"},{"id":"delta_098","question":"econstructed pronunciation of Attic in the late 5th an","answer":"unciation of "},{"id":"epsilon_076","question":"erent stylistic variants, with the descending tail either going straight down o","answer":"Whole bunch"},{"id":"zeta_054","question":"rough breathing when it begins a word. Another diacritic use","answer":"other dia"}]')

The last value is a JSON string. The dateTime field is apparently the problem (7th param). For the example above I just added the text '1/1/2011'. The code is constructing the insert via the parameters. The datetime parameter has a valid date in the debugger.

When I inspect the parameter,"dateTimeParam" the debugger shows dbType = Int32. What's up with that?

UPDATE

Removed the quotes around the parameters in the insert statement. It results in the string literals, @paramname to be inserted. Thanks!


My guess is you don't have quotes around @dateTimeParam in query, and query parser assumes it is an int.

What really bugs me though is why you surround parameters by quotes at all.

Try this:

var command = new SQLiteCommand (
   "INSERT INTO DATAOUT (id, uid, application, eventname, clienttoken, version, datetime, data)" +
    " VALUES (@idParam, @userIdParam, @applicationNameParam, @eventNameParam, @clientTokenParam, @versionParam, @dateTimeParam, @dataParam)",
    Connection);

UPDATE

I have no idea what is causing the issue. I tried to mimic this code. Does this help?

var command = new SQLiteCommand (
    "INSERT INTO DATAOUT (id, uid, application, eventname, clienttoken, version, datetime, data)" +
    " VALUES (@idParam,@userIdParam,@applicationNameParam,@eventNameParam,@clientTokenParam,@versionParam,@dateTimeParam,@dataParam)",
    Connection);

command.Parameters.AddWithValue("@idParam", insertData.ID);
command.Parameters.AddWithValue("@userIdParam", insertData.Uid);
command.Parameters.AddWithValue("@applicationNameParam", insertData.Application);
command.Parameters.AddWithValue("@eventNameParam", insertData.EventName);
command.Parameters.AddWithValue("@clientTokenParam", insertData.ClientToken);
command.Parameters.AddWithValue("@versionParam", insertData.Version);
command.Parameters.AddWithValue("@dateTimeParam", insertData.DateTime);
command.Parameters.AddWithValue("@dataParam", insertData.Data);

command.ExecuteNonQuery();


My problem was caused by using the parameter data type of: System.Data.DbType.DateTime2 and the .NET Sqlite api does not recognize this enumeration.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜