1

I need some advice here. I am making an application to get a particular SQL statement within a VB Form but do not know which would be the best option to do so.

For example i have this sentence on to explain:

  FROM sys.master_files WITH (NOLOCK)

But what i need is

 FROM sys.master_files

I need to get the FROM and the next thing like sys.master_files

The first moment this is just what I need, it is a basic application, only to catch the FROM and the next thing that comes.

Now I've been researching and looking at some questions that had already been made in StackOverFlow and I was wondering if the Regex.Match is a faster way to do this check or if I could use the IndexOf but the I did not understand very well how it works.

This is what i made so far on the Code.

private void btnSearch_Click(object sender, EventArgs e)
{
    string value = cboParam.Text;
    string[] reservedWords = { "FROM", "SELECT", "JOIN", "UPDATE", "DROP", "ALTER", "CREATE" };

    if (cboParam.Text == "")
    {
        MessageBox.Show("Wrong try again.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
        List<string> listParam = new List<string>();
        listParam.Add(value);
        if (selectedPath == null && openFileDialog.FileNames == null)
        {
            MessageBox.Show("Choose a directory please.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else if (selectedPath != null)
        {
            foreach (string str in selectedPath)
            {
                string split = " ";
                string readText = File.ReadAllText(str)
                    .Replace("\n", split)
                    .Replace("\r", split)
                    .Replace("_", split)
                    .Replace("-", split)
                    .Replace("SQL", split)
                    .Replace("sql", split)
                    .Replace("FROM", split)
                    .Replace("from", split)
                    .Replace("JOIN", split)
                    .Replace("join", split)
                    .Replace("UPDATE", split)
                    .Replace("update", split)
                    .Replace("DELETE", split)
                    .Replace("delete", split);
                readText.IndexOf(value);

            }
        }
        else if (openFileDialog.FileNames != null)
        {
            foreach (string str in openFileDialog.FileNames)
            {
                string split = "\nGO";
                string readText = File.ReadAllText(str) 
                    .Replace("\n", split)
                    .Replace("\r", split)
                    .Replace("_", split)
                    .Replace("-", split)
                    .Replace("SQL", split)
                    .Replace("sql", split)
                    .Replace("FROM", split)
                    .Replace("from", split)
                    .Replace("JOIN", split)
                    .Replace("join", split)
                    .Replace("UPDATE", split)
                    .Replace("update", split)
                    .Replace("DELETE", split)
                    .Replace("delete", split);

                readText.IndexOf(value);
            }
        }
    }
}

That's what I had, I was trying to use the IndexOf but can not give a continuity because I did not understand the operation of it.

For a better understanding I'm from Brazil so I changed a bit the pair variables of code into English to become "easy" to understand the code.

3 Answers3

1

That's defenetly task for regular expressions:

var input = "FROM sys.master_files WITH (NOLOCK) FROM sys1.master_files1 WITH (NOLOCK)";
var rg = new Regex(@"FROM [a-zA-Z0-9\._]+ ");
var result = rg.Matches(input);
Uriil
  • 11,948
  • 11
  • 47
  • 68
  • I "get it" but i'm passing reading a whole code in VB like 4k lines and the example are just one line. Can i just replace the example of FROM... and pass the variable that are reading all the text? – Rodolfo Olivieri Dec 19 '14 at 13:39
  • @RodolfoOlivieri yes, and regular expressions will be faster then manual text parsing – Uriil Dec 19 '14 at 13:42
  • well i'll try this thanks. I'm learning C# so it's a little complicated for me but i i'll try. – Rodolfo Olivieri Dec 19 '14 at 13:46
0

If you're looking for a regex to get that part:

string s = System.Text.RegularExpressions.Regex.Match("test FROM sys.master_files WITH (NOLOCK)", "FROM ([^ ]*)").Groups[1].Value;

Another solution with the use of Linq:

string xx = "test FROM sys.master_files WITH (NOLOCK)";
string yy = xx.Split(' ').SkipWhile(x => x != "FROM").Take(2).Last();

And i'm sure there's a ton more.

John
  • 3,627
  • 1
  • 12
  • 13
0

I'm basing my understanding of the question using your title, which says that you need the first two words of the string. Using IndexOf and Substring:

string original = "FROM sys.master_files WITH (NOLOCK)";
string firstTwoWords = original.Substring(0, original.IndexOf(' ', original.IndexOf(' ')+1));

Should get you FROM sys.master_files

What the second line does is that it gets a part of the original string that starts from index 0, and ends with the second occurrence of the space. But this won't work well with multiple spaces in between words (written by accident).

So you can do something like this that takes care of empty spaces:

    string original = "FROM sys.master_files  WITH (NOLOCK)";
    string[] array = original.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);
    string newString = array[0] +' '+ array[1];
Tyress
  • 3,573
  • 2
  • 22
  • 45
  • Well that is one exemple that i posted but how i'm working with large files of VB Form the SQL is not like that. Sometimes it can be "SELECT * FROM variable1" and go on. But i like that way i will try that too. – Rodolfo Olivieri Dec 19 '14 at 13:54