我只使用存储过程对数据库执行任何操作。我不希望使用ORB,你说在这之前:)ado.NET通过字段和表名从数据读取器获取字段
对于每个表我有一个相应的DAO类(VB或C#),例如:
Namespace Dao
Public Class Client
Public Sub New(ByVal id As Integer, ByVal description As String)
Me.id = id
Me.description = description
End Sub
Property id As Integer
Property description As String
End Class
End Namespace
的构造函数建立的类字段/属性。 在另一类我通常建立我的DAO类的列表(容器),调用SELECT存储过程,并获得领域和建立单一的DAO:
Public Shared Function GetList() As List(Of Dao.Client)
Dim model As New List(Of Dao.Client)
Using dr As MySqlDataReader = DBUtils.CallReadingStoredProcedure("sp_get_clients")
While dr.Read
Dim client As New Dao.Client(dr.GetInt32(0), dr.GetString(1))
model.Add(client)
End While
Return model
End Using
End Function
有时候,我需要创建相同Dao.class从另一种方法。如果构建它的字段很多,它将会很有用,而不是直接将值传递给DAO.class构造函数 - 这很容易出错 - 只是将数据读取器传递给不同的构造函数,从而提取字段并构建自身。
这就像通过建设责任到Dao.class本身:
Namespace Dao
Public Class Client
Public Sub New(ByVal dr As DataReader)
Me.id = dr.GetInt32("id")
Me.marca_id = dr.GetInt32("marca_id")
Me.categoria_id = dr.GetInt32("categoria_id")
Me.codice = dr.GetString("codice")
Me.descrizione = dr.GetString("descrizione")
... many other
End Sub
...
End Class
End Namespace
即使我使用不同的存储过程来获得客户通过这种方式,我使用相同的代码来构建它们。
只要datareader字段,即SELECT字段,它就会工作,总是命名为始终命名为。这是可能的,但是当我有一个JOIN,指定的域不包含表名,即该查询在SP:
SELECT
OA.id, -- 0
OA.articolo_id, -- 1
OA.quantita, -- 2
OA.quantita_evasa, -- 3
OA.prezzo, -- 4
A.id, -- 5
A.marca_id, -- 6
A.categoria_id, -- 7
A.codice, -- 8
A.descrizione, -- 9
A.prezzo_listino, -- 10
A.sconto, -- 11
A.prezzo_speciale, -- 12
A.ha_matricola, -- 13
A.unita_misura, -- 14
A.peso, -- 15
A.codice_barre, -- 16
other fields ...
FROM nm_ordini_articoli OA
JOIN articoli A ON (OA.articolo_id = A.id)
other JOINs...
我不能这样做,因为dr.getInt32("OA.id")
字段名称是“ID “和表名是”OA“。我可以使用索引,但这是纯粹的疯狂,因为我应该尝试在不同的存储过程中使用相同的索引来存储相同的数据!
问题是:我想要一个Dao构造函数来构建一个给予datareader的类;如何从数据读取器获取命名字段,包括表别名或名称?我想要做类似dr.getInt32("real table name", "field name")
或dr.getInt32("table.field")
其他建议? 谢谢。