Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for inline SQL queries

I'm working with an asp.net website that uses a lot of inline SQL queries... and I'm wondering if it is best to create the inline queries on the fly:

int i = 500;

    using (SqlConnection conn = new SqlConnection(connStr))
    {
        SqlCommand com = new SqlCommand(conn);
        ...
        com.CommandText = "select from table where column < @parameter";
        ...
    }

Or to have a class to hold all queries needed for the application. Something like this:

class SqlQueries
{
    private string query1 = 
          "select * from tblEmployees where EmployeeName = @EmployeeName";

    private string query2 = 
          "select * from tblVacation where EmployeeName = @EmployeeName";

    public string Query(string s) 
    { 
        string str = string.Empty;

            switch (s) 
            {
                case "query1":
                    str = query1; 
                    break; 
                case "query2":
                    str = query2; 
                    break;
            }     

    return str;    

    }
}

Thank you!

like image 264
user1481183 Avatar asked Oct 05 '12 16:10

user1481183


People also ask

Which is better inline query or stored procedure?

every query is submited it will be compiled & then executed. where as stored procedure is compiled when it is submitted for the first time & this compiled content is stored in something called procedure cache,for subsequent calls no compilation,just execution & hence better performance than query.

Why stored procedure performs better than an inline query?

Since stored procedure is saved on a database level, sharing of application logic between applications is easier than using libraries or APIs. It is easier to troubleshoot a stored procedure than inline query as we can isolate it.


3 Answers

I've used a lot of ADO.NET queries in my day and I have always used the first method. The second method is an interesting idea, but it might be cumbersome to edit those queries if you are at another place in the code that uses it. It also makes it harder to see what a query is doing at a particular place in code. Example:

string sql = "Update User set age = @age where UserId = @UserId";

tells a developer what is happening, while:

string sql = SqlQueries.Query("updateAge");

Leaves questions about what table/columns are being updated. Also, with the first one, you know exactly what params need to be added.

If you are writing this query in several places that might change things

like image 133
Abe Miessler Avatar answered Sep 27 '22 18:09

Abe Miessler


If you absolutely have to have "inline" sql as opposed to stored procedures (and I have done this for utility type applications that merely interact with a database, rather than own it), I would suggest putting your SQL into an embedded resource file. This will make your queries easier to maintain (although you will still need to re-compile your app to make changes).

like image 29
James Curtis Avatar answered Sep 27 '22 19:09

James Curtis


It's not terrible to put the literal directly in the method, as long as you always call that same method every time you want to run that query. However, if you are going to copy that string literal into multiple places in your code, then a constant is definitely preferred. However, rather than taking a string as the argument for the Query method in your second example, it should take an enumeration value.

However, if you are using the second method you described, I would ask you why you don't just start using stored procedures instead?

like image 37
Steven Doggart Avatar answered Sep 27 '22 18:09

Steven Doggart