簡體   English   中英

Datagridview值插入到SQL Server CE數據庫

[英]Datagridview values to insert into SQL Server CE database

我有一個問題要獲取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();                    
            }
       }

我得到的錯誤是:

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

有人可以告訴我如何解決這個問題嗎?

謝謝

您可以在循環的每次迭代中添加參數。 您可以將添加參數移動到循環之外,而在循環中,只需設置值即可。 像這樣:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM