创建一个 sub/function 接受 vb.net 中的各种参数值
creating a sub/function that accepts various parameter values in vb.net
我正在尝试使用 VB.NET 运行 SQL SERVER 2008 存储过程。我可以很好地连接,但我不想为我想调用的每个存储过程重新编写代码。我怎样才能以函数或子的形式拥有该代码,以便我可以通过简单地将存储过程名称传递给代码来重用代码。以下是我正在使用的示例。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.txtProcessDate.Text = "" Then
MsgBox("Please Enter The processing Date Before You Continue ", vbOKOnly, "SMIS")
Me.txtProcessDate.Focus()
Else
Try
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "computationsdelete"
cmd.CommandTimeout = 6000
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "deductionssmoothing"
cmd.CommandTimeout = 6000
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
MsgBox("RECORDS POSTED SUCCESSFULLY. YOU CAN NOW PRINT PAYSLIPS")
conn.Open()
sqlcmd.Parameters.AddWithValue("@d", processdate)
sqlcmd.Connection = conn
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.CommandText = "insertpaymentsanddeductions"
sqlcmd.CommandTimeout = 6000
sqlcmd.ExecuteNonQuery()
sqlcmd.Dispose()
conn.Close()
你最好做一些像@Codexer 在你的问题的评论中建议的事情,但你的确切问题的代码答案如下。
Public Sub CallStoredProcedure(ByVal procedureName as String)
...
cmd.CommandText = procedureName
...
End Sub
这是我在为数据库调用创建单独的 class 时对代码所做的一些改进;
Public Sub searchifexists(ByVal value As String, ByVal table As String, ByVal field As String)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT " & field & " FROM " & table & " where " & field & " = '" & value & "' "
conn.Close()
da.SelectCommand = cmd
da.Fill(dt)
If dt.Rows.Count > 0 Then
Try
MsgBox("Record already exists. Record will not be saved because you can not have duplicate values for Employee Number")
Exit Sub
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
这是我点击按钮时的结果
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
If btnAdd.Text = "&Revert" Then
If EmpnoTextBox.Text = "" Then
MsgBox("You must enter a value for Employee Number")
Me.EmpnoTextBox.Focus()
Exit Sub
ElseIf EmpnameTextBox.Text = "" Then
MsgBox("You must enter a value for Employee Name")
Me.EmpnameTextBox.Focus()
Exit Sub
End If
Dim dbcall As New dbcalls
dbcall.searchifexists(EmpnoTextBox.Text, "employee", "empno")
Save_Record()
Change_Button(True)
btnAdd.Text = "&Add"
End If
DisplayNav(sender, e)
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
MsgBox("Employee data saved")
End Try
End Sub
从中我学会了清理我的代码,使其具有可读性和更好的应用程序。感谢 Whosebug 社区!
我正在尝试使用 VB.NET 运行 SQL SERVER 2008 存储过程。我可以很好地连接,但我不想为我想调用的每个存储过程重新编写代码。我怎样才能以函数或子的形式拥有该代码,以便我可以通过简单地将存储过程名称传递给代码来重用代码。以下是我正在使用的示例。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.txtProcessDate.Text = "" Then
MsgBox("Please Enter The processing Date Before You Continue ", vbOKOnly, "SMIS")
Me.txtProcessDate.Focus()
Else
Try
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "computationsdelete"
cmd.CommandTimeout = 6000
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "deductionssmoothing"
cmd.CommandTimeout = 6000
cmd.ExecuteNonQuery()
cmd.Dispose()
conn.Close()
MsgBox("RECORDS POSTED SUCCESSFULLY. YOU CAN NOW PRINT PAYSLIPS")
conn.Open()
sqlcmd.Parameters.AddWithValue("@d", processdate)
sqlcmd.Connection = conn
sqlcmd.CommandType = CommandType.StoredProcedure
sqlcmd.CommandText = "insertpaymentsanddeductions"
sqlcmd.CommandTimeout = 6000
sqlcmd.ExecuteNonQuery()
sqlcmd.Dispose()
conn.Close()
你最好做一些像@Codexer 在你的问题的评论中建议的事情,但你的确切问题的代码答案如下。
Public Sub CallStoredProcedure(ByVal procedureName as String)
...
cmd.CommandText = procedureName
...
End Sub
这是我在为数据库调用创建单独的 class 时对代码所做的一些改进;
Public Sub searchifexists(ByVal value As String, ByVal table As String, ByVal field As String)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
cmd.CommandText = "SELECT " & field & " FROM " & table & " where " & field & " = '" & value & "' "
conn.Close()
da.SelectCommand = cmd
da.Fill(dt)
If dt.Rows.Count > 0 Then
Try
MsgBox("Record already exists. Record will not be saved because you can not have duplicate values for Employee Number")
Exit Sub
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
这是我点击按钮时的结果
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Try
If btnAdd.Text = "&Revert" Then
If EmpnoTextBox.Text = "" Then
MsgBox("You must enter a value for Employee Number")
Me.EmpnoTextBox.Focus()
Exit Sub
ElseIf EmpnameTextBox.Text = "" Then
MsgBox("You must enter a value for Employee Name")
Me.EmpnameTextBox.Focus()
Exit Sub
End If
Dim dbcall As New dbcalls
dbcall.searchifexists(EmpnoTextBox.Text, "employee", "empno")
Save_Record()
Change_Button(True)
btnAdd.Text = "&Add"
End If
DisplayNav(sender, e)
Catch ex As Exception
MsgBox(ex.Message)
Exit Sub
MsgBox("Employee data saved")
End Try
End Sub
从中我学会了清理我的代码,使其具有可读性和更好的应用程序。感谢 Whosebug 社区!