仅在第一行插入数据

时间:2014-08-26 14:17:46

标签: c# sql datagridview

我正在使用以下参数化查询将数据插入到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");

2 个答案:

答案 0 :(得分:2)

您不断向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");

答案 1 :(得分:0)

问题在这里for-loop 您正在重复添加参数 在for循环中为参数值添加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");