// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Drawing; using System.Windows.Forms; using System.Collections; using System.Data; using System.Data.SqlClient; namespace E { public class MainForm : Form { private TextBox textBox1; private TextBox textBox2; private TextBox textBox3; private Button button1; public MainForm() { InitializeComponent(); } [STAThread] public static void Main(string[] args) { Application.Run(new MainForm()); } private void InitializeComponent() { textBox1 = new TextBox(); textBox2 = new TextBox(); textBox3 = new TextBox(); button1 = new Button(); SuspendLayout(); textBox1.Location = new Point(25, 25); textBox1.Size = new Size(100, 25); textBox2.Location = new Point(25, 75); textBox2.Size = new Size(100, 25); textBox3.Location = new Point(25, 125); textBox3.Size = new Size(100, 25); button1.Location = new Point(25, 175); button1.Size = new Size(100, 25); button1.Text = "Test"; button1.Click += new System.EventHandler(Button1Click); Size = new Size(150, 275); Controls.Add(textBox1); Controls.Add(textBox2); Controls.Add(textBox3); Controls.Add(button1); Text = "Demo"; ResumeLayout(false); } private SqlCommand BuildCommandInGui() { SqlCommand res = new SqlCommand(); res.CommandText = "SELECT * FROM t1"; int n = 0; if(textBox1.Text != "") { res.CommandText += (n > 0 ? " AND" : " WHERE"); res.CommandText += " f1 = @f1"; res.Parameters.Add("@f1", SqlDbType.VarChar, 50); res.Parameters["@f1"].Value = textBox1.Text; n++; } if(textBox2.Text != "") { res.CommandText += (n > 0 ? " AND" : " WHERE"); res.CommandText += " f2 = @f2"; res.Parameters.Add("@f2", SqlDbType.VarChar, 50); res.Parameters["@f2"].Value = textBox2.Text; n++; } if(textBox3.Text != "") { res.CommandText += (n > 0 ? " AND" : " WHERE"); res.CommandText += " f3 = @f3"; res.Parameters.Add("@f3", SqlDbType.VarChar, 50); res.Parameters["@f3"].Value = textBox3.Text; n++; } return res; } private string BuildStringInGui(Hashtable args) { string res = "SELECT * FROM t1"; int n = 0; if(textBox1.Text != "") { res += (n > 0 ? " AND" : " WHERE"); res += " f1 = @f1"; args.Add("@f1", textBox1.Text); n++; } if(textBox2.Text != "") { res += (n > 0 ? " AND" : " WHERE"); res += " f2 = @f2"; args.Add("@f2", textBox2.Text); n++; } if(textBox3.Text != "") { res += (n > 0 ? " AND" : " WHERE"); res += " f3 = @f3"; args.Add("@f3", textBox3.Text); n++; } return res; } private SqlCommand ConvertStringToCommandInDal(string sql, Hashtable args) { SqlCommand res = new SqlCommand(sql); foreach(DictionaryEntry ent in args) { res.Parameters.Add((string)ent.Key, SqlDbType.VarChar, 50); res.Parameters[(string)ent.Key].Value = ent.Value; } return res; } private void Button1Click(object sender, System.EventArgs e) { SqlCommand v1 = BuildCommandInGui(); string dbg1 = v1.CommandText + " / "; for(int i = 0; i < v1.Parameters.Count; i++) { dbg1 += (" " + v1.Parameters[i].ParameterName + "=" + v1.Parameters[i].Value); } MessageBox.Show(dbg1); Hashtable args = new Hashtable(); string s2 = BuildStringInGui(args); SqlCommand v2 = ConvertStringToCommandInDal(s2, args); string dbg2 = v2.CommandText + " / "; for(int i = 0; i < v2.Parameters.Count; i++) { dbg2 += (" " + v2.Parameters[i].ParameterName + "=" + v2.Parameters[i].Value); } MessageBox.Show(dbg2); } } }