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)
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.
The maximum length of an SQL statement string is 65,000 characters.
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.
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.
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
But do be mindful of SQL Injection and use parameters as much as possible- which is pretty much always. (Edit from comments)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With