The following procedures describe a simple application that allows you to pass a parameter value at runtime to a DataSet. Parameters allow you to create applications at design time without knowing specifically what data the user will enter at runtime. This example process assumes that you already have your sample Interbase Employee database set up and connected. For purposes of illustration, this example uses the default connector IBConn1, which is set to a standard location. Your database location may differ.
New
Windows Forms Application for either Delphi for .NET or C#. The Windows Forms designer appears. SELECT EMP_NO, FIRST_NAME, LAST_NAME, SALARY FROM EMPLOYEE WHERE FIRST_NAME = ?;
As you can see, this statement is limiting the number of fields. It also contains a ? character as part of the Where clause. The ? character is a wildcard that represents the parameter value that your application passes in at runtime. There are at least two reasons for using a parameter in this way. The first reason is to make the application capable of retrieving numerous instances of the data in the selected columns, while using a different value to satisfy the condition. The second reason is that you may not know the actual values at design time. You can imagine how limited the application might be if we retrieved only data where FIRST_NAME = 'Bob'.
bdpSelectCommand1.Close();
/* This closes the command to make sure that we will pass the parameter to */
/* the most current bdpSelectCommand. */
bdpDataAdapter1.Active = false;
/* This clears the data adapter so that we don't maintain old data */
bdpSelectCommand1.Parameters["emp"].Value = textBox1.Text;
/* This sets the parameter value to whatever value is in the text field. */
bdpDataAdapter1.Active = true;
/* This re-activates the data adapter so the refreshed data appears in the data grid. */
Self.bdpSelectCommand1.Close(); /* This closes the command to make sure that we will pass the parameter to */ /* the most current bdpSelectCommand. */ Self.BdpDataAdapter1.Active := false; /* This clears the data adapter so that we don't maintain old data */ Self.bdpSelectCommand1.Parameters['emp'].Value := textBox1.Text; /* This sets the parameter value to whatever value is in the text field. */ Self.BdpDataAdapter1.Active := true; /* This re-activates the data adapter so the refreshed data appears in the data grid. */
If you have changed the names of any of these items, you need to update these commands to reflect the new names.
|
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
|
What do you think about this topic? Send feedback!
|