C# SQLITE - 如何通过数组或列表传递绑定参数?

C# SQLITE - How to pass bound parameters via an array or list?

我正在使用 c# 和 sqlite,我希望能够将一组绑定参数传递给一个函数,以便我可以通过该函数插入或更新。

本质上,我想做我通常在 PHP 中使用 PDO 做的事情,并使用 ? 绑定参数。并在我执行语句时传递一个数组,它会按顺序附加它们。但是我不知道如何在 C# 和 sqlite 中做类似的事情。

(我意识到我的设置可能有错误或效率低下,甚至有一般编码错误。任何有关如何正确设置的示例,尤其是完整的工作示例将不胜感激。)

我想做的看起来像这样:

List<string> sqlParameters = new List<string>();
sqlParameters.Add("Red");
sqlParameters.Add("Blue");
sqlParameters.Add("Green");
updateDatabase("update table set column1 = @column1, column2= @column2 where column3 = @column3", sqlParameters);

int updateDatabase(string sql, List<string> sqlParameters)
{
    try 
    {
        dbConnection = new SQLiteConnection("Data Source=database.sqlite;Version=3;FailIfMissing=True");
        dbConnection.Open();
        sqlcommand.Prepare();

        // I want to switch this next line somehow with something that allows me to simply 
        // pass the bound parameters list or array all at once and it attaches them in 
        // order. Similar to PDO using ? and passing an array in PHP.
        sqlcommand.Parameters.AddWithValue("@column1", sqlParameters);
        SQLiteCommand command = new SQLiteCommand(sql, dbConnection);
        int affectedRows = command.ExecuteNonQuery();   
        dbConnection.Close();       
        return affectedRows;
    } 
    catch (Exception e) 
    {
        MessageBox.Show(e.ToString());
    }

    return 0;
}

----------------编辑 - PookyFan 帮助后的结果:----------------

万一有人想用这个,我的最终函数看起来像这样:

List<string> sqlParameters = new List<string>();
sqlParameters.Add("red");
sqlParameters.Add("blue");

updateDatabase("insert or replace into chattracking (column1, column2) VALUES (@param0, @param1)",sqlParameters);




int updateDatabase(string sql, List<string> sqlParameters)
        {
            try {
                dbConnection =
                new SQLiteConnection("Data Source=databasename.sqlite;Version=3;FailIfMissing=True");
                dbConnection.Open();
                SQLiteCommand command = new SQLiteCommand(sql, dbConnection);
                command.Prepare();

                for (int i = 0; i < sqlParameters.Count; i++) {
                    command.Parameters.AddWithValue(("@param" + i.ToString()), sqlParameters[i]);
                }                   

                int affectedRows = command.ExecuteNonQuery();               

                dbConnection.Close();

                return affectedRows;


            } catch (Exception e) {

                MessageBox.Show(e.ToString());
            }

            return 0;
        }

是这样的吗?

for(int i = 0; i < sqlParameters.Count; ++i)
    sqlcommand.Parameters.AddWithValue("@column" + i.ToString(), sqlParameters[i]);