使用vb.net在类文件中调用函数

时间:2013-01-18 09:30:45

标签: vb.net dll .net-4.0

我的数据库访问方法在类文件中。此文件具有单独的项目名称:Usr_mysqladapter。在另一个网站项目中,我将类文件作为dll导入,并通过创建对象来调用类文件中的函数。

类文件包含以下函数:

Public Function readfunctioid(ByVal query As String) As DataTable
    Try
        connecttodb()
        mysqlcmd = New MySqlCommand(query, con)
        da = New MySqlDataAdapter(mysqlcmd)
        dt = New DataTable
        da.Fill(dt)
    Catch ex As Exception
        Throw ex
    Finally
        disconnect()
    End Try
    Return dt
End Function

网站项目按如下方式导入dll:

  Imports Usr_mysqladapter

  Dim obj As Usr_mysqladapter.Class1 = New Usr_mysqladapter.Class1

点击我的按钮调用如下函数:

dt = obj.readfunctioid("select fld_functionid  from tbl_users_function_permission")

不幸的是,我收到了错误。如何从我的网站项目访问类文件中的函数?

2 个答案:

答案 0 :(得分:1)

我是在问题的评论中开始的,但是让我向您展示一个极大改进的数据访问类的开始:

Public Class SqlDataHelper

    Private Property ConnectionString() As String
         Get
            'return the connection string, perhaps read/cached from a config file
            'I'll often require this as an argument to the class constructor as well
         End Get
         'May not need a setter, depending on how you build this
    End Property

    Private Function GetConnection() As SqlConnection
         GetConnection = New SqlConnection(ConnectionString)
         GetConnection.Open()
    End Function

    Private Function GetDataTable(ByVal query As String, ByVal ParamArray args() As SqlParameter) As DataTable
       Dim ds As New DataTable
       Dim cmd As New SqlCommand(query, GetConnection())     
       If args IsNot Nothing Then
           For Each p As SqlParameter In args
               cmd.Parameters.Add(p)
           Next p
       End If

       Dim da As New SqlDataAdapter(cmd)
       da.Fill(dt)
       Return dt

    End Function

    Private Function UpdateData(ByVal command As String, ByVal ParamArray args() As SqlParameter) As Integer
       Using cn As SqlConnection = GetConnection(), _
             cmd As New SqlCommand(command, cn) 

           If args IsNot Nothing Then
               For Each p As SqlParameter In args
                   cmd.Parameters.Add(p)
               Next p
           End If

           Return cmd.ExecuteNonQuery()
    End Function

    Private Function GetSingleValue(ByVal query As String, ByVal ParamArray args() As SqlParameter) As Object
       Using cn As SqlConnection = GetConnection(), _
             cmd As New SqlCommand(query, cn) 

           If args IsNot Nothing Then
               For Each p As SqlParameter In args
                   cmd.Parameters.Add(p)
               Next p
           End If

           Return cmd.ExecuteScalar()
    End Function

    Public Function ReadFunctionID() As DataTable  
        Return GetDataTable("SELECT fld_functionid FROM tbl_users_function_permission", Nothing)
    End Function

    Public Function ReadFunctionIDByUser(ByVal UserID As Integer) As DataTable
         Return GetDataTable("SELECT fld_functionid FROM tbl_users_function_permission WHERE UserID = @UserID", _
                    New SqlParameter("@UserID", UserID))
    End Function
End Class

让我们仔细看看这段代码中的一些改进。首先,注意什么是公共与什么是私人。在任何时候你都不想接受来自表示层的临时sql字符串。这并不意味着您不能使用ad hoc sql,只是想要将数据和表示部分分开。

看一下类中最后两个函数如何处理参数数据。不使用任何参数时需要使用显式Nothing的事实是此代码的功能。这是一个不断提醒你应该使用查询参数。如果您不知道sql注入是什么,以及如何防止它,您不应该编写数据库代码!

希望这对你有所帮助。

答案 1 :(得分:0)

在您的网站项目中,只需按照.....

Dim MyCustomSqlAdapter As = New MyCustomClass()

这是你的课被调用MyCustomClass的时候 INSTANCE称为MyCustomSqlAdapter

然后按钮就像....

dt = MyCustomSqlAdapter.readfunctioid("select fld_functionid  from tbl_users_function_permission")