我很新C#
and SQLite
数据库并有一些变量存储在 SQLite 数据库中TimeStamp
。
这是我的代码:
DateTime now = DateTime.Now;
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
var sql = string.Format("insert into Table (Timestamp) values ({0})", now);
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
但我收到错误。我猜,SQLite不支持DateTime
。我尝试转换DateTime now
to string
但它仍然不起作用。
DateTime now1 = DateTime.Now;
var now = now1.ToString("hh.mm.ss.fff");
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
var sql = string.Format("insert into Table (Timestamp) values ({0})", now);
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
难道我做错了什么?
使用参数避免SQL注入:
var sql = string.Format("insert into Table (Timestamp) values (@now)", now);
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.Parameters.AddWithValue("@now",now);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)