Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Simple ASP.NET Code to Find All the SQL Statements Used in a Project

DZone's Guide to

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.

· Database Zone ·
Free Resource

New whitepaper: Database DevOps – 6 Tips for Achieving Continuous Delivery. Discover 6 tips for continuous delivery with Database DevOps in this new whitepaper from Redgate. In 9 pages, it covers version control for databases and configurations, branching and testing, automation, using NuGet packages, and advice for how to start a pioneering Database DevOps project. Also includes further research on the industry-wide state of Database DevOps, how application and database development compare, plus practical steps for bringing DevOps to your database. Read it now 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.

New whitepaper: Database DevOps – 6 Tips for Achieving Continuous Delivery. Discover 6 tips for continuous delivery with Database DevOps in this new whitepaper from Redgate. In 9 pages, it covers version control for databases and configurations, branching and testing, automation, using NuGet packages, and advice for how to start a pioneering Database DevOps project. Also includes further research on the industry-wide state of Database DevOps, how application and database development compare, plus practical steps for bringing DevOps to your database. Read it now free.

Topics:
c# ,sql

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}