简体   繁体   中英

Datagridview values to insert into SQL Server CE database

I have a problem to get all the values inside the datagridview to insert into a SQL Server CE database.

Here is the code I wrote:

private void btn_savedp_Click(object sender, EventArgs e)
{
        // Retrieve the connection string from the settings file.
        string conString = Properties.Settings.Default.EchonologiaConnectionString;

        string sqlQuery = "INSERT INTO tb_DataPoint (PierID, InspectDate, DPName, ZoneColumn, ZoneRow, Result, Longitude, Latitude)";
        sqlQuery += "VALUES (@PierID, @InspectDate, @DPName, @ZoneColumn, @ZoneRow, @Result, @Longitude, @Latitude)";

        // Open the connection using the connection string.
        using (SqlCeConnection con = new SqlCeConnection(conString))
        {
            // Insert into the SqlCe table. ExecuteNonQuery is best for inserts.
            using (SqlCeCommand com = new SqlCeCommand(sqlQuery, con))
            {
                con.Open();

                for (int i = 0; i < dgDataPoint.Rows.Count; i++)
                {
                    com.Parameters.AddWithValue("DPName", dgDataPoint.Rows[i].Cells["DataPointName"].Value);
                    com.Parameters.AddWithValue("ZoneColumn", dgDataPoint.Rows[i].Cells["ZoneColumn"].Value);
                    com.Parameters.AddWithValue("ZoneRow", dgDataPoint.Rows[i].Cells["ZoneRow"].Value);
                    com.Parameters.AddWithValue("Result", dgDataPoint.Rows[i].Cells["Result"].Value);
                    com.Parameters.AddWithValue("Longitude", dgDataPoint.Rows[i].Cells["Longitude"].Value);
                    com.Parameters.AddWithValue("Latitude", dgDataPoint.Rows[i].Cells["Latitude"].Value);
                    com.Parameters.AddWithValue("PierID", cbPier.Text);
                    com.Parameters.AddWithValue("InspectDate", dtInspect.Text);
                }

                com.ExecuteNonQuery();
                com.Parameters.Clear();            
                con.Close();                    
            }
       }

There error I get is:

The SqlCeParameter with this name is already contained by this SqlCeParameterCollection.

Can someone show me how to rectify this problem?

Thank you

You adding parameters on each iteration in your loop. You can move adding of parameters outside of loop and in the loop, just set the values. Something like this:

//Before loop
cms.Parameters.Add(<parameterNameHere>);

// In the loop
 for (int i = 0; i < dgDataPoint.Rows.Count; i++)
 {
   com.Parameters["DPName"]= 
       dgDataPoint.Rows[i].Cells["DataPointName"].Value;
   ...
}

You have to construct a new command for each row in the grid.

The revised code:

private void btn_savedp_Click(object sender, EventArgs e)
    {

        // Retrieve the connection string from the settings file.
        string conString = Properties.Settings.Default.EchonologiaConnectionString;

        string sqlQuery = "INSERT INTO tb_DataPoint (PierID, InspectDate, DPName, ZoneColumn, ZoneRow, Result, Longitude, Latitude)";
        sqlQuery += "VALUES (@PierID, @InspectDate, @DPName, @ZoneColumn, @ZoneRow, @Result, @Longitude, @Latitude)";


        // Open the connection using the connection string.
        using (SqlCeConnection con = new SqlCeConnection(conString))
        {
                con.Open();
            // Insert into the SqlCe table. ExecuteNonQuery is best for inserts.
                for (int i = 0; i < dgDataPoint.Rows.Count; i++)
                {
                     using (SqlCeCommand com = new SqlCeCommand(sqlQuery, con))
                     {

                         com.Parameters.AddWithValue("DPName", dgDataPoint.Rows[i].Cells["DataPointName"].Value);
                         com.Parameters.AddWithValue("ZoneColumn", dgDataPoint.Rows[i].Cells["ZoneColumn"].Value);
                         com.Parameters.AddWithValue("ZoneRow", dgDataPoint.Rows[i].Cells["ZoneRow"].Value);
                         com.Parameters.AddWithValue("Result", dgDataPoint.Rows[i].Cells["Result"].Value);
                         com.Parameters.AddWithValue("Longitude", dgDataPoint.Rows[i].Cells["Longitude"].Value);
                         com.Parameters.AddWithValue("Latitude", dgDataPoint.Rows[i].Cells["Latitude"].Value);
                         com.Parameters.AddWithValue("PierID", cbPier.Text);
                         com.Parameters.AddWithValue("InspectDate", dtInspect.Text);

                         com.ExecuteNonQuery();

                         com.Parameters.Clear();            
                   }

                }

            con.Close();  
        }

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