문제

I want to get IDs between Startpoint and EndPoint but I can not Filter query.When I write command without greater then and less then operators it works but searchs all table.

I can't figure out how to fix this. Here's my code; please can you help me?

public async Task<int> TaskSearchSqlTweetIDText(string TweetText, string Query, int StartPoint, int EndPoint)
    {
        SqlConnection conn = new SqlConnection(Tools.ConnectionString);
        SqlCommand comm = new SqlCommand("select ID from Tweets where @VsTweetText=TweetText and ID<@VsEndPoint and ID>@VsStartPoint", conn);

        comm.Parameters.AddWithValue("@VsTweetText", TweetText);

        comm.Parameters.Add("@VsStartPoint", SqlDbType.Int);
        comm.Parameters["@VsStartPoint"].Value = StartPoint;

        comm.Parameters.Add("@VsEndPoint", SqlDbType.Int);
        comm.Parameters["@VsEndPoint"].Value = EndPoint;


        if (conn.State == ConnectionState.Closed) conn.Open();

        object sonuc = await comm.ExecuteScalarAsync();
        conn.Close();

        if (sonuc != null)
        {
            return (int)sonuc;

        }
        else
        {
            return 0;

        }
    }
도움이 되었습니까?

해결책

You don't state this in your question, so I assume that you're not getting any results back from the query as it is?

Probably there are simply no Tweets with IDs strictly greater and strictly smaller than the range you're providing where the TweetText matches. You could debug to see what the values for StartPoint and EndPoint are and if there really is an ID in the database that's strictly between them where the TweetText matches.

You could change your statement as follows to include the values for StartPoint and EndPoint.

select ID from Tweets
where @VsTweetText = TweetText
    and ID <= @VsEndPoint
    and ID >= @VsStartPoint

or even

select ID from Tweets
where @VsTweetText = TweetText
    and ID BETWEEN @VsStartPoint AND @VsEndPoint

Please also note that the statement above may very well return more than one record, but you're using ExecuteScalar in your code, so you're using the first ID you encounter - randomly. If you're really only interested in one ID, you should modify your query so that it is more predictable which one is chosen.

Maybe like this:

select top 1 ID from Tweets
where @VsTweetText = TweetText
    and ID BETWEEN @VsStartPoint AND @VsEndPoint
order by ID desc

Which would return the biggest ID found within the range.

다른 팁

If you get more than 1 entry from the query in SSMS, you might want to look into the criteria - why is it returning more than one entry. Tighten the where clause, make it precise.

where @VsTweetText = TweetText
    and ID < @VsEndPoint
    and ID > @VsStartPoint

But if you want to get only first entry (if you do), then use this:

select top 1 ID from Tweets
where @VsTweetText = TweetText
    and ID < @VsEndPoint
    and ID > @VsStartPoint

because you are using ExecuteScalarAsync which can return only a single scalar object.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top