Click here to Skip to main content
14,211,647 members
Rate this:
Please Sign up or sign in to vote.
Hi there. I cannot read my data from database. The query result is empty. I following to this article: GUIDE. Also, executing queries directly in DB returns me wished results. DB is SQLSERVER 2012. What i did wrong?

...string search....//the function's argument

SqlParameter sqlp = new SqlParameter("@searchSome", System.Data.SqlDbType.NVarChar, search.Length);
                    sqlp.Value = search;

                    SqlCommand cmd = new SqlCommand("SELECT EngLyrics FROM dbo.Lyrics WHERE SongName LIKE '%@searchSome%';", DBConnectInfo.connection);
                    cmd.Parameters.Add(sqlp);
                    
                    try
                    {
                        using (SqlDataReader sdr = cmd.ExecuteReader())
                        {
                            string result = String.Empty;
                            while (sdr.Read())
                            {
                                result = sdr["EngLyrics"].ToString();
                                return result;
                            }
                            return result;
                        }
                    }
                    catch (SqlException ex) { return ex.ToString(); }
                    finally
                    {
                        DBConnectInfo.connection.Close();
                    }
Posted
Updated 13-Aug-13 10:14am
v3
Comments
Richard C Bishop 13-Aug-13 16:18pm
   
Test your query in the db and see what you get.
Je7 13-Aug-13 16:22pm
   
I test already. Even if i just copy query from this page, and past in command window( but i changed %@searchSome% on %some text%) and query returned what i need.
Richard C Bishop 13-Aug-13 16:24pm
   
See the solution below, OriginalGriff has it resolved.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

'%@searchSome%' Is a string, so SQL does not try to do and parameter substitution.
Try:
SqlCommand("SELECT EngLyrics FROM dbo.Lyrics WHERE SongName LIKE '%' + @searchSome + '%';", DBConnectInfo.connection);
   
v3
Comments
Richard C Bishop 13-Aug-13 16:21pm
   
No disrespect intended by editing your post, thought I saw a typo. My mistake.
Je7 13-Aug-13 16:26pm
   
First version is working fine. ('%' + @searchSome + '%')
Richard C Bishop 13-Aug-13 16:27pm
   
My mistake, I thought I saw a typo.
OriginalGriff 13-Aug-13 16:27pm
   
It wasn't a typo - your change stops it working and causes a compilation error! :laugh:
Richard C Bishop 13-Aug-13 16:27pm
   
Indeed you are correct, my mistake. Apologies.
Je7 13-Aug-13 16:24pm
   
Thank you. All works perfect.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.190619.2 | Last Updated 13 Aug 2013
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100