如何在函数中启动存储过程



此代码自动更新会计年度...

 Dim yearVal As Double
 If Date.Now.Month = 10 OrElse Date.Now.Month = 11 OrElse Date.Now.Month = 12 Then
     yearVal = Date.Now.Year
 Else
     yearVal = Date.Now.Year - 1
 End If

每年当它更改为下一年时,我想自动执行这个 sql 存储过程......

ALTER PROCEDURE dbo.NewBudget
    @year int,
    @int nvarchar(3)
AS
BEGIN
SET NOCOUNT ON;
    INSERT [NAOLI].[dbo].[BudgetReal]
    ([F_Year],[O_OrgCode],[O_OrgDesc],[S_SubObject],[S_SubDescrip],[B_BudgetAmt],[B_Initials],[B_CIPrefNo],[B_OrgBudgetAmt]) 
    SELECT @year as [F_Year],[O_OrgCode],[O_OrgDesc],[S_SubObject],[S_SubDescrip], 0.00 as [B_BudgetAmt],@int as [B_Initials],[B_CIPrefNo],[B_OrgBudgetAmt]
    FROM [NAOLI].[dbo].[BudgetReal]
END

我怎样才能做到这一点?

这起到了作用

Dim yearVal As Double
    If Date.Now.Month = 10 OrElse Date.Now.Month = 11 OrElse Date.Now.Month = 12 Then
        yearVal = Date.Now.Year
            If yearVal = Date.Now.Month = 10 OrElse Date.Now.Month = 11 OrElse Date.Now.Month = 12 Then
                dt = dal.ExecuteSelectStoredProc(dal.dbType.SqlServer, "NewBudget", "@year", DropDownList1.Text, "@int", )
            End If
    Else
        yearVal = Date.Now.Year - 1
    End If

感谢您的帮助。

您需要使用 ADO.NET 连接到数据库。 你将需要一个基于 RDBMS 的连接字符串。 然后你可以把它放在一个类中,并在你的条件IF语句块中调用它。

Dim sqlConnection1 As New SqlConnection("Your Connection String")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "dbo.NewBudget"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@year").Value = yearVal
cmd.Parameters.Add("@int").Value = intValue 'No idea why you have a Stored Procedure paramter called @int
cmd.Connection = sqlConnection1
sqlConnection1.Open()
reader = cmd.ExecuteNonQuery()
' Data is accessible through the DataReader object here.
sqlConnection1.Close() 

http://msdn.microsoft.com/en-us/library/d7125bke(v=vs.80).aspx#Y1200

http://www.connectionstrings.com

最新更新