The following is a very simple C# .NET Windows program that shows a basic connection to an Omnidex Environment and simple query processing.
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Odbc; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace OdxOdbcNetDemo { public partial class MainForm : Form { private OdbcConnection _connection; public MainForm() { InitializeComponent(); tbHost.Text = "localhost"; tbPort.Text = "7555"; } private void buttonConnect_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(tbEnvironment.Text)) { MessageBox.Show("Please enter Omnidex Environment Filename"); return; } if (string.IsNullOrEmpty(tbHost.Text)) { MessageBox.Show("Please enter Host"); return; } if (string.IsNullOrEmpty(tbPort.Text)) { MessageBox.Show("Please enter port number"); return; } try { if (_connection != null) { if (_connection.State != ConnectionState.Closed) _connection.Close(); } else _connection = new OdbcConnection(); if (tbEnvironment.Text.StartsWith("DSN=") || tbEnvironment.Text.StartsWith("FILEDSN=")) { // connecting to an Omnidex environment file using a machine or file ODBC DataSource _connection.ConnectionString = tbEnvironment.Text; } else { // connecting to an Omnidex environment file without an ODBC DataSource _connection.ConnectionString = string.Format("DRIVER=OMNIDEX;ENV=[{0}:{1}]{2}", tbHost.Text, tbPort.Text, tbEnvironment.Text); } _connection.Open(); MessageBox.Show("Connected"); } catch (Exception ex) { MessageBox.Show(ex.Message); } } private void buttonExecute_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(tbSQL.Text)) return; if (_connection == null || _connection.State != ConnectionState.Open) { MessageBox.Show("Not connected."); return; } // clear result grid if (dgvResult.DataSource != null) dgvResult.DataSource = null; else { dgvResult.Rows.Clear(); dgvResult.Columns.Clear(); } OdbcCommand cmd = null; OdbcDataReader reader = null; try { cmd = new OdbcCommand(tbSQL.Text, _connection); if (tbSQL.Text.StartsWith("SELECT", StringComparison.CurrentCultureIgnoreCase)) { reader = cmd.ExecuteReader(); DataTable dt = new DataTable(); dt.Load(reader); dgvResult.DataSource = dt; dgvResult.ColumnHeadersVisible = true; dgvResult.AutoGenerateColumns = true; dgvResult.Refresh(); } else { int count = cmd.ExecuteNonQuery(); dgvResult.ColumnHeadersVisible = false; dgvResult.AutoGenerateColumns = false; dgvResult.Columns.Add(new DataGridViewTextBoxColumn()); dgvResult.Rows.Add(string.Format("{0} row(s) affected", count)); } } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { if (reader != null) reader.Close(); } } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { try { if (_connection != null && _connection.State != ConnectionState.Closed) _connection.Close(); } catch (Exception) { } } } }
The following designer provides the source code for the form referenced in the program above.
namespace OdxOdbcNetDemo { partial class MainForm { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.label2 = new System.Windows.Forms.Label(); this.tbSQL = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.dgvResult = new System.Windows.Forms.DataGridView(); this.buttonConnect = new System.Windows.Forms.Button(); this.buttonExecute = new System.Windows.Forms.Button(); this.tableLayoutPanel2 = new System.Windows.Forms.TableLayoutPanel(); this.label1 = new System.Windows.Forms.Label(); this.tbEnvironment = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.tbHost = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.tbPort = new System.Windows.Forms.TextBox(); this.tableLayoutPanel1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.dgvResult)).BeginInit(); this.tableLayoutPanel2.SuspendLayout(); this.SuspendLayout(); // // tableLayoutPanel1 // this.tableLayoutPanel1.ColumnCount = 3; this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel1.Controls.Add(this.label2, 0, 1); this.tableLayoutPanel1.Controls.Add(this.tbSQL, 0, 2); this.tableLayoutPanel1.Controls.Add(this.label3, 0, 3); this.tableLayoutPanel1.Controls.Add(this.dgvResult, 0, 4); this.tableLayoutPanel1.Controls.Add(this.buttonConnect, 2, 0); this.tableLayoutPanel1.Controls.Add(this.buttonExecute, 2, 2); this.tableLayoutPanel1.Controls.Add(this.tableLayoutPanel2, 0, 0); this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0); this.tableLayoutPanel1.Name = "tableLayoutPanel1"; this.tableLayoutPanel1.Padding = new System.Windows.Forms.Padding(10); this.tableLayoutPanel1.RowCount = 5; this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 40F)); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle()); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 60F)); this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F)); this.tableLayoutPanel1.Size = new System.Drawing.Size(630, 424); this.tableLayoutPanel1.TabIndex = 0; // // label2 // this.label2.AutoSize = true; this.label2.Dock = System.Windows.Forms.DockStyle.Fill; this.label2.Location = new System.Drawing.Point(13, 41); this.label2.Margin = new System.Windows.Forms.Padding(3, 5, 3, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(82, 13); this.label2.TabIndex = 1; this.label2.Text = "SQL Statement:"; // // tbSQL // this.tableLayoutPanel1.SetColumnSpan(this.tbSQL, 2); this.tbSQL.Dock = System.Windows.Forms.DockStyle.Fill; this.tbSQL.Location = new System.Drawing.Point(13, 57); this.tbSQL.Multiline = true; this.tbSQL.Name = "tbSQL"; this.tbSQL.Size = new System.Drawing.Size(518, 130); this.tbSQL.TabIndex = 2; // // label3 // this.label3.AutoSize = true; this.label3.Dock = System.Windows.Forms.DockStyle.Fill; this.label3.Location = new System.Drawing.Point(13, 195); this.label3.Margin = new System.Windows.Forms.Padding(3, 5, 3, 0); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(82, 13); this.label3.TabIndex = 3; this.label3.Text = "Result:"; // // dgvResult // this.dgvResult.AllowUserToAddRows = false; this.dgvResult.AllowUserToDeleteRows = false; this.dgvResult.AllowUserToOrderColumns = true; this.dgvResult.BackgroundColor = System.Drawing.Color.White; this.dgvResult.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.dgvResult.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.tableLayoutPanel1.SetColumnSpan(this.dgvResult, 3); this.dgvResult.Dock = System.Windows.Forms.DockStyle.Fill; this.dgvResult.Location = new System.Drawing.Point(13, 211); this.dgvResult.MultiSelect = false; this.dgvResult.Name = "dgvResult"; this.dgvResult.ReadOnly = true; this.dgvResult.RowHeadersVisible = false; this.dgvResult.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; this.dgvResult.Size = new System.Drawing.Size(604, 200); this.dgvResult.TabIndex = 4; // // buttonConnect // this.buttonConnect.Location = new System.Drawing.Point(537, 10); this.buttonConnect.Margin = new System.Windows.Forms.Padding(3, 0, 3, 3); this.buttonConnect.Name = "buttonConnect"; this.buttonConnect.Size = new System.Drawing.Size(75, 23); this.buttonConnect.TabIndex = 5; this.buttonConnect.Text = "Connect"; this.buttonConnect.UseVisualStyleBackColor = true; this.buttonConnect.Click += new System.EventHandler(this.buttonConnect_Click); // // buttonExecute // this.buttonExecute.AutoSize = true; this.buttonExecute.Location = new System.Drawing.Point(537, 57); this.buttonExecute.Name = "buttonExecute"; this.buttonExecute.Size = new System.Drawing.Size(80, 23); this.buttonExecute.TabIndex = 6; this.buttonExecute.Text = "Execute SQL"; this.buttonExecute.UseVisualStyleBackColor = true; this.buttonExecute.Click += new System.EventHandler(this.buttonExecute_Click); // // tableLayoutPanel2 // this.tableLayoutPanel2.AutoSize = true; this.tableLayoutPanel2.ColumnCount = 6; this.tableLayoutPanel1.SetColumnSpan(this.tableLayoutPanel2, 2); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle()); this.tableLayoutPanel2.Controls.Add(this.label1, 0, 0); this.tableLayoutPanel2.Controls.Add(this.tbEnvironment, 1, 0); this.tableLayoutPanel2.Controls.Add(this.label4, 2, 0); this.tableLayoutPanel2.Controls.Add(this.tbHost, 3, 0); this.tableLayoutPanel2.Controls.Add(this.label5, 4, 0); this.tableLayoutPanel2.Controls.Add(this.tbPort, 5, 0); this.tableLayoutPanel2.Dock = System.Windows.Forms.DockStyle.Fill; this.tableLayoutPanel2.Location = new System.Drawing.Point(10, 10); this.tableLayoutPanel2.Margin = new System.Windows.Forms.Padding(0); this.tableLayoutPanel2.Name = "tableLayoutPanel2"; this.tableLayoutPanel2.RowCount = 1; this.tableLayoutPanel2.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100F)); this.tableLayoutPanel2.Size = new System.Drawing.Size(524, 26); this.tableLayoutPanel2.TabIndex = 0; // // label1 // this.label1.AutoSize = true; this.label1.Dock = System.Windows.Forms.DockStyle.Fill; this.label1.Location = new System.Drawing.Point(3, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(88, 26); this.label1.TabIndex = 0; this.label1.Text = "Environment File:"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // tbEnvironment // this.tbEnvironment.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend; this.tbEnvironment.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.FileSystem; this.tbEnvironment.Dock = System.Windows.Forms.DockStyle.Fill; this.tbEnvironment.Location = new System.Drawing.Point(97, 3); this.tbEnvironment.Name = "tbEnvironment"; this.tbEnvironment.Size = new System.Drawing.Size(189, 20); this.tbEnvironment.TabIndex = 1; // // label4 // this.label4.AutoSize = true; this.label4.Dock = System.Windows.Forms.DockStyle.Fill; this.label4.Location = new System.Drawing.Point(292, 0); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(32, 26); this.label4.TabIndex = 2; this.label4.Text = "Host:"; this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // tbHost // this.tbHost.Location = new System.Drawing.Point(330, 3); this.tbHost.Name = "tbHost"; this.tbHost.Size = new System.Drawing.Size(100, 20); this.tbHost.TabIndex = 3; // // label5 // this.label5.AutoSize = true; this.label5.Dock = System.Windows.Forms.DockStyle.Fill; this.label5.Location = new System.Drawing.Point(436, 0); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(29, 26); this.label5.TabIndex = 4; this.label5.Text = "Port:"; this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; // // tbPort // this.tbPort.Location = new System.Drawing.Point(471, 3); this.tbPort.Name = "tbPort"; this.tbPort.Size = new System.Drawing.Size(50, 20); this.tbPort.TabIndex = 5; // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(630, 424); this.Controls.Add(this.tableLayoutPanel1); this.Name = "MainForm"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "SQL Window"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainForm_FormClosing); this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.dgvResult)).EndInit(); this.tableLayoutPanel2.ResumeLayout(false); this.tableLayoutPanel2.PerformLayout(); this.ResumeLayout(false); } #endregion private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox tbSQL; private System.Windows.Forms.Label label3; private System.Windows.Forms.DataGridView dgvResult; private System.Windows.Forms.Button buttonConnect; private System.Windows.Forms.Button buttonExecute; private System.Windows.Forms.TableLayoutPanel tableLayoutPanel2; private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox tbEnvironment; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox tbHost; private System.Windows.Forms.Label label5; private System.Windows.Forms.TextBox tbPort; } }
See also: