C# 要插入SQL Server CE数据库的Datagridview值
我在获取datagridview中的所有值以插入SQL Server CE数据库时遇到问题 以下是我编写的代码:C# 要插入SQL Server CE数据库的Datagridview值,c#,sql-server,datagridview,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.EchonologiaC
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();
}
}
private void btn\u savedp\u单击(对象发送方,事件参数e)
{
//从设置文件中检索连接字符串。
string conString=Properties.Settings.Default.EchonologiaConnectionString;
string sqlQuery=“插入tb_数据点(PierID、InspectDate、DPName、ZoneColumn、ZoneRow、结果、经度、纬度)”;
sqlQuery+=“值(@PierID、@InspectDate、@DPName、@ZoneColumn、@ZoneRow、@Result、@Longitude、@Latitude)”;
//使用连接字符串打开连接。
使用(SqlCeConnection con=新的SqlCeConnection(conString))
{
//插入到SqlCe表中。ExecuteOnQuery最适合插入。
使用(SqlCeCommand com=newsqlcecommand(sqlQuery,con))
{
con.Open();
对于(int i=0;i
我得到的错误是:
此SqlCeParameterCollection已包含具有此名称的SqlCeParameter
有人能告诉我如何纠正这个问题吗
谢谢您在循环中的每次迭代中都添加了参数。您可以在循环外和循环中添加参数,只需设置值即可。类似如下:
//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;
...
}
//循环之前
cms.Parameters.Add();
//循环中
对于(int i=0;i
您必须为网格中的每一行构造一个新命令
修订守则:
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();
}
private void btn\u savedp\u单击(对象发送方,事件参数e)
{
//从设置文件中检索连接字符串。
string conString=Properties.Settings.Default.EchonologiaConnectionString;
string sqlQuery=“插入tb_数据点(PierID、InspectDate、DPName、ZoneColumn、ZoneRow、结果、经度、纬度)”;
sqlQuery+=“值(@PierID、@InspectDate、@DPName、@ZoneColumn、@ZoneRow、@Result、@Longitude、@Latitude)”;
//使用连接字符串打开连接。
使用(SqlCeConnection con=新的SqlCeConnection(conString))
{
con.Open();
//插入到SqlCe表中。ExecuteOnQuery最适合插入。
对于(int i=0;i
Hi,我尝试了您的解决方案,但是对于com.Parameters.Clear()。com.Parameters.Clear();
use aftercom.ExecuteNonQuery();
Hi,我尝试了您的答案,但它显示了一个错误“SqlCeParameterCollection只接受非空的SqlCeParameter类型对象,而不是字符串对象。”.我按照你的建议来解决问题。谢谢你的帮助。