从函数传递多个Value到类

时间:2018-06-09 06:42:35

标签: sql vb.net

我有2个类1是获取ms访问数据而另一个类是sql acsess。 我想将acsess类函数值传递给sql类并将其分配给sql类varible.curruntly我只能指定1个变量。

Public Class connectionclass
Dim provider As String = "provider=Microsoft.ACE.OLEDB.12.0;data source=|DataDirectory|"
Dim database As String = "serverdata.accdb"
Public connstring As String = provider & database
Public myconnection As New OleDbConnection(connstring)
Public Function data1()
    Dim x As String
    Dim y As String
    Dim u As String
    Dim p As String
    Try
        myconnection.Open()
        Dim getdata As New OleDbCommand("select * from server", myconnection)
        Dim reader As OleDbDataReader = getdata.ExecuteReader
        While reader.Read
            x = reader.Item("sname")
            y = reader.Item("dbase")
            u = reader.Item("username")
            p = reader.Item("password")
            Return (x)
        End While
        myconnection.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Function

结束班

第二课

ublic Class datbaseconnect
Public obj1 As New connectionclass
Dim returnvalue As String
Dim X = obj1.data1
Dim y = ""
Dim u = ""
Dim p = ""
Public SQLCon As New SqlConnection With
  {.ConnectionString =
  "Server=" & x & ";
    DataBase=" & y & ";
    user ID=" & u & ";
    password=" & p & ";
    Trusted_Connection=false;
"}

End Class

1 个答案:

答案 0 :(得分:0)

Tuples是一种快速简便的方法,可以在单个变量中返回多个数据值,而无需创建一个全新的类来保存单个值。

Public Function data1() As Tuple(Of String, String, String, String)
    Dim x As String
    Dim y As String
    Dim u As String
    Dim p As String
    Try
        myconnection.Open()
        Dim getdata As New OleDbCommand("select * from server", myconnection)
        Dim reader As OleDbDataReader = getdata.ExecuteReader
        While reader.Read
            x = reader.Item("sname")
            y = reader.Item("dbase")
            u = reader.Item("username")
            p = reader.Item("password")
            Return New Tuple(Of String, String, String, String)(x, y, u, p)
        End While
        myconnection.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Function    

调用此函数时,它将返回如下数据:

Dim returnedData = data1()

最后,您只需调用以下函数:

WorkWithData(returnedData)

我无法包含你自己的第二堂课,因为我没有在那里看到一个功能。

Public Sub WorkWithData(incomingData As Tuple(Of String, String, String, String))
    Debug.Print(incomingData.Item1)
    Debug.Print(incomingData.Item2)
    Debug.Print(incomingData.Item3)
    Debug.Print(incomingData.Item4)
End Sub