Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

better way to store long SQL strings in C#

Tags:

c#

sql

I have searched around for a while for this and have not found anything.

I am storing some pretty long SQL select strings (a shorter one like this:)

      string mySelectQuery = "select distribution_stop_information.unique_id_no as stop_unique_id_no,distribution_line_items.unique_id_no as line_unique_id_no, stop_name, stop_address,route_code AS RouteCode, customer_reference," +
            "distribution_line_items.datetime_created, rma_number from distribution_stop_information join distribution_line_items on " +
            "distribution_line_items.unique_id_no = distribution_stop_information.unique_id_no " +
            "where distribution_line_items.datetime_created > '2/22/2017' and customer_no = '91000'";

then passing them by

var Ourstops = (List<stop_data>)db.Query<stop_data>(mySelectQuery);

This is cumbersome and produces hard to read/debug code.

What are some better ways of doing this?

Just a point of clairification - on this project I am not allowed to create any sprocs. Strictly a query only use type, and using postgresql (not that that matters that much here)

like image 259
Joe Ruder Avatar asked Feb 24 '17 02:02

Joe Ruder


People also ask

How can store long string in SQL Server?

n defines the string length and can be a value from 1 through 8,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB). DECLARE @longText varchar(max); SET @longText = REPLICATE('X', 8000); SET @longText = @longText + REPLICATE('X', 8000); SELECT DATALENGTH(@longText); Returns 16 K of characters.

How long can a SQL query string be?

The maximum length of an SQL statement string is 65,000 characters.

Can a SQL query be too long?

Issue. When adding a calculated field to a view, the following error may occur: The query is too large. The maximum standard SQL query length is 1024.00K characters, including comments.

How do you store SQL queries?

Use the Query Store page in SQL Server Management StudioIn Object Explorer, right-click a database, and then select Properties. Requires at least version 16 of Management Studio. In the Database Properties dialog box, select the Query Store page.


3 Answers

This is my preferred formatting, just one guy's opinion:

string mySelectQuery = @"
    select 
        distribution_stop_information.unique_id_no as stop_unique_id_no
        ,distribution_line_items.unique_id_no as line_unique_id_no, stop_name
        ,stop_address,route_code AS RouteCode, customer_reference
        ,distribution_line_items.datetime_created, rma_number 
    from 
        distribution_stop_information 
        join distribution_line_items on distribution_line_items.unique_id_no = distribution_stop_information.unique_id_no
    where 
        distribution_line_items.datetime_created > '2/22/2017' and customer_no = '91000'
";

Benefits

  • Copy and paste right into sql management studio
  • Using the @ (verbatim literal) eliminates all the quotes and concatenations
  • Easy to also use $ for string interpolation
  • commas in front make commenting lines out easier

But do be mindful of SQL Injection and use parameters as much as possible- which is pretty much always. (Edit from comments)

like image 154
travis.js Avatar answered Oct 20 '22 00:10

travis.js


  • Stored procs or views (SQL specific)
  • Resources
  • Use of @ to allow line breaks
  • Configuration files
  • Content management system (if you have one, and even then not too sure about that)
  • Entity Framework so you don't have SQL (I'm not a fan BTW. I'd go SPs)
like image 23
LoztInSpace Avatar answered Oct 20 '22 01:10

LoztInSpace


My favourite question! Like you perhaps, I find SQL in string literals the weirdest thing in programming. So weird that I went off and wrote QueryFirst, a visual studio extension for working intelligently with SQL. Your SQL lives in a .sql file, like god intended. You edit it with the marvellous TSQL editor, connected to the database with intellisense for tables and columns and syntax validation. Every time you save the file, QueryFirst checks that the query runs, then (re)generates the C# wrapper that lets you use it. Behind the scenes, QueryFirst compiles your SQL into the binary, and accesses it with GetManifestResourceStream. All your data access is continually integration tested and working, and you have no more SQL hanging around in string literals.

like image 41
bbsimonbb Avatar answered Oct 20 '22 00:10

bbsimonbb