Insert multiple rows in DB2Command.ExecuteNonQuery

I am trying to insert multiple rows into a DB2 database using C # code as follows:

string query = 
"INSERT INTO TESTDB2.RG_Table (V,E,L,N,Q,B,S,P) values" +
"('abc', 'def', '2009-03-27 12:01:19', 'ghi', 'jkl', NULL, NULL, NULL)," +
"('abc', 'def', '2009-03-27 12:01:19', 'ghi', 'jkl', NULL, NULL, NULL)";

DB2Command cmd = new DB2Command(query, this.connection, this.transaction);

cmd.ExecuteNonQuery();

If I stop building the query string after the first set of values, it runs without error. Attempting to load multiple values ​​using this method results in the following error:

Upload error : ERROR [42601] [IBM][DB2] SQL0104N  
An unexpected token "," was found following "".  
Expected tokens may include:  "<END-OF-STATEMENT>".  SQLSTATE=42601

The SQL syntax matches what I read elsewhere , and an example is provided in the IBM documentation:

cmd = conn.CreateCommand();
cmd.Transaction = trans;

cmd.CommandText =
"INSERT INTO company_a VALUES(5275, 'Sanders', 20, 'Mgr', 15, 18357.50), " +
"(5265, 'Pernal', 20, 'Sales', NULL, 18171.25), " +
"(5791, 'O''Brien', 38, 'Sales', 9, 18006.00)";

cmd.ExecuteNonQuery();

Can someone explain what could explain this?

+3
source share
4 answers

, , . VALUES, z/OS, . . .

, , , .NET, "Chaining" . //, "" , . :

<!-- language: lang-cs --> //Code parser seems to be going crazy here...
public void InsertToDatabase(IEnumerable<Row> rows)
{
    using (var conn = new DB2Connection())
    using (var trans = conn.BeginTransaction())
    using (var cmd = conn.CreateCommand())
    {
        cmd.Transaction = trans;
        cmd.CommandText =
            "INSERT INTO company_a VALUES " +
            "(@field1,@field2,@field3,@field4,@field5,@field6)";

        conn.BeginChain();
        foreach (var row in rows)
        {
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@field1", row.Field1);
            cmd.Parameters.Add("@field2", row.Field2);
            cmd.Parameters.Add("@field3", row.Field3);
            cmd.Parameters.Add("@field4", row.Field4);
            cmd.Parameters.Add("@field5", row.Field5);
            cmd.Parameters.Add("@field6", row.Field6);
            cmd.ExecuteNonQuery();
        }
        conn.EndChain();
        trans.Commit();
    }
}

DB2 , EndChain, .

+1

?. .

0

.

  • cli?
  • JDBC. , , .
0

, mutliple , rowlist;

(V,E,L,N,Q,B,S,P)

, .

SQL :

string query = "INSERT INTO TESTDB2.RG_Table values" +   
"('abc', 'def', '2009-03-27 12:01:19', 'ghi', 'jkl', NULL, NULL, NULL)," +
"('abc', 'def', '2009-03-27 12:01:19', 'ghi', 'jkl', NULL, NULL, NULL)"; 
0

Source: https://habr.com/ru/post/1706608/


All Articles