Hi someone can help me here.
How will this code look like if using ORM connection
Imports System.Data
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Public Sub OpenDBproduct()
Dim ProductConnection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
Dim ProductReader As SqlDataReader
Dim ProductCommand As SqlCommand
Dim ProductSQL As String = ""
ProductSQL = "SELECT * FROM table1 ORDER BY PName DESC;"
ProductCommand = New SqlCommand(ProductSQL, ProductConnection)
'Prøv at åben Connection.
Try
ProductConnection.Open()
ProductReader = ProductCommand.ExecuteReader(CommandBehavior.CloseConnection)
'Hvis der er data, udskriv da dataen.
If ProductReader.HasRows() Then
Response.Write("<h2>Produkter</h2>")
Response.Write("<table style=""width:340px; border:0px;""><tr>")
Do While ProductReader.Read()
Response.Write("<td colspan=""3""><br /></td>")
Response.Write("</tr><tr>")
Response.Write("<td style=""width:10px;""></td>")
Response.Write("<td><div style=""width:52px; text-align:center;""><span class=""""># " & ProductReader("PName") & "</span></div></td>")
Response.Write("<td class=""""><span class="""">" & ProductReader("PName") & "</span><br /><span class="""">" & ProductReader("PPrice") & "</span><br /><span class=""""><b>" & ProductReader("Pean") & "</b></span></td>")
Response.Write("</tr><tr>")
Response.Write("<td colspan=""3""><br />" & ProductReader("PDescription") & "</td>")
Response.Write("</tr><tr>")
Response.Write("<td style=""border-bottom:dashed 2px #FFA902;"" colspan=""3""><br /></td>")
Response.Write("</tr><tr>")
Loop
Response.Write("</tr></table>")
Response.Write("Slutning")
Else
'Hvis der ikke er data, lav en fejl tekst.
Response.Write("<table style=""width:340px; border:0px;""><tr><td>")
Response.Write("Der findes ingen produkter.")
Response.Write("<br /><br />")
Response.Write("Slutning")
Response.Write("</td></tr></table>")
End If
Catch ex As Exception
Console.WriteLine(ex.Message)
Finally
'Lukker Connection, Reader og Sletter Hukommelse.
If Not ProductReader Is Nothing Then
ProductReader.Dispose()
End If
If Not ProductConnection Is Nothing Then
ProductConnection.Dispose()
End If
End Try
End Sub
End Class
If I understood you right – you ask about changes no that code if you use some ORM, like EntityFramework or NHibernate.
So short description is (sorry for C# syntax):
connecting will change to strongly typed code, without SQL string.
Do-While will be changed to
foreach(var product in dbSelect)All
ProductReader("PDescription")would be changed toproduct.PDescription(no strings!)Using ORM