این چیزی که من تو برنامه هام استفاده می کنم
توی کد زیر SelectCommand رو تعیین میکنم
privatestring SelectData()
{
string selectCommand = null;
if (_customerID != 0)
{
selectCommand += " AND CustomerID = " + _customerID;
}
if (_firstName != null)
{
selectCommand += " AND FirstName LIKE '" + _firstName + "%'";
}
if (_lastName != null)
{
selectCommand += " AND LastName LIKE '" + _lastName + "%'";
}
if (_address != null)
{
selectCommand += " AND Address LIKE '" + _address + "%'";
}
if (_phoneNumber1 != null)
{
selectCommand += " AND PhoneNumber1 LIKE '" + _phoneNumber1 + "%'";
}
if (_phoneNumber2 != null)
{
selectCommand += " AND PhoneNumber2 LIKE '" + _phoneNumber2 + "%'";
}
if (_customerDetail != null)
{
selectCommand += " AND CustomerDetail LIKE '" + _customerDetail + "%'";
}
if (_customerSince != null)
{
selectCommand += "AND CustomerSince LIKE '" + _customerSince + "%'";
}
if (selectCommand != null)
{
return selectCommand.Remove(0, 4);
}
returnnull;
}
و توی این هم از SelectCommand ساخته شده واسه گرفتن اطلاعات استفاده میکنم
publicDataTable GetData()
{
OleDbCommand command = newOleDbCommand();
OleDbDataAdapter customerDataAdapter = newOleDbDataAdapter();
DataTable dt = newDataTable();
try
{
connection.Open();
command.Connection = connection;
string selectCommand = SelectData();
if (selectCommand != null)
{
selectCommand = "WHERE " + selectCommand + " ORDER BY CustomerID ASC";
}
else
{
selectCommand = "ORDER BY CustomerID ASC";
}
command.CommandText = "SELECT Customer.CustomerID AS [ ], FirstName AS ," +
" LastName AS [ ], Address AS , PhoneNumber1 AS [ 1], PhoneNumber2 AS [ 2]," +
" CustomerDetail AS , CustomerSince AS [ ] FROM Customer " + selectCommand;
customerDataAdapter.SelectCommand = command;
customerDataAdapter.SelectCommand.ExecuteNonQuery( );
customerDataAdapter.Fill(dt);
return dt;
}
catch
{
returnnull;
}
finally
{
connection.Close();
command.Dispose();
customerDataAdapter.Dispose();
dt.Dispose();
}
}