windowsapplication

时间:2016-10-04 07:09:38

标签: c# datagridview ado.net

在我的桌面应用程序中,我将excel工作表导入到datagridview并将这些行插入到数据库中,因为当我打开以查看我的数据库表时,它会插入所有记录。 我的例外是:

  

其他信息:参数化查询   '(@ CurrentCodeAssignment nvarchar(4000),@ EffectiveOctober1 nvarch'   期望参数'@CurrentCodeAssignment',但不是   提供。

我试过的代码:

foreach(DataGridViewRow row in dataGridView1.Rows) {
  SqlConnection con = new SqlConnection("databseconnection");
  string str = "insert into xcelToDB(CurrentCodeAssignment,EffectiveOctober1,PreviousCodeAssignment)Values(@CurrentCodeAssignment,@EffectiveOctober1,@PreviousCodeAssignment)";
  SqlCommand cmd = new SqlCommand(str, con);
  cmd.CommandType = CommandType.Text;
  cmd.Parameters.AddWithValue("@CurrentCodeAssignment", row.Cells["Current code assignment"].Value);
  cmd.Parameters.AddWithValue("@EffectiveOctober1", row.Cells["Effective October 1"].Value);
  cmd.Parameters.AddWithValue("@PreviousCodeAssignment", row.Cells["Previous Code(s) Assignment"].Value);
  con.Open();
  cmd.ExecuteNonQuery();
  con.Close();
}
MessageBox.Show("Records inserted");

任何人都可以告诉我如何做到这一点 谢谢

0 个答案:

没有答案