ASP.NET: Insert data into SQL Server
From Wiki
Summary: Insert data into SQL Server using a stored procedure and parameters
The SQL Server table:
- CREATE TABLE table1 (id int identity(1,1), myvalue int)
The SQL Server Stored procedure:
- CREATE procedure MyStoredProcedure
- (
- @MyParameter int
- )
- AS
- INSERT table1 (myvalue) VALUES (@MyParameter)
The ASP.NET commands to execute the strored procedure:
- ' Declarations
- Dim cn As New SqlConnection("Your connection string")
- Dim cmd As New SqlCommand("MyStoredProcedure", cn)
- ' Open the connection and add a parameter
- cn.Open()
- cmd.CommandType = CommandType.StoredProcedure
- cmd.Parameters.Add("@MyParameter", SqlDbType.Int).Value = 999
- ' Execute the query and close the connection
- cmd.ExecuteNonQuery()
- cn.Close()
This Hack is part of the ASP.NET Hacks collection


