One of the Purpose of database is to stored the data which is a collection of information. To stored the data, programmer's need to create a command to insert into database.
Sample Code to Insert Data into SQL Server 2003 Database Using the Stored Procedure.
First Step
Create a Database on Sql Server 2003 where the data stored .
Second Step
after Creating Database, Create new table into your Database.
Third Stes
Create an Insert SqlCommand in Stored Procedure for inserting or saving the data into your created table.
Here is the Sample Code of Insert Command
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[InsertCommandSample] //Name of Store Procedure
@Parameter1 VARCHAR(50),
@Paramerte2 VARCHAR(50),
@Parameter3 VARCHAR(50)
AS
BEGIN
Insert INTO [TableName]
(
FieldName1,
FieldName2,
FieldName3
)
values
(
@Parameter1,
@Parameter2,
@Parameter3
)
END
set QUOTED_IDENTIFIER ON
go
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[InsertCommandSample] //Name of Store Procedure
@Parameter1 VARCHAR(50),
@Paramerte2 VARCHAR(50),
@Parameter3 VARCHAR(50)
AS
BEGIN
Insert INTO [TableName]
(
FieldName1,
FieldName2,
FieldName3
)
values
(
@Parameter1,
@Parameter2,
@Parameter3
)
END
Here is the Code of Connecting the Insert Command of stored Procedure into Visual Basic.net
Imports System.Data.SqlClient
Public Class Sample_InsertCommand
Try
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "InsertCommandSample" '--------Name of Insert Command of StoredProcedure
With cmd.Parameters
.Clear()
.Add("@Parameter1", SqlDbType.NVarChar).Value = TextBox1.text
.Add("@Parameter2", SqlDbType.NVarChar).Value= TextBox2.text
.Add("@Parameter3", SqlDbType.NVarChar).Value = TextBox3.text
End With
Cmd.ExecuteNonQuery()
msgBox("Save SuccessFully.")
Catch ex As Exception
msgBox("Ex.message")
End Try
End Using
Public Class Sample_InsertCommand
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Using con=OpenConn 'Function of Database Connection
Try
cmd.CommandText = "InsertCommandSample" '--------Name of Insert Command of StoredProcedure
With cmd.Parameters
.Clear()
.Add("@Parameter1", SqlDbType.NVarChar).Value = TextBox1.text
.Add("@Parameter2", SqlDbType.NVarChar).Value= TextBox2.text
.Add("@Parameter3", SqlDbType.NVarChar).Value = TextBox3.text
End With
Cmd.ExecuteNonQuery()
msgBox("Save SuccessFully.")
Catch ex As Exception
msgBox("Ex.message")
End Try
End Using
End Sub
End Class
No comments:
Post a Comment