The above page contains two TextBox controls named tbFirstname and tbLastname. When you enter values into the two Textbox controls and click Submit, the Button Click subroutine is executed. hype
The Button Click subroutine creates an instance of the OleDbCommand class with a parametric Select statement. Two instances of the OleDbParameter class are created and added to the Parameters collection of the OleDbCommand class. For example, the @firstname parameter is created and added with the following statement:
This statement creates a new OleDbParameter named @firstname and adds it to the Parameters collection of the OleDbCommand class. Passing its name and value creates the parameter. In this case, the value of the parameter is the value of the tbFirstname TextBox control.
Inserting Values in a Table [INSERT Command]
We can use INSERT command for inserting the new records in the table. The syntax of this command is: -
If we try to write this command for our employees’ table, we will write it as:
The important steps needed to execute the INSERT command is: -
1. Create and open a database connection.
2. Create a database command that represents the SQL Insert statement to execute.
3. Execute the command with the ExecuteNonQuery () method.
The following example demonstrates the INSERT command
The output of this example is shown below:
The first line in the above example imports the necessary namespaces to use the OleDb classes.
Next, a connection to an OleDb database is initialized.
Then, a variable named strInsert that has a SQL Insert statement as its value is created .The statement inserts a new record into a table named Books in database.
In the statement that follows, an instance of the OleDbCommand class is created. This class is initialized with two parameters: the command to execute and the connection to use for executing the command.
Finally, the command is executed by calling the ExecuteNonQuery () method of the OleDbCommand class. This method sends the SQL command to the database server, and the database server executes the command.
Here we have used ExecuteNonQuery () rather than ExecuteReader () to execute the command. ExecuteNonQuery () method is used because you are not returning any records from the database.
Practically Parametric Insert is used in the form of a web i.e. the parameters are passed from outside. The following example demonstrates it.