Simple ASP.NET Code to Find All the SQL Statements Used in a Project
Trying to determine which tables you've used in a project and which ones you left behind? Here's some C# to help you out.
Join the DZone community and get the full member experience.
Join For Freebackground
in a project i'm working on, like a lot of projects, we create plenty of tables. some of them are used, and some of them fall by the wayside. we planned to move the project to the server, but we ran into a problem. we couldn't find (in one shot) which 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)
{
//defining the path of directory where all files saved
string filepath = @ "d:\tpms\app_code\";
//get the all file names inside the directory
string[] files = directory.getfiles(filepath);
//loop through the files to search file one by one
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++;
//defining the keyword for search
if (sourceline.contains("from"))
{
//append the result to multiline text box
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, the line number, and the query that contains the table name.
i hope it was useful to learn how to get all you tables in one shot. kindly let me know your thoughts and feedback.
Published at DZone with permission of Karthik Elumalai. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments