使用带有insert语句的集合初始化程序

时间:2013-04-29 12:05:55

标签: c# resharper collection-initializer

我有以下method

public bool InsertUsername(string username)
{
    string SQL = "Insert into [Users](Username, InsertDateTime) Values(@Username, datetime('now'));";
    List<SQLiteParameter> pars = new List<SQLiteParameter>();
    pars.Add(new SQLiteParameter("@Username", username));
    return SQLiteUsernameDatabase.ExecuteNonQuery(SQL, pars);
}

ReSharper告诉我使用collection initializer代码List

问题:如何将SQLiteParameter添加到集合初始值设定项中?

1 个答案:

答案 0 :(得分:1)

没关系,找到了解决方案:

public bool InsertUsername(string username)
{
    string SQL = "Insert into [Users](Username, InsertDateTime) Values(@Username, datetime('now'));";
    var pars = new List<SQLiteParameter> {new SQLiteParameter("@Username", username)};
    return SQLiteUsernameDatabase.ExecuteNonQuery(SQL, pars);
}