我正在尝试将数据从 datagridview 移动到 Oracle 数据库。datagridview 数据集填充了来自 access 数据库表的值,该表有 13 列,每列具有不同的数据类型。我有两个按钮,一个用于填充 datagridview “工作正常”,另一个按钮与以下代码相关,应该将数据从 datagridview 移动到 Oracle 数据库。请注意,我之前从仅包含数字的简单表中尝试过此代码,但是当我尝试执行此代码时,可能会出现异常:“值不在预期范围内”不断弹出。请帮助我。我做这个是为了任务!!任何帮助将非常感激。
private void button3_Click(object sender, EventArgs e)
{
ConnOra = new OracleConnection(oradb);
ConnOra.Open();
String OraQuery = "Insert into availability(availid, nurseid, a_Date, a_start, a_end, anyearly, anymiddle, anylate, anynight, allocated, towhat, status, anyw, anyx, anyy, anyz) values (:availid, :nurseid, to_char(to_date(:a_date,'MM/DD/YYYY'), 'YYYY-MM-DD'), to_char(to_date(:a_start,'MM/DD/YYYY'), 'YYYY-MM-DD'), to_char(to_date(:a_end,'MM/DD/YYYY'), 'YYYY-MM-DD'), :anyearly, :anymiddle, :anylate, :anynight, :allocated, :towhat, :status, :anyw, :anyx, :anyy, :anyz)";
try
{
using (OracleCommand cmd = new OracleCommand(OraQuery, ConnOra))
{
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
for (int j = 0; j < dataGridView1.Rows.Count; j++)
{
cmd.Parameters.Add(new OracleParameter(":availid", dataGridView1.Rows[j].Cells["availid"].Value));
cmd.Parameters.Add(new OracleParameter(":nurseid", dataGridView1.Rows[j].Cells["nurseid"].Value));
cmd.Parameters.Add(new OracleParameter(":a_Date", dataGridView1.Rows[j].Cells["date"].Value));
cmd.Parameters.Add(new OracleParameter(":a_start", dataGridView1.Rows[j].Cells["start"].Value));
cmd.Parameters.Add(new OracleParameter(":a_end", dataGridView1.Rows[j].Cells["end"].Value));
cmd.Parameters.Add(new OracleParameter(":anyearly", dataGridView1.Rows[j].Cells["anyearly"].Value));
cmd.Parameters.Add(new OracleParameter(":anymiddle", dataGridView1.Rows[j].Cells["anymiddle"].Value));
cmd.Parameters.Add(new OracleParameter(":anylate", dataGridView1.Rows[j].Cells["anylate"].Value));
cmd.Parameters.Add(new OracleParameter(":anynight", dataGridView1.Rows[j].Cells["anynight"].Value));
cmd.Parameters.Add(new OracleParameter(":allocated", dataGridView1.Rows[j].Cells["allocated"].Value));
cmd.Parameters.Add(new OracleParameter(":towhat", dataGridView1.Rows[j].Cells["towhat"].Value));
cmd.Parameters.Add(new OracleParameter(":status", dataGridView1.Rows[j].Cells["status"].Value));
cmd.Parameters.Add(new OracleParameter(":anyw", dataGridView1.Rows[j].Cells["anyw"].Value));
cmd.Parameters.Add(new OracleParameter(":anyx", dataGridView1.Rows[j].Cells["anyx"].Value));
cmd.Parameters.Add(new OracleParameter(":anyy", dataGridView1.Rows[j].Cells["anyy"].Value));
cmd.Parameters.Add(new OracleParameter(":anyz", dataGridView1.Rows[j].Cells["anyz"].Value));
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
MessageBox.Show("Success");
ConnOra.Close();
ConnOra.Dispose();
}
}
catch (Exception ex)
{ MessageBox.Show(ex.Message); }
}