DZone
Database Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > Simple ASP.NET Code to Find All the SQL Statements Used in a Project

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.

Karthik Elumalai user avatar by
Karthik Elumalai
·
Jul. 30, 16 · Database Zone · Tutorial
Like (2)
Save
Tweet
2.60K Views

Join the DZone community and get the full member experience.

Join For Free

background

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.

ASP.NET sql

Published at DZone with permission of Karthik Elumalai. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Refactoring Java Application: Object-Oriented And Functional Approaches
  • Evolving Domain-Specific Languages
  • How to Minimize Software Development Cost
  • A Smarter Redis

Comments

Database Partner Resources

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo