如何读取 varbinary max 数据类型在 SQL Server 中。

22 浏览
0 Comments

如何读取 varbinary max 数据类型在 SQL Server 中。

我有这个查询:

CREATE PROCEDURE [dbo].[spGetUserAvatar]
    ( @userid int)  
     AS 
      BEGIN
        select avatar
          from t_user
             where (userID = @userid) AND (avatar<>null)
       end

以及用于执行此查询的 C# 代码:

string ConnectionString = SafaConnectionString.ConnectionString;
    SqlConnection Connection = new SqlConnection(ConnectionString);
    SqlCommand cmd = new SqlCommand("spGetUserAvatar", Connection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@userid", userid);
    DataTable dt = new DataTable();
    SqlDataAdapter sda = new SqlDataAdapter();
    try
    {
        cmd.Connection = Connection;
        Connection.Open();            
       sda.SelectCommand = cmd;
        sda.Fill(dt);
        if (dt.Rows.Count > 0)
            return dt;
        else
            return null;
    }

它之前工作正常,但现在返回dt.Rows.Count == 0。在这种情况下,t_users.avatar 不为空。我是不是查询错了?

0
0 Comments

问题原因是在使用SQL语句时,无法使用<>运算符来比较NULL值,需要使用IS运算符来验证NULL值。

解决方法是将原来的代码中的<>运算符替换为IS运算符,以验证NULL值。具体操作如下:

where userID =  AND avatar is not null

0