Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

ASP.NET: Insert data into SQL Server

From Wiki

Jump to: navigation, search


Summary: Insert data into SQL Server using a stored procedure and parameters

The SQL Server table:

  1. CREATE TABLE table1 (id int identity(1,1), myvalue int)

The SQL Server Stored procedure:

  1. CREATE procedure MyStoredProcedure
  2. (
  3.  @MyParameter int
  4. )
  5. AS
  6. INSERT table1 (myvalue) VALUES (@MyParameter)

The ASP.NET commands to execute the strored procedure:

  1. ' Declarations
  2. Dim cn As New SqlConnection("Your connection string")
  3. Dim cmd As New SqlCommand("MyStoredProcedure", cn)
  4.  
  5. ' Open the connection and add a parameter
  6. cn.Open()
  7. cmd.CommandType = CommandType.StoredProcedure
  8. cmd.Parameters.Add("@MyParameter", SqlDbType.Int).Value = 999
  9.  
  10. ' Execute the query and close the connection
  11. cmd.ExecuteNonQuery()
  12. cn.Close()

This Hack is part of the ASP.NET Hacks collection

474 Rating: 3.0/5 (16 votes cast)