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

时间:2014-11-17 04:58:24

标签: c# sql-server datagridview

我有一个问题是将datagridview中的所有值插入到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();                    
            }
       }

我得到的错误是:

  

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

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

谢谢

2 个答案:

答案 0 :(得分:1)

您在循环中的每次迭代中添加参数。您可以在循环外部和循环中添加参数,只需设置值即可。像这样:

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

答案 1 :(得分:0)

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

修订后的代码:

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