要插入到 SQL Server CE 数据库中的 Datagridview 值

本文关键字:Datagridview 数据库 CE 插入 SQL Server | 更新日期: 2023-09-27 18:30:34

我有一个问题,无法获取数据网格视图中的所有值以插入到SQL Server CE数据库中。

这是我写的代码:

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();                    
            }
       }

我得到的错误是:

具有此名称的 SqlCeParameter 已包含在此 SqlCeParameterCollection 中。

有人可以告诉我如何纠正这个问题吗?

谢谢

要插入到 SQL Server CE 数据库中的 Datagridview 值

在循环中的每次迭代中添加参数。您可以将参数的添加移动到循环之外,并在循环中移动,只需设置值即可。像这样:

//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;
   ...
}

您必须为网格中的每一行构造一个新命令。

修订后的代码:

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();  
        }