你好,這是我的第一個項目在vb.net與ms visual studio 2010工作,我想創建一個類,可以發送參數存儲過程在transact-sql數據庫,我知道如何在vb 6中做到這一點,但我不確定這是否正確。發送參數到存儲過程vb.net
Imports System.Data.SqlClient
Public Class ClsLineas
Public Sub Inserta(ByVal GridLineas As DataGrid, _
ByVal numero As String, _
ByVal tipo As String, _
ByVal estado As String, _
ByVal anexo As Integer, _
ByVal fechaInicio As String, _
ByVal fechaFin As String, _
ByVal pcReg As String, _
ByVal observaciones As String, _
ByVal usuReg As String)
Dim cnx As SqlConnection = New SqlConnection(ClsCon.connectionString)
'ClsCon.connectionString is a class that contains the connection string
Dim cmd As SqlCommand = New SqlCommand()
If cnx.State = ConnectionState.Closed Then cnx.Open()
cmd.Connection = cnx
cmd.CommandText = "SP_INSERTA_LINEA"
cmd.CommandType = CommandType.StoredProcedure
Dim prm As New SqlParameter
prm.ParameterName = "@TIPO"
prm.SqlDbType = SqlDbType.NVarChar
prm.Size = 30
prm.Direction = ParameterDirection.Input
prm.Value = tipo
cmd.Parameters.Add(prm)
prm.ParameterName = "@FECHA_INICIO"
prm.SqlDbType = SqlDbType.NVarChar
prm.Size = 30
prm.Direction = ParameterDirection.Input
prm.Value = fechaInicio
cmd.Parameters.Add(prm)
prm.ParameterName = "@FECHA_FIN"
prm.SqlDbType = SqlDbType.NVarChar
prm.Size = 30
prm.Direction = ParameterDirection.Input
prm.Value = fechaFin
cmd.Parameters.Add(prm)
prm.ParameterName = "@ESTADO"
prm.SqlDbType = SqlDbType.NVarChar
prm.Size = 30
prm.Direction = ParameterDirection.Input
prm.Value = estado
cmd.Parameters.Add(prm)
prm.ParameterName = "@NUMERO"
prm.SqlDbType = SqlDbType.NVarChar
prm.Size = 15
prm.Direction = ParameterDirection.Input
prm.Value = numero
cmd.Parameters.Add(prm)
prm.ParameterName = "@ANEXO"
prm.SqlDbType = SqlDbType.Int
prm.Direction = ParameterDirection.Input
prm.Value = anexo
cmd.Parameters.Add(prm)
prm.ParameterName = "@PC_REG"
prm.SqlDbType = SqlDbType.NVarChar
prm.Size = 50
prm.Direction = ParameterDirection.Input
prm.Value = pcReg
cmd.Parameters.Add(prm)
prm.ParameterName = "@USU_REG"
prm.SqlDbType = SqlDbType.NVarChar
prm.Size = 50
prm.Direction = ParameterDirection.Input
prm.Value = usuReg
cmd.Parameters.Add(prm)
prm.ParameterName = "@OBSERVACIONES"
prm.SqlDbType = SqlDbType.NVarChar
prm.Size = 1000
prm.Direction = ParameterDirection.Input
prm.Value = observaciones
cmd.Parameters.Add(prm)
prm.ParameterName = "@ID"
prm.SqlDbType = SqlDbType.Int
prm.Direction = ParameterDirection.Output
cmd.Parameters.Add(prm)
Dim adp As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim DataSet As DataSet = New DataSet("Lineas")
adp.Fill(DataSet)
GridLineas.DataSource = DataSet.Tables(0)
End Sub
End class
一些我的疑惑是:
我真的需要我每次打電話給我類的方法時打開數據庫?
sqlAdapter和Dataset是否真的需要?在vb 6中,你可以在追加參數和完成之後做類似「command execute inserta」的操作。
你正在打開_connection_而不是數據庫,是的,這是必須完成的。至於'DataSet'和'DataAdepter' - 取決於你在做什麼。 – Oded