we can gerate dynamic query from datset by following function,
but remember the passing dataset should have same table name as Database,
we can give the Dataset table name at fill time,eg. adapter.fill(Ds,"Users");
public string GetCommandText(DataSet Ds1)
{
bool esc = true;
string tabname = Ds1.Tables[0].TableName;
StringBuilder QueryText = new StringBuilder("Select ");
foreach (DataColumn dc in Ds1.Tables[0].Columns)
{
if (esc)
{
QueryText.Append(dc.ColumnName);
}
else
{
QueryText.Append("," + dc.ColumnName);
}
esc = false;
}
QueryText.Append(" from ");
QueryText.Append(tabname);
string Pk = Ds1.Tables[0].PrimaryKey[0].ColumnName;
StringBuilder sbWhere = new StringBuilder();
sbWhere.Append(" Where ");
sbWhere.Append(Pk);
sbWhere.Append(" In (");
foreach (DataRow dr in Ds1.Tables[0].Rows)
{
sbWhere.Append(dr[Pk]);
sbWhere.Append(",");
}
sbWhere.Remove(sbWhere.Length - 1, 1);
sbWhere.Append(")");
QueryText.Append(sbWhere.ToString());
Console.WriteLine(QueryText.ToString());
return QueryText.ToString();
}
Wednesday, March 4, 2009
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment