Background
In our project we created plenty of tables and for some reason, some of them are used in our project and some of them are not used. We planned to move the project to the server, but we couldn't find in one shot what tables exactly were used in the project.
In a normal case we should find them manually one by one by using the find command. It was a very time consuming process. I tried to get some of the tools to find and display the result, but i couldn't get any so I decided to write this small tool to find them.
Aspx code
- <asp:Button ID="btnSqlfinder" runat="server" Text="SQL Finder" OnClick="btnSqlfinder_Click"/>
- <asp:TextBox ID="TxtResult" runat="server" TextMode="MultiLine" Height="1000px" Width="1000px" ></asp:TextBox>
Code behind code
- protected void btnSqlfinder_Click(object sender, EventArgs e)
- {
-
- string filepath = @ "D:\TPMS\App_Code\";
-
- string[] files = Directory.GetFiles(filepath);
-
- for (int i = 0; i < files.Length; i++)
- {
- string sourcefilename = files[i];
- StreamReader sr = File.OpenText(sourcefilename);
- string sourceline = "";
- int lineno = 0;
- while ((sourceline = sr.ReadLine()) != null)
- {
- lineno++;
-
- if (sourceline.Contains("from"))
- {
-
- TxtResult.Text += sourcefilename + lineno.ToString() + sourceline + System.Environment.NewLine;
- }
- if (sourceline.Contains("into"))
- {
- TxtResult.Text += sourcefilename + lineno.ToString() + sourceline + System.Environment.NewLine;
- }
- if (sourceline.Contains("set"))
- {
- TxtResult.Text += sourcefilename + lineno.ToString() + sourceline + System.Environment.NewLine;
- }
- if (sourceline.Contains("delete"))
- {
- TxtResult.Text += sourcefilename + lineno.ToString() + sourceline + System.Environment.NewLine;
- }
- }
- }
- }
Output
Once we click the above button, it will display the path of the file where the file is saved, line no., and the query contains the table name.
Hope it was useful to learn how to get all the tables in one shot. Kindly let me know your thoughts and feedback.