Send feedback on this topic.
Teradata.Client.Provider
VarChar Data Type
.NET Data Provider for Teradata > Developer's Guide > Data Types > Character Data Types > VarChar Data Type

Schema Collections

The Schema collections return metadata for the Advanced SQL Engine objects. The notable metadata columns for the VarChar data type are:

Column Name Description

COLUMN_TYPE

or

DATA_TYPE

Set to VARCHAR.
CHARACTER_MAXIMUM_LENGTH Returns the maximum length of the column in Characters.
CHARACTER_OCTET_LENGTH Returns the maximum length of the column in Bytes. This field is set to "CHARACTER_MAXIMUM_LENGTH * 2" for Unicode Character columns.
FORMAT Format assigned to the SQL Engine object.
CHAR_TYPE

Indicates whether the columns is of type:

  1. LATIN
  2. UNICODE
  3. KANJISJIS
  4. GRAPHIC
  5. KANJI1
PROVIDERDBTYPE It is set to TdType.VarChar.

Schema Table

The TdDataReader.SchemaTable returns result set metadata. The notable metadata columns for the VarChar data type are:

Column Name Description
ColumnSize Returns the maximum length of the column in Characters.
DataType System.Type object for the System.String type.
ProviderType Set to TdType.VarChar.
Format Format assigned to the SQL Engine object.
ProviderSpecificDataType System.Type object for the System.String class.

Configuring Parameters

The following example shows how to configure a VarChar parameter using DbType type declaration and a BCL value. The value is set to a System.String instance.

C#
Copy Code
TdParameter customerName = new TdParameter();
customerName.ParameterName = "customerName";

customerName.DbType = DbType.String;

customerName.Value = "John Smith";
Note

The TdParameter.DbType property can also be set to DbType.AnsiString value for parameters that correspond to the SQL Engine objects of type "VARCHAR(n) CHARACTER SET KANJISJIS". The TdParameter.Size property specifies number of Bytes send to and/or received from the SQL Engine when DbType is set to AnsiString. For additional information refer to DbParameter.Size and TdParameter.Size properties.

The following example shows how to configure a VarChar parameter using TdType type declaration and a BCL value.

C#
Copy Code
TdParameter customerName = new TdParameter();
customerName.ParameterName = "orderDate";

customerName.TdType = TdType.VarChar;

customerName.Value = "John Smith";

The TdParameter.Value should be set to an Array of Characters (Char[]), String object or an object that supports conversion (Convert.ToString) to System.String.

Set TdParameter.Size Property for InputOutput or Output Parameters

The TdParameter.Size property must be set to a number greater than Zero for InputOutput or Output Parameters. The Data Provider will throw an exception when TdParameter.Direction property is set to ParameterDirection.Output or ParameterDirection.InputOutput and TdParameter.Size property is set to Zero. The TdParameter.Size property specifies the maximum number of Unicode Characters (or Bytes) the Data Provider will send to the SQL Engine and/or will receive from the SQL Engine. We recommend to set the TdParameter.Size property to the corresponding SQL Engine object size (e.g. Column or Stored Procedure Parameter size) for Input, InputOutput or Output parameters.

Note

TdParameter.Size is not required for Input parameters. However it will improve the overall performance of Batch Updates. Therefore the general guideline is to always set the TdParameter.Size property.

Specifying VarChar as Literal

The syntax for the VARCHAR Literal is 'string'. A zero-length character literal (i.e. Empty String) is represented by two consecutive single-quotes.

Note

We recommend to always use Parameters in order to take advantage of the SQL Engine's Request Cache.

The following example shows "John Smith" value represented as Character-Literal in the Command Text.

C#
Copy Code
using (TdConnection cn = new TdConnection("data source=x;UserId=y;Password=z;"))
{
    cn.Open();
    TdCommand cmd = cn.CreateCommand();

    cmd.CommandText = "SELECT customerId, customerName from Customer where customerName = 'John Smith' ";

    using (TdDataReader reader = cmd.ExecuteReader())
    {
        if (reader.HasRows)
        {
            while (reader.Read())
            {
                Console.WriteLine("Customer Id={0}, Name={1}", reader.GetString(0), reader.GetString(1));
            }
        }
    }
}

Retrieving VarChar Data

The following methods and properties return the column or parameter value as a System.Char structure or System.Char[] object.

  1. TdDataReader.GetChar
  2. TdDataReader.GetChars
  3. TdDataReader.GetFieldValue<Char>
  4. TdDataReader.GetFieldValue<Char[]>
  5. TdParameter.GetValue<Char[]>

The following methods and properties return the column or parameter value as a System.String object.

  1. TdDataReader.GetString
  2. TdDataReader.GetValue
  3. TdDataReader.GetProviderSpecificValue
  4. TdDataReader.GetFieldValue<String>
  5. TdParameter.Value
  6. TdParameter.GetValue<String>

The following methods and properties return the column or parameter value as a System.TextReader object.

  1. TdDataReader.GetTextReader
  2. TdDataReader.GetFieldValue<TextReader>
  3. TdParameter.GetValue<TextReader>

See Also

Data Type Mappings

Accessor Methods for Retrieving Data

Configuring Parameters and Parameter Data Types