如何基于SQL存储的函数名在VB.NET中调用对应文件导入函数?
列表控件项关联对应导入函数的实现
我在SQL Server中拥有一张包含FileType与FunctionToCall两个字段的数据表。根据文件用途,需要执行对应的函数将文件内容导入SQL Server:例如文件名前4个字符为Pers时,该文件存储员工个人信息;前4个字符为Exps时,存储员工费用信息,每种文件都需遵循特定模板。
我使用VB.NET填充列表控件(ListView或ListBox),展示可导入的文件类型(FileType),相关代码如下:
Dim Function2bCalled As String = "" lstIdentifiers.Items.Clear() Dim myConn As SqlConnection Dim SQLCmd As SqlCommand myConn = New SqlConnection(SQLConnString) Try myConn.Open() SQLCmd = New SqlCommand() SQLCmd.CommandText = "get_LU_FileUploadList" SQLCmd.CommandType = CommandType.StoredProcedure SQLCmd.Parameters.AddWithValue("@OrderBy", Odbc.OdbcType.NVarChar).Value = "Prefix" SQLCmd.Connection = myConn Using myReader = SQLCmd.ExecuteReader() If myReader.HasRows Then Do While myReader.Read() lstIdentifiers.Items.Add(myReader.Item("PrefixFileDescription").ToString()) Function2bCalled = myReader.Item("PrefixFileDescription").ToString() AddHandler lstIdentifiers.Click, AddressOf Function2bCalled Loop End If End Using Catch SQLEx As SqlClient.SqlException '----- If there is a problem with the connection string syntax (i.e. server name is invalid) you will incur a SqlException LogAction(LoggedInName, AppTitle & " error: " & SQLEx.Message, "Error <SQL> - " & GetCurrentMethod().Name, GetCurrentMethod().DeclaringType().Name, "", DoingWhat) Catch DataEx As DataException '----- Catch-all LogAction(LoggedInName, AppTitle & " error: " & DataEx.Message, "Error <Data> - " & GetCurrentMethod().Name, GetCurrentMethod().DeclaringType().Name, "", DoingWhat) Catch GenEx As Exception LogAction(LoggedInName, AppTitle & " error: " & GenEx.Message, "Error <General> - " & GetCurrentMethod().Name, GetCurrentMethod().DeclaringType().Name, "", DoingWhat) Finally End Try
当用户将文件拖放到窗口时,代码会判断文件用途,检查列表中是否存在对应的文件描述。若存在,则需软件执行关联的函数。
我已了解AddressOf关键字的作用,但未找到适用于列表控件的实现示例。现有示例均针对单个按钮点击事件,而我需要给ListView或ListBox的多个列表项各自关联对应的调用函数(如列表项1调用ImportPersonalDetails函数,列表项2调用ImportExpenses函数等)。
内容的提问来源于stack exchange,提问作者Dieter Stalmann
相关产品推荐
相关产品推荐

