Recover milliseconds from a date written to a DB MSSQL Server 2019 with VB.Net + ADO.Net
I would like to know how to retrieve a date as data from the milliseconds written to an MS SQL Server 2019 database using the VB.Net + ADO.Net.
Whenever I try to redeem the date, it comes in the fomrmat "dd / MM / yyyy HH:mm: ss "I would like to also have the".fff", but I can not retrieve this information in the database.
* * * Model Code:
Private _DataAlteracao As DateTime
Public Property DataAlteracao() As DateTime
Get
Return _DataAlteracao
End Get
Set(ByVal value As DateTime)
_DataAlteracao = value
End Set
End Property
* * * Insert Code:
Public Overloads Function InserirHistorico(entidade As ClsHistoricoDomain) As Object
Dim objConfirma As Object
MyBase.LimparStrComando()
MyBase.StrComando.AppendLine(" INSERT INTO tblHistorico (")
MyBase.StrComando.AppendLine(" CNPJ, ")
MyBase.StrComando.AppendLine(" RazaoSocial, ")
MyBase.StrComando.AppendLine(" NomeFantasia, ")
MyBase.StrComando.AppendLine(" CodigoSistema, ")
MyBase.StrComando.AppendLine(" DataAlteracao) ")
MyBase.StrComando.AppendLine(" VALUES (")
MyBase.StrComando.Append(IIf(Not String.IsNullOrEmpty(entidade.CNPJ), String.Format("'{0}',{1}", entidade.CNPJ, Environment.NewLine), "NULL,"))
MyBase.StrComando.Append(IIf(Not String.IsNullOrEmpty(entidade.RazaoSocial), String.Format("'{0}',{1}", entidade.RazaoSocial, Environment.NewLine), "NULL,"))
MyBase.StrComando.Append(IIf(Not String.IsNullOrEmpty(entidade.NomeFantasia), String.Format("'{0}',{1}", entidade.NomeFantasia, Environment.NewLine), "NULL,"))
MyBase.StrComando.Append(IIf(Not String.IsNullOrEmpty(entidade.CodigoSistema), String.Format("'{0}',{1}", entidade.CodigoSistema, Environment.NewLine), "NULL,"))
MyBase.StrComando.Append(IIf(Not String.IsNullOrEmpty(entidade.DataAlteracao), String.Format("'{0}',{1}", entidade.DataAlteracao, Environment.NewLine), "NULL,"))
MyBase.StrComando.AppendLine(")")
objConfirma = SQLHelper.ExecuteScalar(StrComando.Remove(StrComando.ToString.LastIndexOf(","), 1).ToString(), True)
Return objConfirma
End Function
* * * Select All Code:
Public Overrides Function SelecionarTodos() As List(Of ClsHistoricoDomain)
MyBase.LimparStrComando()
MyBase.StrComando.AppendLine(" SELECT CNPJ, ")
MyBase.StrComando.AppendLine(" RazaoSocial, ")
MyBase.StrComando.AppendLine(" NomeFantasia, ")
MyBase.StrComando.AppendLine(" CodigoSistema, ")
MyBase.StrComando.AppendLine(" DataAlteracao ")
MyBase.StrComando.AppendLine(" FROM tblHistorico ")
Return MyBase.RetornaLista(StrComando.ToString())
End Function
1 answers
The problem is that I mounted in 'tblHistorico' a primary key composed of two fields: "CodigoSistema" + "DataAlteracao".
(maybe help someone) The best I could do was do the following:
* * * function call to test
Dim strTeste As String
strTeste = GerenciadorBll.Instance.HistoricoBLL.RetornaCodigoHistorico("1", "18-01-2021 15:14:41.897")
mskDataAlteracao.Text = strTeste
* * * BLL
Public Function RetornaCodigoHistorico(Codigo As String, DataHistorico As String) As String
Return GerenciadorDal.Instance.HistoricoDal.RetornaCodigoHistorico(Codigo, DataHistorico)
End Function
* * * from
Public Function RetornaCodigoHistorico(ByVal codigoHistorico As String, ByVal dataHistorico As String) As String
MyBase.LimparStrComando()
MyBase.StrComando.AppendLine(" SELECT ")
MyBase.StrComando.AppendLine(" DataAlteracao ")
MyBase.StrComando.AppendLine(" FROM tblHistorico ")
MyBase.StrComando.AppendLine(String.Format(" WHERE codigoSistema = '{0}' ", codigoHistorico))
MyBase.StrComando.AppendFormat(" AND DataAlteracao = '{0}' ", dataHistorico)
Return MyBase.RetornaDataHistorico(StrComando.ToString())
End Function
* * * BASE FROM
Protected Function RetornaDataHistorico(ByVal pCommantText As String) As String
Dim milliSeconds As Int32
Dim dt As DateTime
Dim strTempo As String
Using dataReader As System.Data.SqlClient.SqlDataReader = SQLHelper.ExecuteReader(pCommantText)
If dataReader.HasRows Then
dataReader.Read()
dt = dataReader.GetValue(dataReader.GetOrdinal("DataAlteracao"))
milliSeconds = dt.Millisecond
strTempo = dt.ToString() + "." + milliSeconds.ToString()
Else
Return Now()
End If
Return strTempo
End Using
End Function
Now got an error when I want a SELECT ALL to fill a grid...
* * * UI-User Interface
Private Sub PreencheGrid(Optional ByVal cod1 As String = Nothing, Optional ByVal cod2 As String = Nothing)
DgvPadrao.DataSource = Nothing
DgvPadrao.DataSource = GerenciadorBll.Instance.HistoricoBLL.SelecionarTodos
If DgvPadrao.Rows.Count > 0 Then
...
* * * BLL
Public Function SelecionarTodos() As List(Of ClsHistoricoDomain) Implements ICrud(Of ClsHistoricoDomain, String).SelecionarTodos
Return GerenciadorDal.Instance.HistoricoDal.SelecionarTodos()
End Function
* * * from
Public Overrides Function SelecionarTodos() As List(Of ClsHistoricoDomain)
MyBase.LimparStrComando()
MyBase.StrComando.AppendLine(" SELECT CNPJ, ")
MyBase.StrComando.AppendLine(" RazaoSocial, ")
MyBase.StrComando.AppendLine(" NomeFantasia, ")
MyBase.StrComando.AppendLine(" CodigoSistema, ")
MyBase.StrComando.AppendLine(" DataAlteracao ")
MyBase.StrComando.AppendLine(" FROM tblHistorico ")
Return MyBase.RetornaLista2(StrComando.ToString())
End Function
* * BASE FROM
Protected Function RetornaLista2(ByVal commandText As String, Optional ByVal isSql As Boolean = True) As List(Of ClsHistoricoDomain)
Dim listaRetorno As List(Of ClsHistoricoDomain) = Nothing
Try
If isSql Then
Using dataReader As System.Data.SqlClient.SqlDataReader = SQLHelper.ExecuteReader(commandText)
If dataReader.HasRows Then
listaRetorno = New List(Of ClsHistoricoDomain)()
While dataReader.Read()
Dim objeto As ClsHistoricoDomain
objeto = Activator.CreateInstance(GetType(ClsHistoricoDomain), dataReader)
listaRetorno.Add(objeto)
End While
End If
End Using
End If
Return listaRetorno
Catch ex As Exception
Return Nothing
End Try
End Function
SELECT ALL ALREADY RETURNS ALL DATES WITHOUT MILLISECONDS!!!
As in the table 'tblHistorico' my primary key is composed of 'CodigoSistema' + 'DataAlteracao' so I Iron myself, as it returns several records with incomplete composite primary key information...
... I think I'll have to change the whole logic of doing System history.... NO it can be a compound key with a date that depends on milliseconds... it'll have to be an autonumber or something... ... that sucks, because of an error in the code of VB.Net that does not return the milliseconds in a search I will have to totally change my idea.