Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all. My first question after a long time. excuse me for such basic questions cause i am learning/refreshing it.
I have a winform app which has checkbox controls in it. The names of the checkboxes matches column names of a table. I can not normalize the tables cause of huge data involved, already received for the live project. so everything stays as it is. I get the selected checbox names as a csv col1,col2,col3 which later i concatenate it to sql string.(no SPs as its a sql compact 3.5 sdf dbase). In my GetData() method of the DataAccess class i form the sql string. But to avoid sql injections how can ensure that the column names passed are validated.
Could any help how following can be extended?

C#
public static DataTable GetDataPostsCars(string selectedMPs, DateTime fromDateTime, DateTime toDateTime)
{
   var table = new DataTable();
   #region debug block
   //string[] cols = selectedMPs.Split(','); //converts to array
   //object[] cols2 = cols;//gets as object array            
   //string sql = String.Format("SELECT {0} FROM GdRateFixedPosts", cols); // does not work like that?
   #endregion
   string sql = string.Format(
                "SELECT " + selectedMPs + " " +
                "FROM GdRateFixedPosts " +
                "WHERE MonitorDateTime BETWEEN '" + fromDateTime + "' AND '" + toDateTime + "'");

   if (FkDbConnection.conn != null && FkDbConnection.conn.State == ConnectionState.Open)
   {
      using (SqlCeCommand cmd = new SqlCeCommand(sql, FkDbConnection.conn))
      {
         cmd.CommandType = CommandType.Text;
         //cmd.Parameters.Add("@fromDateTime",DbType.DateTime);
         //cmd.Parameters.Add("@toDateTime",DbType.DateTime);
         table = FkDbConnection.ExecuteSelectCommand(cmd);
      }
   }
   return table;
 }
Posted

1 solution

Hi,
It will be good to create a Stored procedure and pass your Column and date condition to the store procedure.
here i have made a sample Store procedure for you.Hope this will help you.

SQL
-- exec USP_GdRateFixedPosts 'Item_Code,item_Name','2014-12-12 08:18:14.740','2014-12-12 08:18:14.740'
Create PROCEDURE [dbo].[USP_GdRateFixedPosts ]                                              
   (                                
          @MyColumns AS NVARCHAR(MAX)     = '' ,
          @fromDateTime    AS DateTime,
          @ToDateTime as DateTime
      )                                                        
AS                                                                
BEGIN                                                 
 DECLARE    @SQLquery  AS NVARCHAR(MAX)

set @SQLquery = N'SELECT ' + @MyColumns + N' FROM GdRateFixedPosts 
            where MonitorDateTime  BETWEEN ' + @fromDateTime + ' and  ' + @ToDateTime + ''
            
exec sp_executesql @SQLquery;
    END
 
Share this answer
 
Comments
shantiom 15-Dec-14 6:17am    
Hi Syed. Thanks for replying. I would have loved to do it via SP. But unfortunately as stated in my original comment .."(no SPs as its a sql compact 3.5 sdf dbase)" SPs are not supported for sql server compact databases. The project requirement insists only on sql server Ce and not express or so on. Its a standalone desktop application installable from a media. So only possibility is passing a dynamic query in C# code. any tips as to how those columns can be validated (by name, type etc) from C# code itself? thnx in advance

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900