简体   繁体   English

Sqlite - Insert给出错误 - 无效的强制转换异常 - “从'DateTime'到'Int32'的无效强制转换。”

[英]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.VarChar) { 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. 我正在尝试调试此INSERT,但我没有太多运气。 Why is sqllite trying to cast my datetime to an int?? 为什么sqllite试图将我的日期时间转换为int? The database schema is expecting a datetime object. 数据库模式需要一个datetime对象。 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"}]') 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”,“问题”:“在经典前时期或在非阁楼方言中发声。对于det”,“回答” “:”nnish“},{”id“:”beta_456“,”问题“:”官方文件,例如法律“,”答案“:”或修改“},{”id“:”gamma_789“,”问题“:”略微保持或修改以适应希腊语音;因此,“回答”:“阁楼”,{“id”:“delta_098”,“问题”:“5号末期的阁楼发音的构建一个“,”答案“:”“},{”id“:”epsilon_076“,”问题“:”不同的风格变体,下降的尾巴要么直接向下“,”回答“:”整群“ },{“id”:“zeta_054”,“问题”:“当它开始一个单词时呼吸粗糙。另一个变音使用”,“回答”:“其他dia”}}')

The last value is a JSON string. 最后一个值是JSON字符串。 The dateTime field is apparently the problem (7th param). dateTime字段显然是问题(第7个参数)。 For the example above I just added the text '1/1/2011'. 对于上面的例子,我刚刚添加了文本'1/1/2011'。 The code is constructing the insert via the parameters. 代码通过参数构造插入。 The datetime parameter has a valid date in the debugger. datetime参数在调试器中具有有效日期。

When I inspect the parameter,"dateTimeParam" the debugger shows dbType = Int32. 当我检查参数“dateTimeParam”时,调试器显示dbType = Int32。 What's up with that? 那是怎么回事?

UPDATE UPDATE

Removed the quotes around the parameters in the insert statement. 删除了insert语句中参数周围的引号。 It results in the string literals, @paramname to be inserted. 它导致插入字符串文字@paramname。 Thanks! 谢谢!

My guess is you don't have quotes around @dateTimeParam in query, and query parser assumes it is an int . 我的猜测是你在查询中没有@dateTimeParam引号,而查询解析器假设它是一个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 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();

我的问题是由使用参数数据类型:System.Data.DbType.DateTime2引起的,而.NET Sqlite api无法识别此枚举。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM