Codecore Logo
Quick Search
»
Advanced Search »

OdbcConnection Object

RSS
An OdbcConnection object represents an open connection to a data source (such as a database). The OdbcConnection object uses the ODBC provider specified in the connection string to connect to the data source. This allows for a variety of data sources to be used such as MySql, Sql Server, Oracle and many more.


Connection String

The connection string is an ODBC provider connection string that includes the data source name, and other parameters needed to establish the initial connection.

You can find a variety of ODBC connection string examples at connectionstrings.com.

Example Connection Strings

  • MySql

Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=mydbname; User=myuserid;Password=mypassword;Option=3;

Constructors

OdbcConnection

Initializes a new OdbcConnection instance with the specified ODBC connection string.

Syntax:

String( String connectionString )

Parameters:

connectionString: The connection used to open the data source.

Example:


db = new OdbcConnection( "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=mydbname; User=myuserid;Password=mypassword;Option=3;" );

Instance Methods

Close

Closes the connection to the data source.

Syntax:

void Close( )

Example:


db.Close();

Execute

Executes a SQL commands (such as SQL INSERT, DELETE, UPDATE, and SET statements) against the connection and returns the number of rows affected.

Syntax:

Number Execute(String sqlStatement)

Parameters:

sqlStatement: The text of the sql statement.

Return Value

The number of rows affected.

Example:


n = db.Execute("delete from mytable where col1=2");

GetValue ( String )

Executes the query and returns the first column of the first row in the result set by the query. Additional columns and rows are ignored.

Syntax:

Object GetValue(String sqlStatement)

Parameters:

sqlStatement: The text of the sql query.

Return Value

The first column of the first row in the result set, or a null reference if the result set is empty.

Example:


n = db.GetValue("select count(col1) from mytable where col1=2");

GetValue ( String, Object )

Executes the query and returns the first column of the first row in the result set by the query if any, otherwise returns the default value. Additional columns and rows are ignored.

Syntax:

Object GetValue(String sqlStatement, Object defaultValue )

Parameters:

sqlStatement: The text of the sql query.

defaultValue: The value to return if the result set is empty.

Return Value

The first column of the first row in the result set, or the default value if the result set is empty.

Example:


n = db.GetValue("select max(col1) from mytable", 0);

GetDataTable

Executes the SQl SELECT query and returns a DataTable instance containing the result set.

Syntax:

DataTable GetDataTable(String sqlStatement)

Parameters:

sqlStatement: A string that is an SQL SELECT statement or stored procedure.

Return Value

The number of rows affected.

Example:


dt = db.GetDataTable("select * from mytable");

See Also

Scripting Language
OleDbConnection Object
DataTable Object
Privacy Policy | Conditions Of Use

Copyright ©2014 Codecore Technologies, All rights reserved.