@manhnguyenv

Welcome to my blog!

VB .NET

June 19, 2017 23:55

Stored Procedure (Northwind database)

alter Procedure [dbo].[InsertCustomer]
@CustomerID nchar(5),
@CompanyName nvarchar(40),
@ContactName nvarchar(30),
@ContactTitle nvarchar(30),
@Address nvarchar(60),
@City nvarchar(15),
@Region nvarchar(15),
@PostalCode nvarchar(10),
@Country nvarchar(15),
@Phone nvarchar(24) ,
@Fax nvarchar(24)=null
AS
INSERT INTO [dbo].[Customers]
([CustomerID]
,[CompanyName]
,[ContactName]
,[ContactTitle]
,[Address]
,[City]
,[Region]
,[PostalCode]
,[Country]
,[Phone]
,[Fax])
VALUES
(@CustomerID
,@CompanyName
,@ContactName
,@ContactTitle
,@Address
,@City
,@Region
,@PostalCode
,@Country
,@Phone
,@Fax)

 

VB.NET

Imports System.Data.SqlClient

Module Module1

Class NewCustomer
Public Property CustomerID As String
Public Property CompanyName As String
Public Property ContactName As String
Public Property ContactTitle As String
Public Property Address As String
Public Property City As String
Public Property Region As String
Public Property PostalCode As String
Public Property Country As String
Public Property Phone As String
Public Property Fax As String
End Class

Sub Main()
Dim cust = New NewCustomer() With {
.CustomerID = 2,
.CompanyName = "A",
.ContactName = "A",
.ContactTitle = "A",
.Address = "A",
.City = "A",
.Region = "A",
.PostalCode = "A",
.Country = "A",
.Phone = "A"
}
InsertCustomer(cust)
End Sub

Private Sub InsertCustomer(entity As NewCustomer)
Using connection = New SqlConnection
connection.ConnectionString = "Data Source=manhnguyenv,1433;Initial Catalog=Northwind;User ID=manhnguyenv;Password=123456;"
Dim procedure = "dbo.InsertCustomer"
Using command = New SqlCommand(procedure, connection)
command.CommandType = CommandType.StoredProcedure

command.Parameters.Add(
New SqlParameter("@CustomerID", entity.CustomerID))
command.Parameters.Add(
New SqlParameter("@CompanyName", entity.CompanyName))
command.Parameters.Add(
New SqlParameter("@ContactName", entity.ContactName))
command.Parameters.Add(
New SqlParameter("@ContactTitle", entity.ContactTitle))
command.Parameters.Add(
New SqlParameter("@Address", entity.Address))
command.Parameters.Add(
New SqlParameter("@City", entity.City))
command.Parameters.Add(
New SqlParameter("@Region", entity.Region))
command.Parameters.Add(
New SqlParameter("@PostalCode", entity.PostalCode))
command.Parameters.Add(
New SqlParameter("@Country", entity.Country))
command.Parameters.Add(
New SqlParameter("@Phone", entity.Phone))

connection.Open()
command.ExecuteNonQuery()
End Using

End Using
End Sub

End Module

Categories

Recent posts