循环添加SqlCeParameters的实现

Implementation of adding SqlCeParameters in a loop

我正在尝试将字符串数组 dateOperation typeOperation bought 和 sold 中的多个值添加到 table 中,它们具有相同的数组长度

这是我尝试过的核心,它可以工作,但它没有循环

using (SqlCeConnection connection = new SqlCeConnection(connectionString))
{
    SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Operations (date, type, bought, sold) VALUES (@date, @type, @bought, @sold)");
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;

    cmd.Parameters.AddWithValue("@date", dateOperation[0]);
    cmd.Parameters.AddWithValue("@type", typeOperation[0]);
    cmd.Parameters.AddWithValue("@bought", bought[0]);
    cmd.Parameters.AddWithValue("@sold", sold[0]);                    

    connection.Open();
    cmd.ExecuteNonQuery();
}

但是这个带有循环的代码片段并不是我在语法中遗漏了什么

using (SqlCeConnection connection = new SqlCeConnection(connectionString))
{
    SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Operations (date, type, bought, sold) VALUES (@date, @type, @bought, @sold)");
    cmd.CommandType = CommandType.Text;
    cmd.Connection = connection;

    for (int i = 0; i <= bought.Length; i++)
    {
        cmd.Parameters.AddWithValue("@date", dateOperation[i]);
        cmd.Parameters.AddWithValue("@type", typeOperation[i]);
        cmd.Parameters.AddWithValue("@bought", bought[i]);
        cmd.Parameters.AddWithValue("@sold", sold[i]);
    }

    connection.Open();
    cmd.ExecuteNonQuery();
}

因为有时 dateOperations 、 typeOperation 、 bought 和 sold 的数组长度并不总是相同,所以代码可以正常工作,但如果数组长度小于 4,它会给我一个错误 table 可以正常接收数据,但是错误是搞砸了我的程序如何修复它或如何产生异常 "Parameterized query expects a parameter value which was not supplied."

string[] dateOperation = new string[4];
            string[] typeOperation = new string[4];
            string[] bought = new string[4];
            string[] sold = new string[4];

            for (int i = 0; i <= 3; i++)
            {
                if (tb[i].Text != "" && Odate[i].Text != "") 
                {
                    dateOperation[i] = Odate[i].Text.ToString();
                    typeOperation[i] = type[i].Text.ToString();
                    bought[i] = tb[i].Text.ToString();
                    sold[i] = BGN[i].Text.ToString();
                }
            }

            string connectionString = @"Data Source=C:\Users\FluksikartoN\Documents\BFDB.sdf;Password=******";
            using (SqlCeConnection connection = new SqlCeConnection(connectionString))
            {


                    SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Operations (date, type, bought, sold) VALUES (@date, @type, @bought, @sold)");
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = connection;
                    cmd.Parameters.AddWithValue("@date", DbType.String);
                    cmd.Parameters.AddWithValue("@type", DbType.String);
                    cmd.Parameters.AddWithValue("@bought", DbType.String);
                    cmd.Parameters.AddWithValue("@sold", DbType.String);
                    connection.Open();
                    for (int i = 0; i < bought.Length; i++)
                    {
                        cmd.Parameters["@date"].Value = dateOperation[i];
                        cmd.Parameters["@type"].Value = typeOperation[i];
                        cmd.Parameters["@bought"].Value = bought[i];
                        cmd.Parameters["@sold"].Value = sold[i];
                        cmd.ExecuteNonQuery();
                    }

您需要先给命令添加参数(在循环之前)。然后在循环中设置参数的值并执行命令:

SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Operations (date, type, bought, sold) VALUES (@date, @type, @bought, @sold)");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;

cmd.Parameters.Add("@date", DbType.DateTime);
// other parameters

connection.Open();
for (int i = 0; i <= bought.Length; i++)
{
    cmd.Parameters["@date"].Value = dateOperation[i];
    // other parameters
    cmd.ExecuteNonQuery();
}