Search code examples
c#sqlcompiler-constructiondatatablewhere-clause

SQL to C# conversion


I have a working process, behind a web application, that is generating some records in a table, it is so general, that I actually had to add SQL (where clause) as Data in a table.

It looks something like this (don't mind the quotes):

 SET @sql = '(((1.00 < col_a) 
     AND (col_a < 5.00))) 
     AND NOT LEFT(col_b, 4) IN ('something') 
     OR ( CHARINDEX('cucu', col_c) > 0 ) 
     AND NOT ISNULL(col_d, '') <> '' 
     AND ((1.00 < CONVERT(FLOAT, col_e)) 
     AND (CONVERT(FLOAT, col_f)< 5.00)) 
     AND NOT col_g IN ('10') '

It works all right, no complaints, but the client would like to have an in-code approach, I am not really sure why. So instead of using a scheduled SQL job, running my stored procedure, I guess I will make all this to be somehow rebuilt in C# desktop application, which is doing the same thing.

I would like to know if there is a way for me to use my code (like my @sql example above) somehow from C#. I mean some kind of compile, or anything.

I thought I would just select the results in a DataTable, and THEN apply my @sql over that DataTable somehow. I would like to narrow down the original DataTable's results using that where clause.

If I don't do something like this, I am afraid I will have to change the entire saving (of @sql) process too, which would make the task even bigger.

I am using SQL 2k5, the client has sql 2k8 (we'll be upgrading soon) and the desktop app I would build in C#.


Solution

  • Do you not execute any database procs from C# currently?

    This is incredibly easy to do. Keep your SQL Code in a proc, and execute it through the SQL classes in .NET:

    http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson07.aspx