带有SQL参数的Asp.net方法返回错误的resutls

时间:2013-11-19 14:18:40

标签: c# asp.net sql stored-procedures objectdatasource

我正在使用存储过程和搜索方法。我发送一个名为@tag的参数。

   @tag varchar 

AS

SELECT top 50
comment_id, 
comment_post_id, 
comment_user_id, 
comment_text, 
comment_like, 
comment_dislike, 
comment_reply_to_id, 
comment_date,
UserId,
UserName,
profil_foto_kucuk,
profil_foto_buyuk,
post_etiket


FROM         
comment
inner join aspnet_Users on comment.comment_user_id = aspnet_Users.UserID
inner join profil on comment.comment_user_id = profil.profil_user_id
inner join post on comment.comment_post_id=post.post_id
WHERE

comment_text like '%'+ @tag +'%'
ORDER BY comment_date DESC

例如,数据库中@ tag = akdeniz只有一个结果。当我手动执行sp for  comment_text like '%'+ akdeniz+'%'
返回一个结果。 但是当我从asp.net端发送参数时返回所有值。 Asp.net方面;

  <SelectParameters>
    <asp:QueryStringParameter Name="tag" QueryStringField="tag"  Type="String"  />
    </SelectParameters>

地址;

http://localhost:52137/Search.aspx?tag=akdeniz

和方法;

 public static List<Yorum> SearchYorumlariGetir(string tag)
    {
        List<Yorum> yorum_listesi = new List<Yorum>();
        try
        {
            SqlConnection baglanti = new SqlConnection(data_works.dbconnect());
            try
            {
                SqlCommand komut = new SqlCommand("sp_search_yorum_getir", baglanti);
                komut.CommandType = CommandType.StoredProcedure;
                komut.Parameters.AddWithValue("@tag", tag);
                Yorum yorum = null;
                baglanti.Open();
                SqlDataReader dr = komut.ExecuteReader(CommandBehavior.CloseConnection);
                try
                {
                    while (dr.Read())
                    {
                        yorum = new Yorum();
                        yorum.Yorum_ID = dr.GetInt32(dr.GetOrdinal("comment_id"));
                        yorum.Post_ID = dr.GetInt32(dr.GetOrdinal("comment_post_id"));
                        yorum.User_ID = dr.GetGuid(dr.GetOrdinal("comment_user_id"));
                        yorum.Yorum_UserName = dr.GetString(dr.GetOrdinal("UserName"));
                        yorum.Yorum_Text = dr.GetString(dr.GetOrdinal("comment_text"));
                        yorum.Yorum_Like = dr.GetInt32(dr.GetOrdinal("comment_like"));
                        yorum.Yorum_Dislike = dr.GetInt32(dr.GetOrdinal("comment_dislike"));
                        yorum.Yorum_ReplyTo_ID = dr.GetInt32(dr.GetOrdinal("comment_reply_to_id"));
                        yorum.Yorum_Date = dr.GetDateTime(dr.GetOrdinal("comment_date"));
                        yorum.Yorum_Post_Etiket = dr.GetString(dr.GetOrdinal("post_etiket"));
                        yorum.Yorum_Profil_Foto_Kucuk = dr.GetString(dr.GetOrdinal("profil_foto_kucuk"));
                        yorum.Yorum_Profil_Foto_Buyuk = dr.GetString(dr.GetOrdinal("profil_foto_buyuk"));
                        yorum_listesi.Add(yorum);
                    }
                    dr.Close();
                }
                finally
                {
                    if (dr != null)
                    {
                        ((IDisposable)dr).Dispose();
                    }
                }
            }
            finally
            {
                if (baglanti != null)
                {
                    ((IDisposable)baglanti).Dispose();
                }
            }
        }
        catch (Exception exception)
        {
            throw exception;
        }
        List<Yorum> yorums = yorum_listesi;
        return yorums;
    }

我无法理解为什么会这样。

1 个答案:

答案 0 :(得分:2)

如果存储过程有varchar参数,则需要指定长度,否则只使用第一个字符。

例如:

@tag varchar(48)