Executing Transact-SQL INSERT statement using SQLCommand
Following example displays us how to send a query using an SQLCommand. We execute a simple Transact-SQL INSERT statement to show all rows from database. I have used Nowthwind sample database for this sample.
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQLClient" %>
<script language="VB" runat="server">
Sub Page_Load(Source as Object, E as EventArgs)
Dim cnn As New SQLConnection("server=LOCALHOST;User id=SA;password=;database=Northwind")
Dim InsertCommand As SqlCommand = New SqlCommand()
InsertCommand.Connection = cnn
Dim sql As String
sql = "INSERT INTO categories (categoryName) VALUES (@newCatName)"
InsertCommand.CommandText = sql
InsertCommand.Parameters.Add("@newCatName", SqlDbType.NVarChar, 250).Value = "Category Name"
Try
cnn.Open()
InsertCommand.ExecuteNonQuery()
Catch ex As Exception
response.Write(ex.ToString())
Finally
cnn.Close()
End Try
end sub
</script>



