build SQL query string using user input

Posted by user175084 on Stack Overflow See other posts from Stack Overflow or by user175084
Published on 2010-04-16T21:54:29Z Indexed on 2010/04/16 22:03 UTC
Read the original article Hit count: 224

Filed under:
|
|
|
|

i have to make a string by using the values which the user selects on the webpage

suppose i need to display files for multiple machines with differnt search criteria..

i currently use this code:

DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection();
        connection.ConnectionString = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString;
        connection.Open();
        SqlCommand sqlCmd = new SqlCommand("SELECT FileID FROM Files WHERE MachineID=@machineID and date= @date", connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);

        sqlCmd.Parameters.AddWithValue("@machineID", machineID);

        sqlCmd.Parameters.AddWithValue("@date", date);

        sqlDa.Fill(dt);

now this is fixed query where the user just has one machine and just selects one date...

i want to make a query in which the user has multiple search options like type or size if he wants depending on what he selects

also if he can select multiple machines..

SELECT FileID FROM Files WHERE (MachineID=@machineID1 or MachineID = @machineID2...)  and (date= @date and size=@size and type=@type... )

all of this happens in runtime... other wise i have to create a for loop to put multiple machines one by one... and have multiple queries depending on the case the user selected...

this is quiet interesting and i could use some help... thanks

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about c#