简体   繁体   English

使用C#将数据从数据集移动到Oracle数据库

[英]Moving Data from DataSet to Oracle Database using C#

I've been squeezing my mind trying to figure out why my code isn't working. 我一直想弄清楚为什么我的代码无法正常工作。 I am trying to read from a DataSet that filled with data from access database and insert the data into an Oracle database which is created previously. 我正在尝试从充满访问数据库中数据的数据集中读取数据,并将数据插入到先前创建的Oracle数据库中。 When I try the following code it won't work and although I use the try and catch block, when debugging it would freeze and won't show me any error. 当我尝试以下代码时,它将无法正常工作,尽管我使用了try and catch块,但在调试时它将冻结,并且不会显示任何错误。 if can see that I have commented out the block just right above my foreach loop..which works perfectly, Any help from you is so much appreciated : 如果可以看到我已经在我的foreach循环正上方注释掉了该块。.它非常完美,非常感谢您的任何帮助:

     private void button3_Click(object sender, EventArgs e)
    {
        string query1 = "Select * from Test;";
        string StrQuery= "Insert Into TEST (ID, DATA) Values (:ID, :DATA)";

        Conn = new OleDbConnection(connStr);
        Conn.Open();
        using (OleDbConnection connection1 = new OleDbConnection(connStr))
        {
            using (OleDbDataAdapter adapter1 = new OleDbDataAdapter(query1, connection1))
            {
                DataSet ds1 = new DataSet();
                adapter1.Fill(ds1);
               // no need for refilling DataGridView1.DataSource = ds.Tables[0]

               // insterting the dataset into oracle
                try
                {
                    using (OracleConnection connect = new OracleConnection(oradb1))
                    {
                        connect.Open();
                        using (OracleCommand comma = new OracleCommand(StrQuery, connect))
                        {


                                /*comma.Parameters.Add(new OracleParameter(":ID", 2));
                                comma.Parameters.Add(new OracleParameter(":DATA", 2));
                                comma.ExecuteNonQuery();*/
                            foreach (DataRow drRow in ds1.Tables[0].Rows)
                            {
                                for (int i = 0; i < ds1.Tables[0].Columns.Count; i++)
                                {
                                    comma.Parameters.Add(new OracleParameter(":ID", drRow[i]));
                                    comma.Parameters.Add(new OracleParameter(":DATA", drRow[i]));
                                    comma.ExecuteNonQuery();
                                }
                            }


                            connect.Close();
                            connect.Dispose();
                        }
                    }

                }

                catch (OracleException)
                {
                    System.Diagnostics.Debugger.Break();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
        }

        Conn.Close();
    }

You are looping columns but adding the drRow[i] as values in the parameters. 您正在循环列,但是将drRow [i]作为值添加到参数中。 I do not think this is what you intended. 我认为这不是您想要的。

skip the columns loop and add the first column value to id and second column value to data. 跳过column循环,并将第一列值添加到id,将第二列值添加到data。 that should be what you wanted.... if not then describe a bit more... 那应该就是你想要的。

Expanding on Judgemaik's answer, I believe you need to do something like this instead (can't really tell what the names of the columns in your access table are but you get the idea: 扩展Judgemaik的答案,我相信您需要做这样的事情(实际上并不能说出访问表中列的名称是什么,但是您可以理解:

foreach (DataRow drRow in ds1.Tables[0].Rows)
{
    comma.Parameters.Add(new OracleParameter(":ID", drRow["IDColumnFromAccessDB"]));
    comma.Parameters.Add(new OracleParameter(":DATA", drRow["DATAColumnFromAccessDB"]));
    comma.ExecuteNonQuery();
}

A similar approach is outlined in my answer here . 在这里的回答中概述了类似的方法。 In that particular case I was moving data from SQL Server Compact into Access, but the same idea could very well be used to move data between any two OleDb data sources. 在那种特殊情况下,我将数据从SQL Server Compact移到Access中,但是同样的想法也可以很好地用于在任何两个OleDb数据源之间移动数据。

It uses an OleDbDataAdapter to pull the source table into a DataTable , copies it over to another DataTable , and then uses another OleDbDataAdapter to update the corresponding table in the destination database. 它使用OleDbDataAdapter将源表拉到DataTable ,将其复制到另一个DataTable ,然后使用另一个OleDbDataAdapter更新目标数据库中的相应表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM