public static BindingSource GetRecordsById(int id)
{
//query to execute
string query = "SELECT * FROM YourTable WHERE YourID = @id";
//OleDbConnection Object to use
OleDbConnection cnGetRecords = new OleDbConnection(amaDBHelper.GetConnectionString("YourConnectionName"));
//OleDbCommand Object to use
OleDbCommand cmdGetRecords = new OleDbCommand();
//OleDbDataAdapter Object to use
OleDbDataAdapter daAgents = new OleDbDataAdapter();
DataSet dsGetRecords = new DataSet();
//Clear any parameters
cmdGetRecords.Parameters.Clear();
try
{
//set the OleDbCommand Object Parameters
cmdGetRecords.CommandText = query; //tell it what to execute
cmdGetRecords.CommandType = CommandType.Text; //tell it its executing a query
//heres the difference from the last method
//here we are adding a parameter to send to our query
//you use the AddWithValue, then the name of the parameter in your query
//then the variable that holds that value
cmdGetRecords.Parameters.AddWithValue("@year", id);
//set the state of the OleDbConnection Object
DataAccess.HandleConnection(cnGetRecords);
//create BindingSource to return for our DataGrid Control
BindingSource oBindingSource = DataAccess.GetBindingSource(cmdGetRecords);
//now check to make sure a BindingSource was returned
if (!oBindingSource == null)
{
//return the binding source to the calling method
return oBindingSource;
}
else //no binding source was returned
{
//let the user know the error
throw new exception("There was no BindingSource returned");
}
}
catch (Exception ex)
{
MsgBox(ex.Message, "Error Retrieving Data");
}
//now we close the connection
DataAccess.HandleConnection(cnGetRecords);
}
|