Page 1 of 1

Connection String Dialog?

Posted: Tue 10 Jul 2007 01:11
by Alladin
Cannot find Connection String Dialog component.

In Visual Studio OracleConnection.ConnectionString has [...] button which spawns this pretty dialog. Nothing really special, just a bunch of attributes gathered in a special configuration class displayed by standard PropertyGrid.

However I can't find how to run it. Any help?
(I can't believe I have to reinvent the wheel)

Posted: Tue 10 Jul 2007 07:38
by Alexey
When you drop OracleConnection component onto a form, it shows a smart tag (a triangle in the right upper corner). Click this smart tag and choose "ConnectionString..." option. Here is your pretty dialog.

Posted: Tue 10 Jul 2007 12:58
by Alladin
Alexey wrote:When you drop OracleConnection component onto a form, it shows a smart tag (a triangle in the right upper corner). Click this smart tag and choose "ConnectionString..." option. Here is your pretty dialog.
You didn't get the question right. How do I execute this dialog from my c# program?

Posted: Wed 11 Jul 2007 09:26
by Alexey
This is design-time dialog. You cannot execute it from any program.

Posted: Wed 11 Jul 2007 17:03
by Alladin
Alexey wrote:This is design-time dialog. You cannot execute it from any program.
Is there a deep hidden reason to make this dialog design-time only?

Posted: Thu 12 Jul 2007 07:29
by Alexey
Because this dialog is part of our design-time infrastructure. Nowhere connection dialog is a part of data provider.

Posted: Thu 12 Jul 2007 11:11
by Alladin
It doesn't have to be a part of data provider assembly (Corelab.Oracle).

What's about Corelab.Oracle.Design.dll ? Sounds nice

Posted: Thu 12 Jul 2007 13:09
by Alexey
What's about Corelab.Oracle.Design.dll ? Sounds nice
It is already in this assembly. Could you describe what exactly you need or what is the problem? By the way, we have Connect form in our sample project. You can use it.

Posted: Fri 13 Jul 2007 18:24
by Alladin
Ok, I make this once and for all demanding ppl. Shame on CoreLab :)

Save as ConnectionStringEditor.cs

Code: Select all

using System;
using System.Windows.Forms;

namespace CoreLab.Oracle
{
  public partial class ConnectionStringEditor : Form
  {
    public ConnectionStringEditor()
    {
      InitializeComponent();

      Connection = new OracleConnectionSettings();
    }

    private OracleConnectionSettings _connection;

    public OracleConnectionSettings Connection
    {
      get
      {
        return _connection;
      }
      set
      {
        _connection = value;
        _inspector.SelectedObject = value;
      }
    }


    private void props_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
    {
      _connEdit.Text = _connection.ConnectionString;
    }

    private void button1_Click(object sender, EventArgs args)
    {
      using (OracleConnection conn = new OracleConnection(_connection.ConnectionString))
      {
        try
        {
          conn.Open();
          conn.Close();
          MessageBox.Show("Successfully connected.", "Connection Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception e)
        {
          MessageBox.Show(e.Message, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
      }
    }
  }
}
Save as ConnectionStringEditor.Designer.cs

Code: Select all

namespace CoreLab.Oracle
{
  partial class ConnectionStringEditor
  {
    /// 
    /// Required designer variable.
    /// 
    private System.ComponentModel.IContainer components = null;

    /// 
    /// Clean up any resources being used.
    /// 
    /// true if managed resources should be disposed; otherwise, false.
    protected override void Dispose(bool disposing)
    {
      if (disposing && (components != null))
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #region Windows Form Designer generated code

    /// 
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// 
    private void InitializeComponent()
    {
      this._inspector = new System.Windows.Forms.PropertyGrid();
      this._connEdit = new System.Windows.Forms.TextBox();
      this._testButton = new System.Windows.Forms.Button();
      this._okButton = new System.Windows.Forms.Button();
      this._cancelButton = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // props
      // 
      this._inspector.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                  | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this._inspector.Location = new System.Drawing.Point(8, 8);
      this._inspector.Name = "props";
      this._inspector.Size = new System.Drawing.Size(299, 420);
      this._inspector.TabIndex = 0;
      this._inspector.PropertyValueChanged += new System.Windows.Forms.PropertyValueChangedEventHandler(this.props_PropertyValueChanged);
      // 
      // _connEdit
      // 
      this._connEdit.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
      this._connEdit.BackColor = System.Drawing.SystemColors.Control;
      this._connEdit.Location = new System.Drawing.Point(8, 432);
      this._connEdit.Name = "_connEdit";
      this._connEdit.ReadOnly = true;
      this._connEdit.Size = new System.Drawing.Size(299, 23);
      this._connEdit.TabIndex = 1;
      // 
      // _testButton
      // 
      this._testButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
      this._testButton.Location = new System.Drawing.Point(8, 461);
      this._testButton.Name = "_testButton";
      this._testButton.Size = new System.Drawing.Size(111, 25);
      this._testButton.TabIndex = 2;
      this._testButton.Text = "Test Connection";
      this._testButton.UseVisualStyleBackColor = true;
      this._testButton.Click += new System.EventHandler(this.button1_Click);
      // 
      // _okButton
      // 
      this._okButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
      this._okButton.Location = new System.Drawing.Point(151, 461);
      this._okButton.Name = "_okButton";
      this._okButton.Size = new System.Drawing.Size(75, 25);
      this._okButton.TabIndex = 3;
      this._okButton.Text = "OK";
      this._okButton.UseVisualStyleBackColor = true;
      // 
      // _cancelButton
      // 
      this._cancelButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
      this._cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel;
      this._cancelButton.Location = new System.Drawing.Point(232, 461);
      this._cancelButton.Name = "_cancelButton";
      this._cancelButton.Size = new System.Drawing.Size(75, 25);
      this._cancelButton.TabIndex = 4;
      this._cancelButton.Text = "Cancel";
      this._cancelButton.UseVisualStyleBackColor = true;
      // 
      // ConnectionStringEditor
      // 
      this.AcceptButton = this._okButton;
      this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
      this.CancelButton = this._cancelButton;
      this.ClientSize = new System.Drawing.Size(315, 494);
      this.Controls.Add(this._connEdit);
      this.Controls.Add(this._cancelButton);
      this.Controls.Add(this._okButton);
      this.Controls.Add(this._testButton);
      this.Controls.Add(this._inspector);
      this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
      this.MaximizeBox = false;
      this.MinimizeBox = false;
      this.Name = "ConnectionStringEditor";
      this.Padding = new System.Windows.Forms.Padding(8);
      this.ShowIcon = false;
      this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
      this.Text = "Oracle Connection String Editor";
      this.ResumeLayout(false);
      this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.PropertyGrid _inspector;
    private System.Windows.Forms.TextBox _connEdit;
    private System.Windows.Forms.Button _testButton;
    private System.Windows.Forms.Button _okButton;
    private System.Windows.Forms.Button _cancelButton;
  }
}

Save as OracleConnectionParameters.cs

Code: Select all

using System;
using System.ComponentModel;
using System.Collections.Generic;

namespace CoreLab.Oracle
{
  public enum ConnectMode
  {
    Normal,
    SysDba,
    SysOper
  }

  public class OracleConnectionSettings : INotifyPropertyChanged
  {
    #region ConnectMode Property

    protected ConnectMode _mode = ConnectMode.Normal;

    [Category("Initialization")]
    [DisplayName("Connect Mode"), Description("Allows to open a session with administrative privileges SYSDBA or SYSOPER.")]
    [DefaultValue(ConnectMode.Normal), CSName("connect mode")]
    public ConnectMode ConnectMode
    {
      get
      {
        return _mode;
      }
      set
      {
        if (_mode != value)
        {
          _mode = value;
          NotifyChange("ConnectMode");
        }
      }
    }

    #endregion

    #region Home Property

    protected string _home = "";

    [Category("Initialization")]
    [Description("The Oracle home name.")]
    [DefaultValue(""), CSName("home")]
    public string Home
    {
      get
      {
        return _home;
      }
      set
      {
        if (_home != value)
        {
          _home = value;
          NotifyChange("Home");
        }
      }
    }

    #endregion

    #region Unicode Property

    protected bool _unicode = false;

    [Category("Initialization")]
    [Description("Specifies whether OraDirect.NET uses UTF16 mode API calls.")]
    [DefaultValue(false), CSName("unicode")]
    public bool Unicode
    {
      get
      {
        return _unicode;
      }
      set
      {
        if (_unicode != value)
        {
          _unicode = value;
          NotifyChange("Unicode");
        }
      }
    }

    #endregion

    #region ConnectionTimeout Property

    protected int _timeout = 15;

    [Category("Initialization")]
    [DisplayName("Connection Timeout"), Description("Time (in seconds) to wait for a connection to the server before terminating the attempt and generating an error.")]
    [DefaultValue(15), CSName("connection timeout")]
    public int ConnectionTimeout
    {
      get
      {
        return _timeout;
      }
      set
      {
        if (_timeout != value)
        {
          _timeout = value;
          NotifyChange("ConnectionTimeout");
        }
      }
    }

    #endregion

    #region Direct Property

    protected bool _direct = false;

    [Category("Initialization")]
    [Description("Specifies whether Oracle client is used.")]
    [DefaultValue(false), CSName("direct")]
    public bool Direct
    {
      get
      {
        return _direct;
      }
      set
      {
        if (_direct != value)
        {
          _direct = value;
          NotifyChange("Direct");
        }
      }
    }

    #endregion

    #region Pooling Property

    protected bool _pooling = true;

    [Category("Pooling")]
    [Description("If true, the connection is drawn from the appropriate pool or is created and added to the appropriate pool.")]
    [DefaultValue(true), CSName("pooling")]
    public bool Pooling
    {
      get
      {
        return _pooling;
      }
      set
      {
        if (_pooling != value)
        {
          _pooling = value;
          NotifyChange("Pooling");
        }
      }
    }

    #endregion

    #region MinPoolSize Property

    protected int _minPoolSize = 0;

    [Category("Pooling")]
    [DisplayName("Min Pool Size"), Description("The minimum number of connections allowed in the pool.")]
    [DefaultValue(0), CSName("min pool size")]
    public int MinPoolSize
    {
      get
      {
        return _minPoolSize;
      }
      set
      {
        if (_minPoolSize != value)
        {
          _minPoolSize = value;
          NotifyChange("MinPoolSize");
        }
      }
    }

    #endregion

    #region MaxPoolSize Property

    protected int _maxPoolSize = 100;

    [Category("Pooling")]
    [DisplayName("Max Pool Size"), Description("The maximum number of connections allowed in the pool.")]
    [DefaultValue(100), CSName("max pool size")]
    public int MaxPoolSize
    {
      get
      {
        return _maxPoolSize;
      }
      set
      {
        if (_maxPoolSize != value)
        {
          _maxPoolSize = value;
          NotifyChange("MaxPoolSize");
        }
      }
    }

    #endregion

    #region ConnectionLifetime Property

    protected int _lifetime = 0;

    [Category("Pooling")]
    [DisplayName("Connection Lifetime"), Description("Time interval (in seconds) during which connection is kept alive.")]
    [DefaultValue(0), CSName("connection lifetime")]
    public int ConnectionLifetime
    {
      get
      {
        return _lifetime;
      }
      set
      {
        if (_lifetime != value)
        {
          _lifetime = value;
          NotifyChange("ConnectionLifetime");
        }
      }
    }

    #endregion

    #region Username Property

    protected string _username = "";

    [Category("Security")]
    [DisplayName("User Id"), Description("The Oracle login account.")]
    [DefaultValue(""), CSName("user id")]
    public string Username
    {
      get
      {
        return _username;
      }
      set
      {
        if (_username != value)
        {
          _username = value;
          NotifyChange("Username");
        }
      }
    }

    #endregion

    #region Password Property

    protected string _password = "";

    [Category("Security")]
    [Description("The password for the Oracle login account.")]
    [DefaultValue(""), PasswordPropertyText(true), CSName("password")]
    public string Password
    {
      get
      {
        return _password;
      }
      set
      {
        if (_password != value)
        {
          _password = value;
          NotifyChange("Password");
        }
      }
    }

    #endregion

    #region SecurityInfo Property

    protected bool _security = false;

    [Category("Security")]
    [DisplayName("Persist Security Info"), Description("When false, security-sensitive information, such as the password, is not returned as part of the connection if the connection is open or has ever been in an open state.")]
    [DefaultValue(false), CSName("persist security info")]
    public bool PersistSecurityInfo
    {
      get
      {
        return _security;
      }
      set
      {
        if (_security != value)
        {
          _security = value;
          NotifyChange("PersistSecurityInfo");
        }
      }
    }

    #endregion

    #region Server Property

    protected string _server = "";

    [Category("Source")]
    [Description("The name of TNS alias of Oracle database to which to connect.")]
    [DefaultValue(""), CSName("server")]
    public string Server
    {
      get
      {
        return _server;
      }
      set
      {
        if (_server != value)
        {
          _server = value;
          NotifyChange("Server");
        }
      }
    }

    #endregion

    #region Sid Property

    protected string _sid = "";

    [Category("Source")]
    [Description("The system identifier that specifies instance name.")]
    [DefaultValue(""), CSName("sid")]
    public string Sid
    {
      get
      {
        return _sid;
      }
      set
      {
        if (_sid != value)
        {
          _sid = value;
          NotifyChange("Sid");
        }
      }
    }

    #endregion

    #region Port Property

    protected int _port = 1521;

    [Category("Source")]
    [Description("The number of port that Oracle server listens.")]
    [DefaultValue(1521), CSName("port")]
    public int Port
    {
      get
      {
        return _port;
      }
      set
      {
        if (_port != value)
        {
          _port = value;
          NotifyChange("Port");
        }
      }
    }

    #endregion

    [Browsable(false)]
    public string ConnectionString
    {
      get
      {
        List result = new List();

        PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this, new Attribute[] { new CSNameAttribute(null) });

        foreach (PropertyDescriptor prop in props)
          if (!prop.IsReadOnly && prop.ShouldSerializeValue(this))
            foreach (Attribute attr in prop.Attributes)
              if (attr is CSNameAttribute)
              {
                CSNameAttribute name = (CSNameAttribute)attr;

                result.Add(name.Name + "=" + Convert.ToString(prop.GetValue(this)));

                break;
              }

        return string.Join(";", result.ToArray());
      }
    }

    protected void NotifyChange(string name)
    {
      if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(name));
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
  }

  [AttributeUsage(AttributeTargets.Property)]
  public class CSNameAttribute : Attribute
  {
    public CSNameAttribute(string name)
    {
      _name = name;
    }

    private string _name;

    public string Name
    {
      get { return _name; }
    }

    public override bool Match(object obj)
    {
      return (obj is CSNameAttribute) && ((_name == null) || string.Equals(_name, ((CSNameAttribute)obj).Name));
    }
  }

}
Save as Program.cs

Code: Select all

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace CoreLab.Oracle
{
  static class Program
  {
    [STAThread]
    static void Main()
    {
      Application.EnableVisualStyles();

      new ConnectionStringEditor().ShowDialog();
    }
  }
}

Posted: Sat 14 Jul 2007 08:30
by Serious
Thanks for sample code. It may be useful for our customers.
We'll examine necessity of adding some kind of simple connection string editor to our demo projects.