S
Seòras
Guest
Hi,
I'm trying to get the results of a query stored in an Access database. I've tried everything I found in the Internet, but I only get the column names. No rows. However, if I execute the query in Access, I get a lot of rows.
My code (one of the many attempts) is this:
Dim sbConsulta As New StringBuilder()
Dim sCadenaConexion As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Projects\DB_2018.accdb;"
Dim conexion As New Data.OleDb.OleDbConnection(sCadenaConexion)
Dim ds As New DataSet()
Dim da As New OleDb.OleDbDataAdapter("ORIGEN", conexion)
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.Fill(ds, "Tabla")
I've also tried to run the query directly, but it still doesn't give rows, I think because it refers to another stored query.
Try
Using cnn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Projects\DB_2018.accdb;")
' Open connection.
cnn.Open()
Dim dtVistas As DataTable = cnn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Views, New Object() {Nothing, Nothing, Nothing})
Dim sConsulta As String = ""
Dim fila As DataRow = Nothing
sConsulta = dtVistas.Select("TABLE_NAME='ORIGEN'")(0)("VIEW_DEFINITION")
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand()
cmd.Connection = cnn
cmd.CommandText = sConsulta
Dim dtFilas As New DataTable()
dtFilas.Load(cmd.ExecuteReader())
dgvDatos.DataSource = dtFilas
Return True
End Using
Catch ex As Exception
Return False
End Try
I'm starting to despair...
Continue reading...
I'm trying to get the results of a query stored in an Access database. I've tried everything I found in the Internet, but I only get the column names. No rows. However, if I execute the query in Access, I get a lot of rows.
My code (one of the many attempts) is this:
Dim sbConsulta As New StringBuilder()
Dim sCadenaConexion As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Projects\DB_2018.accdb;"
Dim conexion As New Data.OleDb.OleDbConnection(sCadenaConexion)
Dim ds As New DataSet()
Dim da As New OleDb.OleDbDataAdapter("ORIGEN", conexion)
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.Fill(ds, "Tabla")
I've also tried to run the query directly, but it still doesn't give rows, I think because it refers to another stored query.
Try
Using cnn As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Projects\DB_2018.accdb;")
' Open connection.
cnn.Open()
Dim dtVistas As DataTable = cnn.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Views, New Object() {Nothing, Nothing, Nothing})
Dim sConsulta As String = ""
Dim fila As DataRow = Nothing
sConsulta = dtVistas.Select("TABLE_NAME='ORIGEN'")(0)("VIEW_DEFINITION")
Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand()
cmd.Connection = cnn
cmd.CommandText = sConsulta
Dim dtFilas As New DataTable()
dtFilas.Load(cmd.ExecuteReader())
dgvDatos.DataSource = dtFilas
Return True
End Using
Catch ex As Exception
Return False
End Try
I'm starting to despair...
Continue reading...