仅在第一行插入数据

本文关键字:一行 插入 数据 | 更新日期: 2023-09-27 18:19:24

我正在使用以下参数化查询将数据插入datagridview:

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    textBox1.Text = "insert";
    cmd = new SqlCommand();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Connection = con;
    cmd.CommandText = "prcfunddetails";
    cmd.Parameters.AddWithValue("@action", textBox1.Text);
    cmd.Parameters.AddWithValue("@fundid", dataGridView1.Rows[i].Cells[0].Value);
    cmd.Parameters.AddWithValue("@name", dataGridView1.Rows[i].Cells[1].Value);
    cmd.Parameters.AddWithValue("@startdate", dataGridView1.Rows[i].Cells[2].Value);
    cmd.Parameters.AddWithValue("@enddate", dataGridView1.Rows[i].Cells[3].Value);
    cmd.ExecuteNonQuery();
    MessageBox.Show("Records successfully inserted");
}

第一行插入成功,但第二行抛出唯一约束错误(该名称已经存在)。

注意,我在数据库的name列上设置了一个唯一的约束。

上面的代码有什么问题导致这个错误吗?


感谢Hassan Nisar和lboshuizen先生的大力帮助。对不起,但我仍然得到一个相同的错误,我认为我做错了什么。请再帮一次忙。

                textBox1.Text = "insert";
                cmd = new SqlCommand();
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection = con;
                cmd.CommandText = "prcfunddetails";
                cmd.Parameters.Add("@action", SqlDbType.VarChar);
                cmd.Parameters.Add("@fundid", SqlDbType.Int);
                cmd.Parameters.Add("@name", SqlDbType.VarChar);
                cmd.Parameters.Add("@startdate", SqlDbType.DateTime);
                cmd.Parameters.Add("@enddate", SqlDbType.DateTime);
                for (int i = 0; i<dataGridView1.Rows.Count; i++)
                {
                    cmd.Parameters["@action"].Value = textBox1.Text;
                    cmd.Parameters["@fundid"].Value = dataGridView1.Rows[i].Cells[0].Value;
                    cmd.Parameters["@name"].Value = dataGridView1.Rows[i].Cells[1].Value;
                    cmd.Parameters["@startdate"].Value = dataGridView1.Rows[i].Cells[2].Value;
                    cmd.Parameters["@enddate"].Value = dataGridView1.Rows[i].Cells[3].Value;
                    cmd.ExecuteNonQuery();
                }
                MessageBox.Show("Records successfully inserted");

仅在第一行插入数据

你一直在给cmd添加参数。

试试这个:

var cmd = new SqlCommand(con){
 CommandType = CommandType.StoredProcedure,
CommandText = "prcfunddetails"
};
//add the needed parameters to the cmd without the values.
//note: second parameter DbType SHOULD match type of the underlying db-field
cmd.Parameters.Add("@action", SqlDbType.VarChar);
.
.
.
textBox1.Text = "insert"; //I wonder what the effect of "delete" will be :-)
for (var i = 0; i<dataGridView1.Rows.Count; i++)
{
   //just place the correct values on the parameters
   cmd.Parameters["@action"].Value = textBox1.Text;
   cmd.Parameters["@fundid"].Value = dataGridView1.Rows[i].Cells[0].Value;
   //etc//                 
   cmd.ExecuteNonQuery();
}
//Messagebox out of the loop saves a sweet amount of mouseclicks
MessageBox.Show("Records successfully");

问题在这里,for-loop
您正在重复添加参数。
将SqlCommand的参数添加到for循环之外,而将参数值添加到for循环中。

textBox1.Text = "insert";
cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
cmd.CommandText = "prcfunddetails";
cmd.Parameters.Add("@action", SqlDbType.VarChar); //assign valid SqlDbType
cmd.Parameters.Add("@fundid", SqlDbType.VarChar);
//...
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{    
    cmd.Parameters["@action"].Value =  textBox1.Text;
    cmd.Parameters["@fundid"].Value =  dataGridView1.Rows[i].Cells[0].Value;
    //...
    cmd.ExecuteNonQuery();        
 }
 //one for all.... as indicated in other answer.
 MessageBox.Show("Records successfully inserted");