Reputation: 298
I just wonder if I can create an standard class in VB 2008
I am trying to create a function GetRow(txtSQL) which accepts sql as parameter. I wanted this function to return an object which contains columns of database tables as properties so i could use as
myObj=GetRow("select name, address from tbl_contacts where id=1")
txtname.text=myObj.name
Upvotes: 0
Views: 213
Reputation: 5393
As far as I know you cannot have properties that are generated at run time available at design time. Since the SQL is simply a string, the compiler doesn't know that .Name needs to be a property.
The best I can offer you is as follows:
Function GetRow(cnn As SqlConnection, sql As String) As DataRow
Using dtb As New DataTable
Using dad As New SqlDataAdapter(sql, cnn)
dad.Fill(dtb)
End Using
If dtb.Rows.Count >= 1 Then
Return dtb.Rows(0)
Else
Return Nothing
End If
End Using
End Function
Sub test()
Using cnn As New SqlConnection("Data Source=mycomputername;Timeout=10;User ID=myusername;Password=mypassword;")
cnn.Open()
Dim strSql As String = "select name, address from tbl_contacts where id=1"
Dim drw As DataRow = GetRow(cnn, strSql)
txtName.text = CStr(drw("name"))
cnn.Close
End Using
End Sub
Upvotes: 1