<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>OpenText Analytics Database 26.2.x – Inserting data (ADO.NET)</title>
    <link>/en/connecting-to/client-libraries/accessing/c/querying-db-using-ado-net/inserting-data-ado-net/</link>
    <description>Recent content in Inserting data (ADO.NET) on OpenText Analytics Database 26.2.x</description>
    <generator>Hugo -- gohugo.io</generator>
    
	  <atom:link href="/en/connecting-to/client-libraries/accessing/c/querying-db-using-ado-net/inserting-data-ado-net/index.xml" rel="self" type="application/rss+xml" />
    
    
      
        
      
    
    
    <item>
      <title>Connecting-To: Using parameters</title>
      <link>/en/connecting-to/client-libraries/accessing/c/querying-db-using-ado-net/inserting-data-ado-net/using-parameters/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/connecting-to/client-libraries/accessing/c/querying-db-using-ado-net/inserting-data-ado-net/using-parameters/</guid>
      <description>
        
        
        &lt;p&gt;You can use parameters to execute similar SQL statements repeatedly and efficiently.&lt;/p&gt;
&lt;h2 id=&#34;using-parameters&#34;&gt;Using parameters&lt;/h2&gt;
&lt;p&gt;VerticaParameters are an extension of the System.Data.DbParameter base class in ADO.NET and are used to set parameters in commands sent to the server. Use Parameters in all queries (SELECT/INSERT/UPDATE/DELETE) for which the values in the WHERE clause are not static; that is for all queries that have a known set of columns, but whose filter criteria is set dynamically by an application or end user. Using parameters in this way greatly decreases the chances of a SQL injection issue that can occur when simply creating a SQL query from a number of variables.&lt;/p&gt;
&lt;p&gt;Parameters require that a valid DbType, VerticaDbType, or System type be assigned to the parameter. See &lt;a href=&#34;../../../../../../../en/sql-reference/data-types/#&#34;&gt;Data types&lt;/a&gt; and &lt;a href=&#34;../../../../../../../en/connecting-to/client-libraries/accessing/c/ado-net-data-types/#&#34;&gt;ADO.NET data types&lt;/a&gt; for a mapping of System, OpenText™ Analytics Database, and DbTypes.&lt;/p&gt;
&lt;p&gt;To create a parameter placeholder, place either the at sign (@) or a colon (:) character in front of the parameter name in the actual query string. Do not insert any spaces between the placeholder indicator (@ or :) and the placeholder.

&lt;div class=&#34;alert admonition note&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Note&lt;/h4&gt;

The @ character is the preferred way to identify parameters. The colon (:) character is supported for backward compatibility.

&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;For example, the following typical query uses the string &#39;MA&#39; as a filter.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;SELECT customer_name, customer_address, customer_city, customer_state
FROM customer_dimension WHERE customer_state = &amp;#39;MA&amp;#39;;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;Instead, the query can be written to use a parameter. In the following example, the string MA is replaced by the parameter placeholder @STATE.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;SELECT customer_name, customer_address, customer_city, customer_state
FROM customer_dimension WHERE customer_state = @STATE;
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;For example, the ADO.net code for the prior example would be written as:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;VerticaCommand command = _conn.CreateCommand();
command.CommandText = “SELECT customer_name, customer_address, customer_city, customer_state
    FROM customer_dimension WHERE customer_state = @STATE”;
command.Parameters.Add(new VerticaParameter( “STATE”, VerticaType.VarChar));
command.Parameters[&amp;#34;STATE&amp;#34;].Value = &amp;#34;MA&amp;#34;;
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&#34;alert admonition note&#34; role=&#34;alert&#34;&gt;
&lt;h4 class=&#34;admonition-head&#34;&gt;Note&lt;/h4&gt;

Although the VerticaCommand class supports a Prepare() method, you do not need to call the Prepare() method for parameterized statements because the database automatically prepares the statement for you.

&lt;/div&gt;

      </description>
    </item>
    
    <item>
      <title>Connecting-To: Creating and rolling back transactions</title>
      <link>/en/connecting-to/client-libraries/accessing/c/querying-db-using-ado-net/inserting-data-ado-net/creating-and-rolling-back-transactions/</link>
      <pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate>
      
      <guid>/en/connecting-to/client-libraries/accessing/c/querying-db-using-ado-net/inserting-data-ado-net/creating-and-rolling-back-transactions/</guid>
      <description>
        
        
        &lt;h2 id=&#34;creating-transactions&#34;&gt;Creating transactions&lt;/h2&gt;
&lt;p&gt;Transactions in OpenText™ Analytics Database are atomic, consistent, isolated, and durable. When you connect to a database using the ADO.NET Driver, the connection is in autocommit mode and each individual query is committed upon execution. You can collect multiple statements into a single transaction and commit them at the same time by using a transaction. You can also choose to rollback a transaction before it is committed if your code determines that a transaction should not commit.&lt;/p&gt;
&lt;p&gt;Transactions use the VerticaTransaction object, which is an implementation of DbTransaction. You must associate the transaction with the VerticaCommand object.&lt;/p&gt;
&lt;p&gt;The following code uses an explicit transaction to insert one row each into to tables of the VMart schema.&lt;/p&gt;
&lt;h2 id=&#34;to-create-a-transaction-in-opentexttrade-analytics-database-using-the-adonet-driver&#34;&gt;To create a transaction in OpenText™ Analytics Database using the ADO.NET driver:&lt;/h2&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;a href=&#34;../../../../../../../en/connecting-to/client-libraries/accessing/c/connecting-to-db/opening-and-closing-db-connection-ado-net/&#34;&gt;Create a connection to the database&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Create a command object using the connection.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;VerticaCommand command = _conn.CreateCommand();
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Start an explicit transaction, and associate the command with it.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;VerticaTransaction txn = _conn.BeginTransaction();
command.Connection = _conn;
command.Transaction = txn;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Execute the individual SQL statements to add rows.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;command.CommandText =
     &amp;#34;insert into product_dimension values( ... )&amp;#34;;
command.ExecuteNonQuery();
command.CommandText =
     &amp;#34;insert into store_orders_fact values( ... )&amp;#34;;
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Commit the transaction.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;txn.Commit();
&lt;/code&gt;&lt;/pre&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;rolling-back-transactions&#34;&gt;Rolling back transactions&lt;/h2&gt;
&lt;p&gt;If your code checks for errors, then you can catch the error and rollback the entire transaction.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;VerticaTransaction txn = _conn.BeginTransaction();
VerticaCommand command = new
        VerticaCommand(&amp;#34;insert into product_dimension values( 838929, 5, &amp;#39;New item 5&amp;#39; )&amp;#34;, _conn);
// execute the insert
command.ExecuteNonQuery();
command.CommandText = &amp;#34;insert into product_dimension values( 838929, 6, &amp;#39;New item 6&amp;#39; )&amp;#34;;
// try insert and catch any errors
bool error = false;
try
{
    command.ExecuteNonQuery();
}
catch (Exception e)
{
    Console.WriteLine(e.Message);
    error = true;
}
if (error)
{
    txn.Rollback();
    Console.WriteLine(&amp;#34;Errors. Rolling Back.&amp;#34;);
}
else
{
    txn.Commit();
    Console.WriteLine(&amp;#34;Queries Successful. Committing.&amp;#34;);
}
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;commit-and-rollback-example&#34;&gt;Commit and rollback example&lt;/h2&gt;
&lt;p&gt;This example details how you can commit or rollback queries during a transaction.&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Vertica.Data.VerticaClient;
namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            VerticaConnectionStringBuilder builder = new VerticaConnectionStringBuilder();
            builder.Host = &amp;#34;192.168.1.10&amp;#34;;
            builder.Database = &amp;#34;VMart&amp;#34;;
            builder.User = &amp;#34;dbadmin&amp;#34;;
            VerticaConnection _conn = new VerticaConnection(builder.ToString());
            _conn.Open();
            bool error = false;
                VerticaCommand command = _conn.CreateCommand();
                VerticaCommand command2 = _conn.CreateCommand();
                VerticaTransaction txn = _conn.BeginTransaction();
                command.Connection = _conn;
                command.Transaction = txn;
                command.CommandText =
                &amp;#34;insert into test values(1, &amp;#39;test&amp;#39;, &amp;#39;test&amp;#39;, &amp;#39;test&amp;#39; )&amp;#34;;
                Console.WriteLine(command.CommandText);
                try
                {
                    command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    error = true;
                }
                command.CommandText =
                &amp;#34;insert into test values(2, &amp;#39;ear&amp;#39;, &amp;#39;eye&amp;#39;, &amp;#39;nose&amp;#39;, &amp;#39;extra&amp;#39; )&amp;#34;;
                Console.WriteLine(command.CommandText);
                try
                {
                    command.ExecuteNonQuery();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    error = true;
                }
                if (error)
                {
                    txn.Rollback();
                    Console.WriteLine(&amp;#34;Errors. Rolling Back.&amp;#34;);
                }
                else
                {
                    txn.Commit();
                    Console.WriteLine(&amp;#34;Queries Successful. Committing.&amp;#34;);
                }
            _conn.Close();
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;p&gt;The example displays the following output on the console:&lt;/p&gt;
&lt;pre tabindex=&#34;0&#34;&gt;&lt;code&gt;insert into test values(1, &amp;#39;test&amp;#39;, &amp;#39;test&amp;#39;, &amp;#39;test&amp;#39; )
insert into test values(2, &amp;#39;ear&amp;#39;, &amp;#39;eye&amp;#39;, &amp;#39;nose&amp;#39;, &amp;#39;extra&amp;#39; )
[42601]ERROR: INSERT has more expressions than target columns
Errors. Rolling Back.
&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&#34;see-also&#34;&gt;See also&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;../../../../../../../en/connecting-to/client-libraries/accessing/c/querying-db-using-ado-net/inserting-data-ado-net/creating-and-rolling-back-transactions/setting-transaction-isolation-level/#&#34;&gt;Setting the transaction isolation level&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

      </description>
    </item>
    
  </channel>
</rss>
