Executing Transact SQL UPDATE statement using SQLCommand

posted by Scudyen date: Thursday, May 22, 2008 category: ASP.NET

 

Following example displays us how to send a query using an SQLCommand. We execute a simple Transact-SQL UPDATE 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 UpdateCommand As SqlCommand = New SqlCommand()
UpdateCommand.Connection = cnn
Dim sql As String
sql = "Update categories Set categoryName = @CatName WHERE CategoryID = @CatID"

UpdateCommand.CommandText = sql

UpdateCommand.Parameters.Add("@CatName", SqlDbType.NVarChar, 250).Value = "Category Name"
UpdateCommand.Parameters.Add("@CatID", SqlDbType.Int).Value = 14
Try
cnn.Open()
UpdateCommand.ExecuteNonQuery()
Catch ex As Exception
response.Write(ex.ToString())
Finally
cnn.Close()
End Try


end sub
</script>


Read More

Custom Search



Gazatem Technologies Active News Manager


page counter

Listed with 411asp.net Listed with 4aspin.com