繁体   English   中英

Sql异常:不允许从数据类型datetime到int的隐式转换。 使用CONVERT函数运行此查询

[英]Sql Exception : Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query

这是我的存储过程:

ALTER PROCEDURE [dbo].[NextPrevPost]
@PostQueryString NVARCHAR(100),
@NextID INT OUTPUT,
@NextTitle NVARCHAR(500) OUTPUT,
@NextPostDate DATETIME OUTPUT,
@NextQueryString NVARCHAR(100) OUTPUT,
@PrevID INT OUTPUT,
@PrevTitle NVARCHAR(500) OUTPUT,
@PrevPostDate DATETIME OUTPUT,
@PrevQueryString NVARCHAR(100) OUTPUT
AS
BEGIN
    DECLARE @ID INT;
    SELECT @ID=PostID FROM Post WHERE PostQueryString=@PostQueryString;

   SELECT TOP 1 
      @NextID = COALESCE(P.PostID,0),
      @NextTitle = P.PostTitle,
      @NextPostDate = COALESCE(P.PostDate, getdate()),
      @NextQueryString = P.PostQueryString 
   FROM 
      Post P 
   WHERE 
      P.PostDate >= (SELECT PostDate FROM Post WHERE PostID = @ID) 
      AND P.PostID != @ID
   ORDER BY 
      PostDate ASC

   SELECT TOP 1 
      @PrevID = COALESCE(P.PostID, 0),
      @PrevTitle = P.PostTitle,
      @PrevPostDate = COALESCE(P.PostDate, GETDATE()),
      @PrevQueryString = P.PostQueryString
   FROM 
      Post P 
   WHERE 
      P.PostDate <= (SELECT PostDate FROM Post WHERE PostID = @ID) 
      AND P.PostID != @ID
   ORDER BY 
      PostDate DESC

   IF(@PrevPostDate IS NULL)
   BEGIN
        SET @PrevPostDate = GETDATE();
   END

   IF(@NextPostDate IS NULL)
   BEGIN
        SET @NextPostDate = GETDATE();
   END

SET @PrevPostDate=GETDATE();
SET @NextPostDate=GETDATE();

这是我调用上一个存储过程的c#函数:

private void SetNextPrev(string s)
{
    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString);

    SqlCommand command = new SqlCommand("NextPrevPost",connection);
    connection.Open();

    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add(new SqlParameter("PostQueryString", DbType.String)).Value = s;

    SqlParameter nextid = new SqlParameter("NextID", DbType.Int32);
    nextid.Direction = ParameterDirection.Output;
    command.Parameters.Add(nextid);

    SqlParameter nexttitle = new SqlParameter("NextTitle", DbType.String);
    nexttitle.Direction = ParameterDirection.Output;
    command.Parameters.Add(nexttitle);

    SqlParameter NextPostDate = new SqlParameter("NextPostDate", DbType.DateTime);
    NextPostDate.Direction = ParameterDirection.Output;
    command.Parameters.Add(NextPostDate);

    SqlParameter NextQueryString = new SqlParameter("NextQueryString", DbType.String);
    NextQueryString.Direction = ParameterDirection.Output;
    command.Parameters.Add(NextQueryString);

    SqlParameter prevtid = new SqlParameter("PrevID", DbType.Int32);
    prevtid.Direction = ParameterDirection.Output;
    command.Parameters.Add(prevtid);

    SqlParameter prevtitle = new SqlParameter("PrevTitle", DbType.String);
    prevtitle.Direction = ParameterDirection.Output;
    command.Parameters.Add(prevtitle);

    SqlParameter PrevPostDate = new SqlParameter("PrevPostDate", DbType.DateTime);
    PrevPostDate.Direction = ParameterDirection.Output;
    command.Parameters.Add(PrevPostDate);

    SqlParameter PrevQueryString = new SqlParameter("PrevQueryString", DbType.String);
    PrevQueryString.Direction = ParameterDirection.Output;
    command.Parameters.Add(PrevQueryString);

    try
    {
        command.ExecuteNonQuery();

        if (prevtid.Value.ToString()!=string.Empty)
        {
        }

        if (nextid.Value.ToString()!=string.Empty)
        {
        }
    }
    catch { }
    finally { command.Dispose(); connection.Close(); }
}

我收到了错误:

不允许从数据类型datetime到int的隐式转换。 使用CONVERT函数运行此查询。

为什么?

谢谢!

UPDATE!

@BoStigChristensen我从DbType改为SqlDbType类型。 所以我的c#函数看起来像这样:

   private void SetNextPrev(string s)
{

    SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString);
    SqlCommand command = new SqlCommand("NextPrevPost",connection);
    connection.Open();

    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.Add(new SqlParameter("PostQueryString", SqlDbType.NVarChar)).Value = s;


    SqlParameter nextid = new SqlParameter("NextID", SqlDbType.Int);
    nextid.Direction = ParameterDirection.Output;
    command.Parameters.Add(nextid);

    SqlParameter nexttitle = new SqlParameter("NextTitle", SqlDbType.NVarChar);
    nexttitle.Direction = ParameterDirection.Output;
    command.Parameters.Add(nexttitle);

    SqlParameter NextPostDate = new SqlParameter("NextPostDate", SqlDbType.DateTime);
    NextPostDate.Direction = ParameterDirection.Output;
    command.Parameters.Add(NextPostDate);


    SqlParameter NextQueryString = new SqlParameter("NextQueryString", SqlDbType.NVarChar);
    NextQueryString.Direction = ParameterDirection.Output;
    command.Parameters.Add(NextQueryString);

    SqlParameter prevtid = new SqlParameter("PrevID", SqlDbType.Int);
    prevtid.Direction = ParameterDirection.Output;
    command.Parameters.Add(prevtid);


    SqlParameter prevtitle = new SqlParameter("PrevTitle", SqlDbType.NVarChar);
    prevtitle.Direction = ParameterDirection.Output;
    command.Parameters.Add(prevtitle);


    SqlParameter PrevPostDate = new SqlParameter("PrevPostDate", SqlDbType.DateTime);
    PrevPostDate.Direction = ParameterDirection.Output;
    command.Parameters.Add(PrevPostDate);


    SqlParameter PrevQueryString = new SqlParameter("PrevQueryString", SqlDbType.NVarChar);
    PrevQueryString.Direction = ParameterDirection.Output;
    command.Parameters.Add(PrevQueryString);

    try
    {
        command.ExecuteNonQuery();

        if (prevtid.Value.ToString()!=string.Empty)
        {

        }

        if (nextid.Value.ToString()!=string.Empty)
        {

        }
    }
    catch { }
    finally { command.Dispose(); connection.Close(); }

}

但不幸的是它抛出错误: String [2]:Size属性的大小无效为0。

SqlParameter构造函数采用SqlDbType而不是DbType

SqlParameter(String, SqlDbType)

传递正确的类型SqlDbType :-)

通过将DbType.DateTime传递给SqlDbType参数,您基本上传递了可能错误的映射,从而导致不可预测的结果。

一个例子:

using System;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1 {
    class Program {
        static void Main(string[] args) {
            const SqlDbType MY_SQL_ENUM = new SqlDbType();
            const DbType MY_DB_ENUM = new DbType();

            var wrongParam = new SqlParameter("WrongTest", DbType.DateTime);
            Console.WriteLine(Enum.GetName(MY_DB_ENUM.GetType(), wrongParam.DbType));
            Console.WriteLine(Enum.GetName(MY_SQL_ENUM.GetType(), wrongParam.SqlDbType));

            var rightParam = new SqlParameter("RightTest", SqlDbType.DateTime);
            Console.WriteLine(Enum.GetName(MY_DB_ENUM.GetType(), rightParam.DbType));
            Console.WriteLine(Enum.GetName(MY_SQL_ENUM.GetType(), rightParam.SqlDbType));

            Console.ReadKey();
        }
    }
}

暂无
暂无

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

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