This shows you the differences between two versions of the page.
dev:odbc:sample_net [2015/01/28 01:53] doc |
dev:odbc:sample_net [2016/06/28 22:38] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | {{page>:top_add&nofooter&noeditbtn}} | ||
- | |||
- | ====== Development: ODBC Interface ====== | ||
- | |||
- | [[dev:odbc:home|Overview]] | [[dev:odbc:datasources|ODBC Datasources]] | [[dev:odbc:sample_c|Sample C# Program]] | **[[dev:odbc:sample_net|Sample .NET Program]]** | ||
- | |||
- | ---- | ||
- | |||
- | |||
- | ===== Sample .NET Program ===== | ||
- | |||
- | The following is a very simple .NET program that shows a basic connection to an Omnidex Environment and simple query processing. | ||
- | |||
- | <code csharp> | ||
- | 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) | ||
- | { } | ||
- | } | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | ===== ===== | ||
- | |||
- | **[[dev:odbc:datasources|Prev]]** | ||
- | |||
- | ====== Additional Resources ====== | ||
- | |||
- | See also: | ||
- | |||
- | {{page>:dev:see_also&nofooter&noeditbtn}} | ||
- | |||
- | {{page>:bottom_add&nofooter&noeditbtn}} | ||