简体   繁体   English

如何将多个VB函数/ SQL查找合并为一个vb.net函数

[英]How to Combine Multiple VB Functions / SQL Lookups into one vb.net function

I have two functions that check the same table, one right after the other. 我有两个检查同一张表的函数,一个紧接另一个。 This setup seems inefficient. 此设置似乎效率很低。 Is there a way to combine these? 有没有办法将这些结合起来?

getCustomerName(customerID)
getCustomerEmail(customerID)

'GET CUSTOMER NAME 
Public Shared function getCustomerName(myArg) as String
Dim objConnection As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("connectionString"))
Dim finalCustomerName as string
objConnection.Open()
    Dim objCommand As New SqlCommand("SELECT customerName FROM customers WHERE customerID = '" + MyArg + "'", objConnection)
    Dim objDataReader as SqlDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
    While objDataReader.Read()
        finalCustomerName = objDataReader("customerName")
    End While
objConnection.Close()
Return finalCustomerName
End function

'GET CUSTOMER EMAIL
Public Shared function getCustomerEmail(myArg) as String
Dim objConnection As New SqlConnection(System.Configuration.ConfigurationSettings.AppSettings("connectionString"))
Dim finalCustomerEmail as string
objConnection.Open()
    Dim objCommand As New SqlCommand("SELECT customerEmail FROM customers WHERE customerID = '" + MyArg + "'", objConnection)
    Dim objDataReader as SqlDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
    While objDataReader.Read()
        finalCustomerEmail = objDataReader("customerEmail")
    End While
objConnection.Close()
Return finalCustomerEmail
End function

Try this 尝试这个

New Customer class (you can add more properties that are related to customer and return them from function below): 新客户类(您可以添加更多与客户相关的属性,并从下面的函数中返回它们):

Public Class Customer

    Public Property CustomerName() As String
        Get
            Return m_CustomerName
        End Get
        Set
            m_CustomerName = Value
        End Set
    End Property
    Private m_CustomerName As String
    Public Property CustomerEmail() As String
        Get
            Return m_CustomerEmail
        End Get
        Set
            m_CustomerEmail = Value
        End Set
    End Property
    Private m_CustomerEmail As String
End Class

And your function should be 你的功能应该是

// your function to get customer details
Public function getCustomer(myArg) as Customer
Dim custobj as New Customer()

Dim objCommand As New SqlCommand("SELECT customerEmail,CustomerName FROM customers WHERE customerID = @custid", objConnection)

objCommand.Parameters.AddWithValue("@custid",myArg) //use parameters to avoid sql injections

Dim objDataReader as SqlDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)
While objDataReader.Read()
    custobj.CustomerName = objDataReader("customerName")
    custobj.CustomerEmail = objDataReader("customerEmail")
End While
objDataReader.Close()
objConnection.Close()

Return custObj
End function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM