sql中的DateTime在不同的本地化中

时间:2011-12-27 17:20:08

标签: c# sqlite

在sqllite我有约会,我有一个查询:

INSERT INTO stats_project_funds(id_project, id_funds, month,stake, stake_in_units,  stake_in_percent, profit, profit_in_units, picks, hitrate, won, draw, lost)  
VALUES(1, 5, strftime("%Y-%m",'27.12.2011'), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); 

如您所见,日期为俄文格式。我的程序是用欧洲本地化编写的,日期格式是27-12-2011。我需要更改此查询将适用于所有本地化?现在数据库中的这个字段是数据时间类型。更好的解决方案是将其更改为varchar并插入字符串值?在字段中仅存储年份和月份。

我的查询如下:

DateTime newMonth = DateTime.Parse(month);
            StringBuilder query = new StringBuilder();
            query.AppendFormat(" INSERT INTO stats_project_funds(id_project, id_funds, month,stake, stake_in_units, "
                + " stake_in_percent, profit, profit_in_units, picks, hitrate, won, draw, lost) "
                + " VALUES({0}, {1}, strftime(\"%Y-%m\",'{2}'), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); ", idProject, idFunds, newMonth.Date.ToShortDateString());
            return query.ToString();

由于

1 个答案:

答案 0 :(得分:3)

我会使用参数而不是String Builder,它更干净,更安全,更不容易出错。这样,您就不必担心日期格式,这是使用参数的首选方式。

IDbCommand db = 
    new SqlCommand("insert into table (myDateField) values (@myDateParam)");
db.Parameters.Add(new SqlParameter("@myDateParam", DateTime.Now));