Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper Way to Format SQL Query Within C# Application

Tags:

c#

sql

I have a C# console application that makes a bunch of queries to a database server.

I frequently need to modify the SQL and would like to simply copy/paste the SQL from my SQL Editor into my C# source without having to reformat the SQL every time.

Currently, the SQL is all on one line... like below:

  OleDbDataAdapter da_ssm_servers = new OleDbDataAdapter(@"SELECT * FROM mytable ORDER BY Server;", connSSM);

The SQL is much longer than above with a lot of table JOINS, etc.

I would like to keep the formatting, but don't really want to have to go back and add quotes around each line, etc.

If anyone has any recommendations and examples, it would be appreciated.

like image 788
user500741 Avatar asked Dec 27 '22 08:12

user500741


1 Answers

I do it like this:

string sql = @"
    SELECT * 
    FROM mytable 
    ORDER BY Server";
OleDbDataAdapter da_ssm_servers = new OleDbDataAdapter(sql, connSSM); 
like image 200
D'Arcy Rittich Avatar answered Jan 11 '23 02:01

D'Arcy Rittich