Hi
You are right; the query builder does need a connection. But only add a connection string is not enough. As far as I know the build-in query builder is designed for IDbCommand. You can custom a query builder for yourself. Try something like the following.
The code for UserControl:
Code Block
public partial class UCCommandText : UserControl
{
string m_CommandText;
[Editor(typeof(CommandTextUIEditor), typeof(UITypeEditor))]
public string CommandText
{
get { return m_CommandText; }
set { m_CommandText = value; }
}
private string _connectionstring;
[RefreshProperties(RefreshProperties.All), DefaultValue(""), Editor("Microsoft.VSDesigner.Data.SQL.Design.SqlConnectionStringEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public string Connectionstring
{
get { return _connectionstring; }
set { _connectionstring = value; }
}
}
The code for CommandTextUIEditor:
Code Block
class CommandTextUIEditor : UITypeEditor
{
private IWindowsFormsEditorService edSvc = null;
protected virtual void SetEditorProps(UCCommandText editingInstance, UCCommandText editor)
{
if (editingInstance != null)
{
editor.Connectionstring = editingInstance.Connectionstring;
editor.CommandText = editingInstance.CommandText;
}
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (context != null && context.Instance != null &&
provider != null)
{
edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
UCCommandText command = new UCCommandText();
SetEditorProps((UCCommandText)context.Instance, command);
CusCommandTextEditor editor = new CusCommandTextEditor(command);
edSvc.ShowDialog(editor);
if (editor.DialogResult == DialogResult.OK)
{
value = command.CommandText;
}
}
}
return value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
if (context != null && context.Instance != null)
{
return UITypeEditorEditStyle.Modal;
}
return base.GetEditStyle(context);
}
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return false;
}
Hi,
And here is the code for CusCommandTextEditor Form:
Code Block
public partial class CusCommandTextEditor : Form
{
public CusCommandTextEditor()
{
InitializeComponent();
}
DataTable dsResultdt = new DataTable();
private UCCommandText command;
public CusCommandTextEditor(UCCommandText command)
: this()
{
this.command = command;
this.txtCommandText.Text = command.CommandText;
}
private void execute()
{
SqlConnection connection = new SqlConnection(command.Connectionstring);
try
{
//clear the data table
this.dsResultdt.Clear();
this.dsResultdt.Columns.Clear();
// Open connection
connection.Open();
// Update CommandText
this.command.CommandText = this.txtCommandText.Text;
// Execute command & fill result
SqlDataAdapter adapter = new SqlDataAdapter(this.command.CommandText, connection);
adapter.Fill(dsResultdt);
// Set result table
this.dataGridView1.DataSource = this.dsResultdt;
}
catch (Exception ex)
{
this.dsResultdt.Clear();
this.lblError.Text = ex.Message;
}
finally
{
connection.Close();
}
}
private void btnExecute_Click(object sender, EventArgs e)
{
this.execute();
}
private void btnOk_Click(object sender, EventArgs e)
{
this.command.CommandText = this.txtCommandText.Text;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void txtCommandText_TextChanged(object sender, EventArgs e)
{
if (this.txtCommandText.Text != null &&
this.txtCommandText.Text.Trim().Length > 0)
{
this.btnExecute.Enabled = true;
}
else
{
this.btnExecute.Enabled = false;
}
}
}
Hope this helps. Best regards. Rong-Chun Zhang |