更好的方法来填充从SQL表单字段?字段、表单、方法来、SQL

由网友(何以安尘)分享简介:我想从一个SQL表格填充网页。这就是我现在所拥有的,虽然我不知道这是否是做的事情最好的办法,请给我点建议:公共类为userDetails公共地址1作为字符串公共地址2作为字符串众城作为字符串.........末级公共类clsPerson设置SQL连接昏暗删去了objformat作为新clsFormat昏暗objC...

我想从一个SQL表格填充网页。

这就是我现在所拥有的,虽然我不知道这是否是做的事情最好的办法,请给我点建议:

 公共类为userDetails
    公共地址1作为字符串
    公共地址2作为字符串
    众城作为字符串
    ...
...
...
末级

公共类clsPerson
    设置SQL连接
    昏暗删去了objformat作为新clsFormat
    昏暗objConn作为新clsConn()
    昏暗connStr作为字符串= objConn.getConn()
    昏暗MyConnection的作为新Data.SqlClient.SqlConnection(connStr)

    公共功能GetPersonDetails()作为为userDetails
        连接和所有其他的好东西在这里

        尝试
            执行命令
            myConnection.Open()
            博士= myCommand.ExecuteReader()

            确保返回的记录
            如果dr.Read(),然后
                创建和填充ApplicantDetails
                userDetails.address1 =博士(地址1)
                userDetails.address2 = objFormat.CheckNull(DR(地址2))
                userDetails.city = objFormat.CheckNull(DR(城市))
                ....

            其他
                Err.Raise(4938,clsUser,在GetUserDetails错误 - 用户未找到)
            结束如果

            dr.Close()
        最后
            myConnection.Close()
        结束尝试

        返回为userDetails
    端功能
 

然后我用GetPersonDetails()函数在我的后端填充表单。

像这样:

 昏暗为userDetails如新为userDetails
的UserDetails = getPersonDetails()

txtAddress.text = userdetails.address1

等等....
 
sql查询时,字段值有办法写在sql语句中修改吗

不过,有喜欢的用户数据库50场,而且好像很多重新键入的......请大家帮我找一个更好的方式来做到这一点。

感谢您!

解决方案

Reflect属性名称

推断属性名称的控件名称,例如,TXT&放大器;字段名

使用的FindControl 以寻找控制,填充它的Text属性。

您还可以动态地生成的领域,虽然你失去了在presentation一些控制。这是一个解决方案,我不会用,但是 - 它往往使code脆,难以调试

I am trying to populate a web from from a SQL table.

This is what I have right now, though I am not sure if it's the best way to do things, please give me suggestions:

Public Class userDetails
    Public address1 As String
    Public address2 As String
    Public city As String
    ...
...
...
End Class

Public Class clsPerson
    'set SQL connection
    Dim objFormat As New clsFormat
    Dim objConn As New clsConn()
    Dim connStr As String = objConn.getConn()
    Dim myConnection As New Data.SqlClient.SqlConnection(connStr)

    Public Function GetPersonDetails() As userDetails
        'connection and all other good stuff here

        Try
            ' Execute the command
            myConnection.Open()
            dr = myCommand.ExecuteReader()

            ' Make sure a record was returned
            If dr.Read() Then
                ' Create and Populate ApplicantDetails
                userDetails.address1 = dr("address1")
                userDetails.address2 = objFormat.CheckNull(dr("address2"))
                userDetails.city = objFormat.CheckNull(dr("city"))
                ....

            Else
                Err.Raise(4938, "clsUser", "Error in GetUserDetails - User Not Found")
            End If

            dr.Close()
        Finally
            myConnection.Close()
        End Try

        Return userDetails
    End Function

i then use GetPersonDetails() function in my backend to populate the form.

like so:

Dim userDetails as new userDetails
userdetails = getPersonDetails()

txtAddress.text = userdetails.address1

etc....

however, there are like 50 fields in the User db, and it seems like a lot of retyping... please help me find a better way to do this.

Thank you!

解决方案

Reflect the property names

Infer the controlname from the property name, e.g., "txt" & fieldName

Use FindControl to find the control and populate its Text property.

You could also generate the fields dynamically, though you lose some control over the presentation. This is a solution I wouldn't use, however - it will tend to make the code brittle and difficult to debug.

阅读全文

相关推荐

最新文章