简体   繁体   English

Datagridview值插入到SQL Server CE数据库

[英]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. 我有一个问题要获取datagridview内部的所有值以插入到SQL Server CE数据库中。

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. 具有此名称的SqlCeParameter已包含在此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();  
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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