.net SQLConnectionString到ORM的方式
嗨,有人可以帮我 如果使用ORM连接,这段代码会是什么样子.net SQLConnectionString到ORM的方式,.net,orm,.net,Orm,嗨,有人可以帮我 如果使用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(ConfigurationMa
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
如果我没弄错的话——如果您使用某种ORM,比如EntityFramework或NHibernate,您会询问代码的更改。 因此,对于C语法,简短的描述很抱歉: 连接将更改为强类型代码,不带SQL字符串
using (var dbContext = new ProductEntites())
{
var dbSelect = dbContext.Products.OrderBy(x=>x.PName);
Do While将在dbSelect中更改为foreachvar产品
所有ProductReaderDescription将更改为product.p说明无字符串
使用ORM
使用由VisualStudio工具生成的强类型类的所有收益要安全得多
SQL注入的参数检查是自动进行的
如果需要创建DB独立应用程序,强烈建议使用
我想他指的是LINQ to SQL DataContext?嗨,thx,但我不是每天都在做这个,所以希望有人能给我一个ec。从我的代码中,我可以看到哪个部分做了什么。