简体   繁体   中英

How to add multiple records into a SQL Server table from a gridview?

I got a csv file and I'm inserting it to a gridview, then displaying it. After that, I need to store it into the database, all the records from the gridview.

So on button click I'm using the following code :

using (SqlConnection scn = new SqlConnection("Data Source=(local); Database='WaselProject'; Integrated Security = yes;"))
{
    scn.Open();
    string query = "INSERT INTO Wasel_Card (RECORD, CARD_ID, FULL_NAME, SERVICE_ID, PROC_COUNT, READ_COUNT) VALUES (@record, @cardid, @fullname, @serviceid, @proccount, @readcount)";

    using (SqlCommand scm = new SqlCommand(query, scn))
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            scm.Parameters.AddWithValue("@record", dataGridView1.Rows[i].Cells["Record"].Value.ToString());
            scm.Parameters.AddWithValue("@cardid", dataGridView1.Rows[i].Cells["CARD_ID"].Value.ToString());
            scm.Parameters.AddWithValue("@fullname", dataGridView1.Rows[i].Cells["FULL_NAME"].Value.ToString());
            scm.Parameters.AddWithValue("@serviceid", dataGridView1.Rows[i].Cells["SERVICE_ID"].Value.ToString());
            scm.Parameters.AddWithValue("@proccount", dataGridView1.Rows[i].Cells["PROC_COUNT"].Value.ToString());
            scm.Parameters.AddWithValue("@readcount", dataGridView1.Rows[i].Cells["READ_COUNT"].Value.ToString());

            scm.ExecuteNonQuery();
        }
    }
}

but I get an error:

Additional information: The variable name '@record' has already been declared. Variable names must be unique within a query batch or stored procedure.

Any help?

Thanks in advance.

Your issue is that you keep defining the parameters inside the loop and obviously the same parameter cannot be defined many times for the same command. Two ways to fix your issue.

Method 1

Call Clear() on your parameters within the loop.

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    scm.Parameters.AddWithValue("@record", dataGridView1.Rows[i].Cells["Record"].Value.ToString());
    scm.Parameters.AddWithValue("@cardid", dataGridView1.Rows[i].Cells["CARD_ID"].Value.ToString());
    scm.Parameters.AddWithValue("@fullname", dataGridView1.Rows[i].Cells["FULL_NAME"].Value.ToString());
    scm.Parameters.AddWithValue("@serviceid", dataGridView1.Rows[i].Cells["SERVICE_ID"].Value.ToString());
    scm.Parameters.AddWithValue("@proccount", dataGridView1.Rows[i].Cells["PROC_COUNT"].Value.ToString());
    scm.Parameters.AddWithValue("@readcount", dataGridView1.Rows[i].Cells["READ_COUNT"].Value.ToString());
    scm.ExecuteNonQuery();
    scm.Parameters.Clear();
}

Method 2

Only add the values within the loop but declare your parameters once outside the loop.

// Define here just once
var paramRecord = scm.Parameters.Add("@record", System.Data.SqlDbType.Variant ); // There are 6 Add methods in total. 5 of them return SqlParameter so you can use any of those 5
// the other parameters
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    paramRecord.Value = dataGridView1.Rows[i].Cells["Record"].Value.ToString());
    // other parameter values here
    scm.ExecuteNonQuery();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM