如何将gridcontrol devexpress中的数据保存到oracle数据库中?

How to save data from gridcontrol devexpress into oracle database?

我有一个 GridControl 数据,我在 C# 中从 TextEdit 插入数据,然后我想将这些数据保存到 oracle 数据库,这是我的场景:

private void fmTest_Load(object sender, EventArgs e)
{
    dt = new DataTable();
    DataColumn dc1 = new DataColumn("NAMA");
    DataColumn dc2 = new DataColumn("ALAMAT");
    dt.Columns.Add(dc1);
    dt.Columns.Add(dc2);
    DataRow dr1 = dt.NewRow();
    gridControl1.DataSource = dt;
}

这是我将数据从 TextEdit 保存到 Grid 的方法:

public void SimpanGrid()
{
    DataRow dr1 = dt.NewRow();
    dr1[0] = txtNama.Text;
    dr1[1] = txtAlamat.Text;
    dt.Rows.Add(dr1);
    gridControl1.DataSource = dt;
}

此方法用于保存来自 GridControl 的数据,但我仍然遇到错误:

public void SimpanTest()
{
    //membuat koneksi_manual
    if (koneksi_manual.con.State == ConnectionState.Open)
    {
        koneksi_manual.con.Close();
    }
    koneksi_manual.con.Open();

    try
    {
        for (int i = 0; i < gridView1.RowCount - 1; i++)
        {

            OracleCommand cmd = new OracleCommand();

            cmd.CommandText = @"INSERT INTO TEST (NAMA, ALAMAT) 
                    VALUES ('" + gridView1.Columns[i].FieldName.ToString() +
                          "', '" + gridView1.Columns[i].FieldName.ToString() + "')";

            cmd.Connection = koneksi_manual.con;
            cmd.ExecuteNonQuery(); //eror disini
            MessageBox.Show("DATA TELAH DISIMPAN");
            koneksi_manual.con.Close();
        }
    }
    catch (Exception ex)
    {
        // Memunculkan pesan error
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

然后我在 Button Simpan:

中调用那个方法 SimpanTes()
private void btnSimpan_Click_1(object sender, EventArgs e)
{
    SimpanTest();
}

所以,这是我无法将数据保存到数据库的问题,我不知道我必须使用什么正确的代码。

cmd.CommandText = @"INSERT INTO TEST (NAMA, ALAMAT) 
                            VALUES ('" + gridView1.Columns[i].FieldName.ToString() +
                                  "', '" + gridView1.Columns[i].FieldName.ToString() + "')";

您的代码中存在几个问题。您将在循环结束时关闭连接,并在下一次迭代中收到关闭连接的错误。因此,您必须在执行命令之前打开连接。您还需要使用 GridView.DataRowCount property instead of GridView.RowCount because GridView.RowCount counts visible rows, group rows, new row and filter row. And to get the row cell value you must use GridView.GetRowCellValue 方法。
这是示例:

if (koneksi_manual.con.State == ConnectionState.Open)    
   koneksi_manual.con.Close();

for (int i = 0; i < gridView1.DataRowCount - 1; i++)// <= Use gridView1.DataRowCount instead of gridView1.RowCount.
{    
    OracleCommand cmd = new OracleCommand();

    cmd.CommandText = @"INSERT INTO TEST (NAMA, ALAMAT) 
            VALUES ('" + gridView1.GetRowCellValue(i, "NAMA") +
                  "', '" + gridView1.GetRowCellValue(i, "ALAMAT") + "')";// <= Use gridView1.GetRowCellValue to get the cell value.

    cmd.Connection = koneksi_manual.con;

    koneksi_manual.con.Open();// <= Open connection before executing the command.

    cmd.ExecuteNonQuery(); //eror disini
    MessageBox.Show("DATA TELAH DISIMPAN");
    koneksi_manual.con.Close();
}

但您也可以直接使用 dt 对象来保存其中的值:

if (koneksi_manual.con.State == ConnectionState.Open)    
   koneksi_manual.con.Close();

foreach (DataRow dataRow in dt.Rows)
{    
    OracleCommand cmd = new OracleCommand();

    cmd.CommandText = @"INSERT INTO TEST (NAMA, ALAMAT) 
            VALUES ('" + dataRow["NAMA"] +
                  "', '" + dataRow["ALAMAT"] + "')";

    cmd.Connection = koneksi_manual.con;

    koneksi_manual.con.Open();// <= Open connection before executing the command.

    cmd.ExecuteNonQuery(); //eror disini
    MessageBox.Show("DATA TELAH DISIMPAN");
    koneksi_manual.con.Close();
}