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: cmdSelect.Parameters.Add( "@firstname", tbFirstname.Text ) 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: - INSERT [into] table_name (column1, column2…) VALUES (value1, value2…) If we try to write this command for our employees’ table, we will write it as: INSERT into employees (EmployeeID,FirstName,LastName) VALUES (102, 'John', ‘Greg’ ) 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
Example25 SqlInsertDemo.aspx |
<%@ Import Namespace="System.Data.SqlClient" %>
<%
Dim conNorthwind As SqlConnection
Dim strInsert As String
Dim cmdInsert As SqlCommand
conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;database=Northwind" )
strInsert = "Insert books ( bookName,Author) Values ('Express AspNet', 'John' )"
cmdInsert = New SqlCommand( strInsert, conNorthwind )
conNorthwind. Open ()
cmdInsert. ExecuteNonQuery ()
conNorthwind. Close ()
%>
New book Added! |
| |
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.
Parametric Insert Practically Parametric Insert is used in the form of a web i.e. the parameters are passed from outside. The following example demonstrates it.
Example 27 SqlParameterInsert.aspx |
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<Script Runat="Server">
Sub Button_Click (s as Object, e As EventArgs)
Dim conNorthwind As SqlConnection
Dim strInsert As String
Dim cmdInsert As SqlCommand
conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;database=Northwind" )
strInsert = "Insert Books ( bookName, Author ) Values ( @bkName, @bkAuthor )"
cmdInsert = New SqlCommand( strInsert, conNorthwind )
cmdInsert.Parameters.Add( "@bkName", txtbookName.Text )
cmdInsert.Parameters.Add( "@bkAuthor",txtAuthor.Text)
conNorthwind. Open ()
cmdINsert. ExecuteNonQuery ()
ConNorthwind. Close ()
End Sub
</Script>
<html>
<body>
<form Runat="Server">
<h3>Add New Book Form</h3>
<b>Book's Name:</b>
<br>
<asp:TextBox
ID="txtbookName"
Runat="Server" />
<p>
<b>Author:</b>
<br>
<asp:TextBox
ID="txtAuthor"
Runat="Server" />
<p>
<asp:Button
Text="Add Books!"
OnClick="Button_Click"
Runat="Server" />
</form>
</body>
</html> |
| |
|