0

我正在使用 vb.net

我的类(class1)文件中有一个函数,即保存。这个 Save 函数有 3 个参数。现在我使用以下方法来发送参数。

Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        dim p as new class1
        p.save(txtempid.text,txtempname.text,txtage.text)
        messagebox.show("Successfully Saved")
End sub

第一类:

imports system.data.sqlclient
imports system.configuration
public class class1
    Dim constr As String
    Dim con As SqlConnection
    Dim cmd As SqlCommand

public function save(byval s as string,byval s1 as string,byval s2 as string)
    constr = ConfigurationManager.AppSettings("con")
    con = New SqlConnection(constr)
    con.Open()
    cmd=new sqlcommand("Emp_insert",con)
    cmd.commandtype=commandtype.storedprocedure
    cmd.Parameters.Add("@EmployeeID", SqlDbType.BigInt).Value = s
    cmd.Parameters.Add("@EmployeeName", SqlDbType.VarChar).Value = s1
    cmd.Parameters.Add("@Age", SqlDbType.BigInt).Value = s2
    cmd.executenonquery()
    cmd.dispose()
    con.close()
   return 0
End function
End Class

没关系,但是当我有更多的论点时,这很难。有人可以建议我更好的方法吗?

4

1 回答 1

1

修改

public function save(parameters as Dictionary(Of string, object)) as int
for each item as KeyvaluePair(Of string, object) in parameters
cmd.Parameters.AddWithValue("@" & item.Key, item.Value)
next
return cmd.ExecuteNonQuery()
end function

您可以使用元组

public sub DoSomething(Tuple (Of string, string, integer) myparameter)
dim firstItem as string = myparameter.Item1 
dim secondItem as string = myparameter.Item2
dim thirdItem as integer = myparameter.Item3
end sub

sub main()
Dim tuple as New Tuple(Of string, string, integer)("First","Second", 100)
DoSomething(tuple)
end sub
于 2012-07-24T04:47:23.563 回答