Login or Sign Up to become a member!

EXPERTS, INFORMATION, IDEAS & KNOWLEDGE

Social bookmarker Add this

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: 2.9/5 (13 votes cast)