Results 1 to 8 of 8

Thread: My First Class

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2003
    Posts
    44

    My First Class

    I have created a class which accepts an sqlstring and an open connection and executes the sql returing an error message if one was thrown. I don't know if I should add a dispose to the class or what else I should add. This is my first class I created, so any suggestions of how I can enhance the code to make it better please let me know. I tested the class and it does what it is suppose too.

    Public Class RunSql

    Dim Err As SqlException

    Sub Execute(ByVal SqlString As String, ByVal MyCnn As SqlConnection)

    Dim e As SqlException

    Dim myCMD As New SqlCommand(SqlString, MyCnn)

    Try
    myCMD.ExecuteNonQuery()
    Catch e
    Err = e
    Finally

    End Try

    End Sub

    ReadOnly Property SqlErr() As SqlException
    Get
    Return Err
    End Get

    End Property

    End Class

    Frank

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    should .Dispose() you SQLCommand object when done with it.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2003
    Posts
    44
    Thanks.

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You could make your class generic by using interfaces. This way, you can check the type that is passed in and use the correct objects. Just a thought..

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2003
    Posts
    44
    I have also created a class to return a field value .. .executescalar. And I was stuck on how to distinguise between datetime, string, integer, real types being passed in. I will have to investigate how to check the type being passed in.

    Thanks.

    Frank

  6. #6
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    my function will do the job easily . just pass the object you to know its type....
    VB Code:
    1. Public Function RetObj(ByVal MyObj As Object)
    2.         Dim obj As New Object()
    3.         If obj.GetType Is GetType(Object) Then
    4.             MessageBox.Show(MyObj.GetType.Name)
    5.         End If
    6.     End Function
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, _
    9.     ByVal e As System.EventArgs) Handles Button1.Click
    10.  
    11.         Dim str As String = "Pirate"
    12.         Dim datetim As DateTime
    13.         Dim int As Integer
    14.         Dim byt As Byte
    15.         RetObj(str)
    16.         RetObj(datetim)
    17.         RetObj(int)
    18.         RetObj(byt)
    19.     End Sub

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2003
    Posts
    44
    Thanks!
    That will get me on the right track.

    Frank

  8. #8
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    little note , if you passed null string it will throw exception , till now i can't figure how to pass null str . otherwise it's works like charm !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width