Store the DataTable directly in the oracle database (transfer)

Notice

    1: The columns of the incoming DataTable must be consistent with the table columns in the database, otherwise the data will be stored in the first few columns by default.

    2: As long as the sql statement is a query to the table to be inserted, the purpose is to determine the table name

   3: The method to obtain the connection string is GetOracleConnection(), you need to add it yourself

public static bool insertValueWithDt(DataTable dataTable, string sql)

        {

            String ConnStr = GetOracleConnection();

            using (OracleConnection conn = new OracleConnection(ConnStr))

            {

                try

                {

                    OracleCommand cmd = new OracleCommand(sql,conn);

                    OracleDataAdapter adapter = new OracleDataAdapter(cmd);

                    OracleCommandBuilder cb = new OracleCommandBuilder(adapter);

                    DataTable dsNew = new DataTable();

                    int count = adapter.Fill(dsNew);

                    for (int i = 0; i < dataTable.Rows.Count; i++)

                    {

                        DataRow dr = dsNew.NewRow();

                        for (int j = 0; j < dataTable.Columns.Count; j++)

                        {

                            dr[dsNew.Columns[j].ColumnName] = dataTable.Rows[i][j];

                        }

                        dsNew.Rows.Add(dr);

                    }

                    count = adapter.Update(dsNew);

                    adapter.UpdateBatchSize = 200;

                    //adapter.Update(dataTable);

                    return true;

                }

                catch (Exception e)

                {

                    WriteLog writeLog = new WriteLog();

                    writeLog.WriteLogs(e.ToString());

                    return false;

                }

            }

        }

From: http://blog.sina.com.cn/s/blog_877c72ac010126h1.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325169683&siteId=291194637