如何最小化vb.net代码 [英] how to minimize vb.net code
本文介绍了如何最小化vb.net代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何最小化此代码:
how can i minimized this code :
cn.open()
Dim temp As String
temp = "pending for client approval"
str = "select Count(*) from Asset_Service_Master where CaseStatus= @temp "
cmd = New SqlCommand(str, cn)
cmd.Parameters.Add(New SqlParameter("@temp", temp))
tb_PendingForClientApproval.Text = cmd.ExecuteScalar().ToString
Dim tp As String
tp = "yes"
Dim tmp As String = ""
str = "select count(*) from Asset_Service_Master where CallClosed= @tp & AND BillingComplete=@tmp "
cmd = New SqlCommand(str, cn)
cmd.Parameters.Add(New SqlParameter("@tp", tp))
cmd.Parameters.Add(New SqlParameter("@tmp", tmp))
tb_CalledClosed.Text = cmd.ExecuteScalar().ToString
cn.Close()
推荐答案
我所看到的不只是以下内容:
I don''t see much more than:
Dim tp As String = "yes"
您还可以删除几个多余的换行符.
问候,
You can also remove a couple of superflous linebreaks.
Regards,
您有两个非常相似的代码块,创建一个方法(带有适当的参数)并调用两次.
顺便说一句,由于您使用的是常量(您可以在查询内部直接编写常量),因此您也可以丢弃 SqlParameters .
You have two blocks of code very similar, create a method (with proper arguments) and call it twice.
BTW you may also discard the SqlParameters, since you are using constants (write the constants directly inside the queries).
首先,您的内容没有错做到了.
但是既然你问了:)
首先,我将创建一个存储过程
First thing there is nothing wrong with what you did.
But since you asked :)
First I would create a stored procedure
CREATE PROCEDURE [dbo].[Asset_Service_Master_Get_Counts]
@CaseStatus VARCHAR(50) = NULL, --You'll need to add the correct types.
@CallClosed VARCHAR(50) = NULL,
@BillingComplete VARCHAR(50) = NULL
AS
BEGIN
SELECT COUNT(*)
FROM Asset_Service_Master
WHERE CaseStatus = COALESCE(@CaseStatus, Asset_Service_Master.CaseStatus)
AND CallClosed = COALESCE(@CallClosed,Asset_Service_Master.CallClosed)
AND BillingComplete = COALESCE(@BillingComplete,
Asset_Service_Master.BillingComplete)
END
GO
这是我会使用的方法.
Here is the method I would use.
Public Function GetCount(Optional ByVal caseStatus As String = "", Optional ByVal callClosed As String = "", Optional ByVal billingComplete As String = "") As String
Using cn As New SqlConnection(connectionString)
cn.Open()
If cn.State = ConnectionState.Open Then
Using cmd As New SqlCommand("Asset_Service_Master_Get_Counts", cn)
cmd.Parameters.Add(New SqlParameter("@CaseStatus", IIf(caseStatus = "", DBNull.Value, caseStatus)))
cmd.Parameters.Add(New SqlParameter("@CallClosed", IIf(callClosed = "", DBNull.Value, callClosed)))
cmd.Parameters.Add(New SqlParameter("@BillingComplete", IIf(billingComplete = "", DBNull.Value, billingComplete)))
cmd.CommandType = CommandType.StoredProcedure
Return cmd.ExecuteScalar().ToString
End Using
Else
Return String.Empty
End If
End Using
End Function
这篇关于如何最小化vb.net代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文