Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a desktop application in which i have used c sharp with SQL server the below query fetches the data from the database when i am using the sql server as database but when i used sqlite database the below query does return nothing .tell me is there any different in writing inline queries in c sharp for sql and sqlite , or does the MAX or like function works differently in SQL and SQLITE below is my query.

C#
com.CommandText = "SELECT MAX(DocumentCode) AS DocumentCode FROM DocumentsHeader WHERE (DocumentCode LIKE @DocumentCode + '%')";
com.Parameters.Add("@DocumentCode", SqlDbType.NVarChar, 18).Value = Passvalue.ToString().Trim();
returnvalue = com.ExecuteScalar();


This query fetches record when using sql and when using sqlite it does nt fetching record while i check that my Db has the record .
any one can assists me ...
Posted
Updated 21-Oct-15 6:58am
v4
Comments
phil.o 21-Oct-15 12:58pm    
What is the type of the com variable?
Malikdanish 21-Oct-15 13:21pm    
com is a sqlite command type
com = new SQLiteCommand();

You should alter your parameter code to include the SQL wildcard.
C#
com.Parameters.Add("@DocumentCode", SqlDbType.NVarChar).Value = Passvalue.ToString().Trim() + "%";

The reason that your SQL wasn't working is that this part of the query
C#
DocumentCode LIKE @DocumentCode + '%'

Becomes this SQL
SQL
DocumentCode LIKE 'DOCCODE' + '%'

The SQL becomes a string concatenation, not a value with a wildcard.
 
Share this answer
 
The MAX function does not work on non-numeric fields. Your DocumentCode field is apparently defined as nvarchar, which is non-numeric.

My bad. It DOES work with character fields. It would help if I verified with the documentation before speaking. I've never had occasion to use MAX on character data.
 
Share this answer
 
v2
Comments
Malikdanish 21-Oct-15 13:07pm    
When I remove the where part it is pulling the data and fetching the data . mean while this Query works fine with SQL server having similar DB schema , Is there any problem with Like operator or using this wild cards character + '%' .. The Same query works fine in sql server i have both Db for test purposes i test with SQL it works fine.Is there any difference in Like operator between these Two Database ?
Dave Kreskowiak 21-Oct-15 13:13pm    
I don't use SQLite so I can't comment on that.
Malikdanish 21-Oct-15 13:15pm    
ok
Foothill 21-Oct-15 13:19pm    
Try putting the wildcard character in your parameter value.
Foothill 21-Oct-15 13:23pm    
com.CommandText = "SELECT MAX(DocumentCode) AS DocumentCode FROM DocumentsHeader WHERE (DocumentCode LIKE @DocumentCode)";
com.Parameters.Add("@DocumentCode", SqlDbType.NVarChar).Value = Passvalue.ToString().Trim() + "%";

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