using OleDbDataReader class to retrieve a value based on one or more variable values C#.net 2005 winform app
string staff_no = staffDataGridView.CurrentRow.Cells[0].Value.ToString();
string staff_id="";
using (OleDbConnection connection = new OleDbConnection(Properties.Settings.Default.HPMConnectionString))
{
//OleDbCommand findStaffIDCommand = new OleDbCommand("SELECT staff_id FROM staff WHERE staff_no = '" + staff_no + "'", connection);
OleDbCommand findStaffIDCommand = new OleDbCommand( "SELECT staff_id FROM staff WHERE staff_no = @staff_no",connection );
findStaffIDCommand.Parameters.AddWithValue( "@staff_no" , staff_no );
connection.Open();
OleDbDataReader reader = findStaffIDCommand.ExecuteReader();
while (reader.Read())
{
staff_id = (reader[0].ToString());
}
reader.Close();
}
MessageBox.Show( staff_id.ToString() );
2 comments:
If you just want a single value returned from your query, have you tried ExecuteScalar instead?
yup, it also works, thx for the contribution of more efficient ideas, Please leave your name if you like so that I know who to thank :)
Post a Comment