【问题标题】:Inserting data grid view selected rows into database using loop使用循环将数据网格视图选定的行插入数据库
【发布时间】:2018-03-15 17:45:12
【问题描述】:

我有一些数据应该插入到 4 个表中,其中一个数据存储在数据网格视图选定的行中,所以我将循环通过数据网格视图选定的行将它们插入数据库我还使用了一种方法执行事务并将 SqlCommands 作为矩阵,然后一一进行,但是当我尝试插入数据时,它给了我异常“索引超出范围。必须是非负数并且小于集合的大小。”在这一行

cm[i].Parameters.Add("@car", Convert.ToInt32(newCarAdCarDgv.Rows[i].Cells[0].Value.ToString()));  

这是交易方式:

public void ExTr(SqlCommand[] cm)
{
    if (cn.State == ConnectionState.Closed)
    {
        cn.Open();
    }
    SqlTransaction tr = cn.BeginTransaction();
    for (int x = 0; x < cm.Length; x++)
    {
        cm[x].Connection = cn;
        cm[x].Transaction = tr;

    }

    try
    {
        for (int x = 0; x < cm.Length; x++)
        {
            cm[x].ExecuteNonQuery();
        }
        tr.Commit();
        MessageBox.Show("تمت عملية إضافة البيانات بنجاح");
    }
    catch (SqlException ex)
    {
        tr.Rollback();
        MessageBox.Show(ex.Message);

    }
    finally
    {
        if (cn.State == ConnectionState.Open)
        {
            cn.Close();
        }
    }

}

这是插入代码:

private void newAdSaveBtn_Click(object sender, EventArgs e)
{
    try
    {
        count = newCarAdCarDgv.SelectedRows.Count + 3;
        if (cn.State == ConnectionState.Closed)
        {
            cn.Open();
        }

        newCarAdClientPhoneTxt_Leave(sender, e);
        newCarAdClientEmailTxt_Leave(sender, e);
        newCarAdNotesTxt_Leave(sender, e);

        SqlCommand[] cm = new SqlCommand[count];

        cm[0] = new SqlCommand("insert into clientData (Id,clientName,clientWork,clientPhone,clientMobile,clientEmail) values (@id,@name,@work,@phone,@mobile,@email)", cn);
        cm[0].Parameters.AddWithValue("@id", id);
        cm[0].Parameters.AddWithValue("@name", newCarAdClientNameTxt.Text.Trim());
        cm[0].Parameters.AddWithValue("@work", newCarAdClientWorkTxt.Text.Trim());
        cm[0].Parameters.AddWithValue("@phone", newCarAdClientPhoneTxt.Text.Trim());
        cm[0].Parameters.AddWithValue("@mobile", newCarAdClientMobileTxt.Text.Trim());
        cm[0].Parameters.AddWithValue("@email", newCarAdClientEmailTxt.Text.Trim());

        cm[1] = new SqlCommand("insert into marketingData (m_Id,marketingDurations,marketingStartsFrom,marketingEndsIn,notes,adDate) values (@id,@durations,@start,@end,@notes,@date)", cn);
        cm[1].Parameters.AddWithValue("@id", id);
        cm[1].Parameters.AddWithValue("@durations", newCarAdAdDurationTxt.Text.Trim());
        cm[1].Parameters.AddWithValue("@start", newCarAdStartDayDtp.Value);
        cm[1].Parameters.AddWithValue("@end", newCarAdEndDayDtp.Value);
        cm[1].Parameters.AddWithValue("@notes", newCarAdNotesTxt.Text.Trim());
        cm[1].Parameters.AddWithValue("@date", newCarAdDateDtp.Value);

        cm[2] = new SqlCommand("insert into priceAndProfits (p_Id,marketingCost,marketingPrice,marketingProfit,dollarPrice) values (@id,@cost,@price,@profit,@dollar)", cn);
        cm[2].Parameters.AddWithValue("@id", id);
        cm[2].Parameters.AddWithValue("@cost", newCarAdCostTxt.Text.Trim());
        cm[2].Parameters.AddWithValue("@price", newCarAdPriceTxt.Text.Trim());
        cm[2].Parameters.AddWithValue("@profit", newCarAdProfitTxt.Text.Trim());
        cm[2].Parameters.AddWithValue("@dollar", newCarAdDollarPriceTxt.Text.Trim());

        for (int i = 3; i <= newCarAdCarDgv.SelectedRows.Count + 3; i++)
        {
            cm[i] = new SqlCommand("insert into carWorkCount (c_Id,carId) value (@id,@car)", cn);
            cm[i].Parameters.Add("@id", id);
            cm[i].Parameters.Add("@car", Convert.ToInt32(newCarAdCarDgv.Rows[i].Cells[0].Value.ToString()));
        }

        ExTr(cm);

    }
    catch (SqlException ex)
    {
        MessageBox.Show(ex.Message);
    }
}

【问题讨论】:

    标签: c# sql-server winforms datagridview insert


    【解决方案1】:

    将最终循环改为

    for (int i = 0; i < newCarAdCarDgv.SelectedRows.Count; i++)
    {
        cm[i+3] = new SqlCommand("insert into carWorkCount (c_Id,carId) value (@id,@car)", cn);
        cm[i+3].Parameters.Add("@id", id);
        cm[i+3].Parameters.Add("@car", 
          Convert.ToInt32(newCarAdCarDgv.SelectedRows[i].Cells[0].Value.ToString()));
    }
    

    您的代码引用 Rows[i] 但 i 以值 3 开头,如果您选择网格中的所有行,则可以超出行数。相反,您只需要使用 i+3 引用命令

    【讨论】:

    • 它给了我“索引超出了数组范围”
    • 你在哪一行得到了异常?
    猜你喜欢
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 2020-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 1970-01-01
    相关资源
    最近更新 更多