I am trying to generate a dataset from a query that has a where
statement. If the user passes a value additional things need to be added to the where
statement. Is this the best way to go about this?
class TableAdapters
{
public class FindScriptByTargetVDN
{
public static DataSet Fill(SqlConnection connection, string parameter)
{
string conditional = string.Empty;
if (parameter.Length > 0)
{
conditional = "and svce.PeripheralNumber= " + parameter + " ";
}
string query = "select svce.PeripheralNumber AS Service, "
+ "ms.EnterpriseName AS [Script Name], "
+ "p.EnterpriseName AS Peripheral, "
+ "sg.PeripheralNumber AS [Skill Mapping], "
+ "s.Version AS [Latest Version], "
+ "s.DateTime AS Created, "
+ "s.Author "
+ "from Script_Cross_Reference scr "
+ "LEFT OUTER JOIN Script s on s.ScriptID = scr.ScriptID "
+ "LEFT OUTER JOIN Master_Script ms on s.MasterScriptID = ms.MasterScriptID "
+ "LEFT OUTER JOIN Service svce on svce.SkillTargetID = scr.ForeignKey "
+ "LEFT OUTER JOIN Peripheral p on svce.PeripheralID = p.PeripheralID "
+ "LEFT OUTER JOIN Service_Member sm on svce.SkillTargetID = sm.ServiceSkillTargetID "
+ "LEFT OUTER JOIN Skill_Group sg on sg.SkillTargetID = sm.SkillGroupSkillTargetID "
+ "where s.Version = ms.CurrentVersion and scr.TargetType = 1 "
+ conditional
+ "order by svce.PeripheralNumber, ms.EnterpriseName, p.EnterpriseName";
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataSet dataSet = new DataSet();
dataSet.Tables.Add(new DataTable());
adapter.Fill(dataSet.Tables[0]);
return dataSet;
}
}
}
Here is an example of the call:
this.FindScriptsDataGrid.DataSource = TableAdapters.FindScriptByTargetVDN.Fill(connection, this.ValueTextBox.Text).Tables[0];
The connection is defined before the call. The reason for the some what round about method is the user has the option of defining what server they want to run this query on before they run it.