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

时间:2015-02-21 01:28:39

标签: c# oracle devexpress

我有一个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);
    }
}

然后我在SimpanTes()中调用该方法Button Simpan

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() + "')";

1 个答案:

答案 0 :(得分:0)

您的代码中存在几个问题。您正在循环结束时关闭连接,并在下一次迭代中通过关闭连接获得错误。因此,您必须在执行命令之前打开连接。此外,您需要使用GridView.DataRowCount属性而不是GridView.RowCount,因为GridView.RowCount计算可见行,组行,新行和过滤行。要获得行单元格值,您必须使用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();
}