martes, mayo 03, 2005

Codigo ASP.Net - Leer un registro de una tabla.

A continuación un snippet, que siempre ando olvidando a la hora de leer un registro de una table:

intIDLote = Request.QueryString("lote")
Dim drLote As System.Data.DataRow
drLote = obtenLotexID(intIDLote).Tables(0).Rows(0)
Titulo.Text = drLote.Item("Titulo")

La siguiente funcion se genera en Web Matrix. Recuerda que tiene que ser un DATASET, y no un DataReader.

Function obtenLotexID(ByVal iDLote As Integer) As System.Data.DataSet
Dim connectionString As String = System.Configuration.ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_connGabasys")
Dim dbConnection As System.Data.IDbConnection = New System.Data.OleDb.OleDbConnection(connectionString)

Dim queryString As String = "SELECT ...... ([Lotes].[IDLote] = @IDLote))"
Dim dbCommand As System.Data.IDbCommand = New System.Data.OleDb.OleDbCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim dbParam_iDLote As System.Data.IDataParameter = New System.Data.OleDb.OleDbParameter
dbParam_iDLote.ParameterName = "@lote"
dbParam_iDLote.Value = iDLote
dbParam_iDLote.DbType = System.Data.DbType.Int32
dbCommand.Parameters.Add(dbParam_iDLote)

Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.OleDb.OleDbDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)

Return dataSet
End Function

No hay comentarios.: