Hellow, I have problem like this. I have SQL query where with button press need to add brackets.
Sample of SQL: SELECT something FROM sometable WHERE item1 OR item2 XOR item3 AND item4
Whan I press the botton it's adds brackets that SQL is right.
Like this: SELECT something FROM sometable WHERE (((item1 OR item2) XOR item3) AND item4)
Please help me.
Loading
FroglegPosted Feb 17, 2011, 5:04 AM
public void addBrkts()
{
string str = richTextBox1.Text;
richTextBox1.Text = ("");
string[] strsp = str.Split(' ');
int strLen = strsp.Length ;
if (strLen != 12)
{
return;
}
string strAdd = strsp[5].Insert(0, "(((");
strsp[5] = strAdd;
string strAdd2 = strsp[7].Insert(strsp[7].Length , ")");
strsp[7] = strAdd2;
string strAdd3 = strsp[9].Insert(strsp[9].Length, ")");
strsp[9] = strAdd3;
string strAdd4 = strsp[11].Insert(strsp[11].Length, ")");
strsp[11] = strAdd4;
foreach (string s in strsp)
{
richTextBox1.AppendText(s);
richTextBox1.AppendText(" ");
}
}
Janis PeksaPosted Feb 17, 2011, 7:09 AM
public void button1_Click(object sender, EventArgs e)
{
string abc = richTextBox1.Text.Trim();
richTextBox1.Text = abc.Replace(" and ", ") and ((");
string abc2 = richTextBox1.Text.Trim();
if (abc2.Contains(" where ") && (abc2.Contains(" and ") || abc2.Contains(" or ")))
{
richTextBox1.Text = abc2.Replace(" where ", " where ((");
}
int len = richTextBox1.Text.Length;
string bca = richTextBox1.Text;
string word1 = "and";
Regex r = new Regex(@"\b" + word1 + @"\b", RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(richTextBox1.Text);
int mcSum = mc.Count;
if (bca.Contains(" where ") && (bca.Contains(" and ")))
{
for (int i = 0; i < mcSum + 1; i++)
{
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
}
if (mcSum == 1)
{
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
}
}
}
Janis PeksaPosted Feb 17, 2011, 6:07 AM
I'm not sure that my code is correct.
FroglegPosted Feb 17, 2011, 6:03 AM
So you are OK now ?
Janis PeksaPosted Feb 17, 2011, 5:50 AM
I just wanna know is my code is correct.
For any SQL that have been entered in richtextbox.
FroglegPosted Feb 17, 2011, 5:43 AM
string abc = richTextBox1.Text;
richTextBox1.Text = abc.Replace(" WHERE ", " WHERE (((");
string abc2 = richTextBox1.Text;
richTextBox1.Text = abc2.Replace(" XOR ", ") XOR ");
string abc3 = richTextBox1.Text;
richTextBox1.Text = abc3.Replace(" AND ", ") AND ");
richTextBox1.AppendText(")");
Janis PeksaPosted Feb 17, 2011, 5:35 AM
FroglegPosted Feb 17, 2011, 5:27 AM
Janis PeksaPosted Feb 17, 2011, 5:24 AM
But I have SELECT a FROM b WHERE ... etc ....
This spaces don't need brackets.
So your code is not usefull.
Janis PeksaPosted Feb 17, 2011, 4:44 AM
Janis PeksaPosted Feb 17, 2011, 4:42 AM
if (mcSum == 1)
{
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
}
Janis PeksaPosted Feb 17, 2011, 4:31 AM
public void button1_Click(object sender, EventArgs e)
{
string abc = richTextBox1.Text.Trim();
richTextBox1.Text = abc.Replace(" and ", ") and ((");
string abc2 = richTextBox1.Text.Trim();
if (abc2.Contains(" where ") && (abc2.Contains(" and ") || abc2.Contains(" or ") || abc2.Contains(" xor ")))
{
richTextBox1.Text = abc2.Replace(" where ", " where ((");
}
string abc3 = richTextBox1.Text.Trim();
richTextBox1.Text = abc3.Replace(" xor ", ") xor ((");
int len = richTextBox1.Text.Length;
string bca = richTextBox1.Text;
string word1 = "and";
string word2 = "xor";
Regex r = new Regex(@"\b" + word1 + @"\b", RegexOptions.IgnoreCase);
MatchCollection mc = r.Matches(richTextBox1.Text);
Regex r2 = new Regex(@"\b" + word2 + @"\b", RegexOptions.IgnoreCase);
MatchCollection mc2 = r2.Matches(richTextBox1.Text);
int mcSum = mc.Count + mc2.Count;
if (bca.Contains(" where ") && (bca.Contains(" and ") || bca.Contains(" xor ")))
{
for (int i = 0; i < mcSum; i++)
{
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
}
if (mcSum == 1)
{
richTextBox1.Text = richTextBox1.Text.Insert(len, ")");
}
}
}
Janis PeksaPosted Feb 17, 2011, 2:26 AM
Janis PeksaPosted Feb 16, 2011, 3:18 PM
So I need this that my SQL query is right. Thats why I need this brackets to be added.
Suthish NairPosted Feb 16, 2011, 12:40 PM
Janis PeksaPosted Feb 16, 2011, 7:51 AM