ADO Data Types

Welcome to The Coding College! When working with ActiveX Data Objects (ADO), understanding data types is crucial for building efficient, error-free applications. ADO uses specific data types to define the kind of data stored in fields, parameters, and variables, ensuring proper handling of database interactions.

What Are ADO Data Types?

ADO data types are enumerated values that specify the type of data stored in a field, parameter, or variable. These types ensure compatibility between ADO and the underlying data source, such as SQL Server, Access, or Oracle.

Using the correct data type:

  • Improves performance by avoiding unnecessary conversions.
  • Prevents errors during data insertion, retrieval, or updates.
  • Ensures precision for numeric, date, or binary operations.

ADO Data Type Enumeration

ADO data types are defined in the DataTypeEnum enumeration. Below is a complete list:

Data TypeEnum ValueDescription
adBinary128Binary data, such as images or files.
adBoolean11True/False values.
adBSTR8Null-terminated Unicode string.
adChar129Single-byte character string.
adCurrency6Monetary values.
adDate7Date and time values.
adDBDate133Date only (no time component).
adDBTime134Time only (no date component).
adDBTimeStamp135Date and time with higher precision.
adDecimal14Exact numeric values with fixed precision and scale.
adDouble5Double-precision floating-point numbers.
adGUID72Globally unique identifier.
adIDispatch9Automation object reference.
adInteger34-byte signed integer.
adLongVarBinary205Long binary data (e.g., BLOBs).
adLongVarChar201Long character data (e.g., text).
adLongVarWChar203Long Unicode character data.
adNumeric131Exact numeric values.
adSingle4Single-precision floating-point numbers.
adSmallInt22-byte signed integer.
adTinyInt161-byte signed integer.
adUnsignedInt194-byte unsigned integer.
adUnsignedSmallInt182-byte unsigned integer.
adUnsignedTinyInt171-byte unsigned integer.
adVarBinary204Variable-length binary data.
adVarChar200Variable-length character data.
adVariant12Variant type.
adVarWChar202Variable-length Unicode character data.
adWChar130Fixed-length Unicode character data.

Commonly Used ADO Data Types

1. adVarChar and adWChar

  • Use for variable-length text data.
  • adVarChar handles ASCII strings, while adWChar supports Unicode.

2. adNumeric and adDecimal

  • Ideal for financial or scientific applications requiring high precision.

3. adDate and adDBTimeStamp

  • Use adDate for standard date and time.
  • Opt for adDBTimeStamp for high-precision date-time data.

4. adLongVarBinary

  • Handles large binary data like files, images, or multimedia.

Mapping ADO Data Types to SQL Server

When working with SQL Server, mapping is essential to ensure compatibility. Here’s a quick reference:

ADO Data TypeSQL Server Data Type
adIntegerINT
adVarCharVARCHAR(n)
adWCharNVARCHAR(n)
adLongVarBinaryVARBINARY(MAX)
adNumericNUMERIC(p, s)
adDateDATETIME

Example: Using ADO Data Types in Code

Here’s how to define and use ADO data types in your ASP application:

1. Creating a Recordset with ADO Data Types

Dim conn, rs
Set conn = Server.CreateObject("ADODB.Connection")
Set rs = Server.CreateObject("ADODB.Recordset")

' Open the connection
conn.Open "YourConnectionString"

' Open the recordset
rs.CursorType = 2 ' adOpenDynamic
rs.LockType = 3 ' adLockOptimistic
rs.Open "SELECT * FROM Employees", conn

' Add a new record
rs.AddNew
rs("Name") = "John Doe" ' adVarChar
rs("Salary") = 50000.00 ' adCurrency
rs("StartDate") = Now() ' adDate
rs.Update

' Close objects
rs.Close
conn.Close
Set rs = Nothing
Set conn = Nothing

2. Working with Parameters

Dim cmd, param
Set cmd = Server.CreateObject("ADODB.Command")
cmd.CommandText = "INSERT INTO Employees (Name, Salary, StartDate) VALUES (?, ?, ?)"
cmd.CommandType = 1 ' adCmdText

' Add parameters
Set param = cmd.CreateParameter("Name", 200, 1, 50, "John Doe") ' adVarChar
cmd.Parameters.Append param

Set param = cmd.CreateParameter("Salary", 6, 1, , 50000.00) ' adCurrency
cmd.Parameters.Append param

Set param = cmd.CreateParameter("StartDate", 7, 1, , Now()) ' adDate
cmd.Parameters.Append param

' Execute the command
cmd.Execute
Set cmd = Nothing

Best Practices for ADO Data Types

  1. Choose the Right Data Type:
    • Always match the ADO data type with the database field type.
  2. Validate Input:
    • Ensure user input aligns with the expected data type to avoid runtime errors.
  3. Use Unicode Types for Multilingual Support:
    • Use adWChar and adVarWChar for applications supporting multiple languages.
  4. Handle Null Values Gracefully:
    • Use IsNull or DBNull.Value to manage null data efficiently.
  5. Avoid Overflows:
    • Verify the size and precision of numeric fields to prevent overflow errors.

Conclusion

Understanding and correctly using ADO Data Types is essential for robust and efficient database programming. Whether working with text, numeric, or binary data, aligning ADO types with your database schema is key to success.

For more insights, tutorials, and hands-on examples, visit The Coding College.

Leave a Comment