This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
dev:odbc:sample [2011/01/12 22:42] els |
dev:odbc:sample [2012/10/26 14:51] (current) |
||
|---|---|---|---|
| Line 3: | Line 3: | ||
| ====== Development: ODBC Interface ====== | ====== Development: ODBC Interface ====== | ||
| - | [[dev:odbc:home|Overview]] | [[dev:odbc:datasources|ODBC Datasources]] | **[[dev:odbc:sample|Sample C# Program]]** | + | [[dev:odbc:home|Overview]] | [[dev:odbc:datasources|ODBC Datasources]] | [[dev:odbc:registering|Registering the Omnidex Driver]] | **[[dev:odbc:sample|Sample C# Program]]** | [[dev:odbc:examples:php_select | Sample PHP Program]] |
| ---- | ---- | ||
| Line 20: | Line 19: | ||
| namespace OdxODBCSample | namespace OdxODBCSample | ||
| { | { | ||
| - | class OdxODBCSample | + | class OdxODBCSample |
| + | { | ||
| + | static void Main(string[] args) | ||
| { | { | ||
| - | static void Main(string[] args) | + | Console.WriteLine("Omnidex Sample C# Program"); |
| + | |||
| + | string connString = | ||
| + | @"DRIVER={Omnidex};CONNECTIONSTRING=[server1:7555]c:\class\simple.xml"; | ||
| + | string sqlselect = @"select NAME, PHONE from INDIVIDUALS"; | ||
| + | OdbcConnection conn = null; | ||
| + | OdbcDataReader reader = null; | ||
| + | try | ||
| + | { | ||
| + | // Open Connection | ||
| + | conn = new OdbcConnection(connString); | ||
| + | conn.Open(); | ||
| + | // Execute the SQL statement | ||
| + | OdbcCommand cmd = new OdbcCommand(sqlselect, conn); | ||
| + | reader = cmd.ExecuteReader(); | ||
| + | // Display output header | ||
| + | Console.WriteLine("NAME\tPHONE\n"); | ||
| + | // Process the result set | ||
| + | while (reader.Read()) | ||
| + | { | ||
| + | Console.WriteLine( | ||
| + | "{0}\t{1}", | ||
| + | reader.GetString(0), | ||
| + | reader.GetString(1) | ||
| + | ); | ||
| + | } | ||
| + | } // try | ||
| + | catch (Exception e) | ||
| + | { | ||
| + | Console.WriteLine("Sql Error: " + e); | ||
| + | } | ||
| + | finally | ||
| + | { | ||
| + | try | ||
| + | { | ||
| + | if (reader != null) | ||
| + | reader.Close(); | ||
| + | if (conn != null) | ||
| + | conn.Close(); | ||
| + | } | ||
| + | catch (Exception e) | ||
| { | { | ||
| - | Console.WriteLine("Omnidex Sample C# Program"); | + | Console.WriteLine("Close Error: " + e); |
| - | + | } | |
| - | string connString = | + | } |
| - | @"DRIVER={Omnidex};CONNECTIONSTRING=[server1:7555]c:\class\simple.xml"; | + | } |
| - | string sqlselect = "select NAME, PHONE from INDIVIDUALS"; | + | } |
| - | OdbcConnection conn = null; | + | } |
| - | OdbcDataReader reader = null; | + | |
| - | try | + | |
| - | { | + | |
| - | // Open Connection | + | |
| - | conn = new OdbcConnection(connString); | + | |
| - | conn.Open(); | + | |
| - | // Execute the SQL statement | + | |
| - | OdbcCommand cmd = new OdbcCommand(sqlselect, conn); | + | |
| - | reader = cmd.ExecuteReader(); | + | |
| - | // Display output header | + | |
| - | Console.WriteLine("NAME\tPHONE"); | + | |
| - | // Process the result set | + | |
| - | while(reader.Read()) | + | |
| - | { | + | |
| - | Console.WriteLine( | + | |
| - | "{0}\t{1}" | + | |
| - | , reader["NAME"].ToString().PadLeft(10) | + | |
| - | , reader["PHONE"].ToString().PadLeft(10) | + | |
| - | ); | + | |
| - | } | + | |
| - | } // try | + | |
| - | catch (Exception e) | + | |
| - | { | + | |
| - | Console.WriteLine("Error: " + e); | + | |
| - | } | + | |
| - | finally | + | |
| - | { | + | |
| - | if (reader != null) | + | |
| - | reader.Close(); | + | |
| - | if (conn != null) | + | |
| - | conn.Close(); | + | |
| - | } | + | |
| - | } // end main | + | |
| - | } // end class | + | |
| - | } // end namespace | + | |
| </code> | </code> | ||