Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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?

C#
...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

'%@searchSome%' Is a string, so SQL does not try to do and parameter substitution.
Try:
C#
SqlCommand("SELECT EngLyrics FROM dbo.Lyrics WHERE SongName LIKE '%' + @searchSome + '%';", DBConnectInfo.connection);
 
Share this answer
 
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900