简体   繁体   English

从ASP.Net/VB.Net中的存储过程返回日期

[英]Returning date from Stored procedure in ASP.Net/VB.Net

I want to execute a method on VB.Net to return a date which is in the stored procedure. 我想在VB.Net上执行一个方法以返回存储过程中的日期。 I tried using ExecuteScalar but it doesnt work it retruns error 我尝试使用ExecuteScalar,但是它不起作用,它会重新运行错误

'Implicit conversion from data type datetime to int is not allowed. '不允许从数据类型datetime到int的隐式转换。 Use the CONVERT function to run this query' 使用CONVERT函数运行此查询”

Any help would be much appreciated please? 任何帮助将不胜感激,请?

thank you 谢谢

below is the code 下面是代码

  Public Function GetHolidaydate(ByVal struserID as String) As DateTime

        Dim objArgs1 As New clsSQLStoredProcedureParams

        objArgs1.Add("@userID", Me.Tag)
        objArgs1.Add("@Date", 0, 0, ParameterDirection.Output)

        Return (CDate(ExecuteScalar(clsLibrary.MyStoredProcedure.GetHolidayDate, objArgs1)))

End Function

I think that your problem is here: 我认为您的问题在这里:

objArgs1.Add("@Date", 0, 0, ParameterDirection.Output)

You are adding 0's where they should be typeOf DateTime. 您正在添加0,它们应为typeOf DateTime。

EDIT 编辑

Public Function GetHolidayDate(ByVal struserID as String) AS DateTime
    Dim con As New SqlConnection(yourSQLConnectionString)
    Dim cmd As New SqlCommand
    Dim date As DateTime    

    con.Open()
    cmd.Connection = con
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "yourStoredProcedureName"
    cmd.Parameters.Add("@userID", Me.Tag)
    cmd.Parameters("@Date").Direction = ParameterDirection.Output
    cmd.ExecuteScalar()
    date = cmd.Parameters("@Date").Value
    con.Close()

    Return date
End Function

Looks like the error you are getting is a SQL error not a VB.Net error. 看起来您得到的错误是SQL错误而不是VB.Net错误。 Are you trying to convert a datetime to an int somewhere? 您是否要在某处将日期时间转换为int? You could you try running the ExecuteNonQuery() method to see if you get the same error. 您可以尝试运行ExecuteNonQuery()方法以查看是否遇到相同的错误。

You could also run SQLProfiler and see exactly what SQL is being run. 您还可以运行SQLProfiler并确切查看正在运行的SQL。 You could then try running this SQL is SQL Server Management Studio 然后,您可以尝试运行此SQL,即SQL Server Management Studio

You add two parameters - but you said it should return the later one ? 您添加了两个参数-但您说应该返回下一个参数?

So it must be 所以一定是

date DateTime = cmd.ExecuteScalar()
REM date = cmd.Parameters("@Date").Value
con.Close()

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

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