更好的方法来填充从SQL表单字段? [英] Better way to populate form fields from SQL?

查看:167
本文介绍了更好的方法来填充从SQL表单字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个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

等等....
 

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

感谢您!

解决方案
  1. <一个href="http://stackoverflow.com/questions/240836/dynamically-invoke-properties-by-string-name-using-vb-net">Reflect属性名称

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

  3. 使用的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!

解决方案

  1. Reflect the property names

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

  3. 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.

这篇关于更好的方法来填充从SQL表单字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆