你好,有人可以帮助我。
如果使用ORM连接,此代码将如何显示
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
答案 0 :(得分:0)
如果我理解正确的话 - 如果您使用某些ORM(例如EntityFramework或NHibernate),则会询问有关更改的代码。
如此简短的描述(抱歉C#语法):
连接将更改为强类型代码,不带SQL字符串。
using (var dbContext = new ProductEntites())
{
var dbSelect = dbContext.Products.OrderBy(x=>x.PName);
Do-While将更改为foreach(var product in dbSelect)
所有ProductReader("PDescription")
都会更改为product.PDescription
(无字符串!)
使用ORM