I'm using SQLite to compare some ids (varchar 50) so I get a numeric database id.
I'm using C# and visual studio 2003. Pretty new in C# so maybe I'm doing a newbie mistake.
When doing some comparison like the following
SELECT * FROM tblUSer WHERE Use_id like '%Ñ120%'
I don't get anything even if this exist... I suppose is an encoding problem, but I don't know how to solve it.
I can't change the database schema since is not my database (need to modify select some data and update a field).
I can't change the data per se since it is a data that already exist and the user codes are the same as some reference like a club id or something...
Changing the data to UTF-8 worked to make it do the query (without it it would give an error) but still I don't get the desired data. Maybe is also how the data is saved by the original program.
What do I do so my query works with the Ñ?
What if your remove the N from your query and just check
Use_id LIKE '%120%'
and then select the appropriate ones in C#?
try this:
SELECT * FROM tblUSer WHERE Use_id like N'%Ñ120%'
Notice the N letter.
You should write in this way:
SELECT * FROM tblUSer WHERE Use_id like N'%Ñ120%'
Related
I've written an app which is working with a SQL Server database(DataGridView with DataSource) and I was wondering how I could make a search (by id/name/etc). I've created it with a direct query on the DataGridView, something like this:
SELECT *
FROM TABLE
WHERE USER LIKE #USER + '%' (+'%' being added automatically).
Everything works well, but just with full word search, as an example: if I want to search the name John, I have to write John into the search box to give me the results. As "J" or "Jo" doesn't show me nothing. How can I modify the query to show me all the results starting with the string in the search box?
Thanks a lot in advance. Hope it isn't a stupid question, I'm a beginner.
The best approach is by just querying all the data from the database once, and filtering the data in the datagridview, otherwise this will cost you a query each time you filter.
So keep it like:
SELECT *
FROM TABLE
and use this as followed:
(dataGridViewFields.DataSource as DataTable).DefaultView.RowFilter = string.Format("Field = '{0}'", textBoxFilter.Text);
Goodluck!
I've an old system where I have to read data from. It's stores the data an old Visual FoxPro DBF Table.
In C# I'm using adodb.dll with Provider=VFPOLEDB.1 to get the data and it works well so far.
When I select following (its an index):
select * from tableXYZ where tab_f1+STR(tab_f2,10)+STR(tab_f3,3)+STR(tab_f4,3)+STR(tab_f5,3) = "AR1234567890"
I get in Visual FoxPro a fast result (using the index), "AR" is the tab_f1, "1234567890" is the STR(tab_f2,10). The tab_f3, tab_f4 and tab_f5 seems to be "ignored", even filled in the table i get all the rows, regardless of the content of f3 to f5.
The same select in c# with the ADODB Connection returns no result set. Is there a way to let the VFPOLEDB ignore the three fields behind too or do i need an index only for tab_f1+tab_f2?
VFP will treat = as "starts with" based on the SET ANSI setting. If you want to use that same functionality in ADODB I would use LIKE instead:
select *
from tableXYZ
where tab_f1+STR(tab_f2,10)+STR(tab_f3,3)+STR(tab_f4,3)+STR(tab_f5,3) LIKE "AR1234567890%"
However note that ADODB will not use stand-alone ("IDX") index files. If your index in in a CDX associated with that table then it should be used.
I am storing arabic text in sqlite database in my wp8 app. Below is an example of the arabic text stored in the sqlite file.
ٱلْحَمْدُ لِلَّهِ رَبِّ ٱلْعَٰلَمِينَ
The user would be able to search like below:
الحمد
this should return the above text, but I know in normal it doesn't.
I am using very simple query in sqlite
select * from tbl where ArabicText like '%الحمد%'
and in c#
query = "select * from tbl where ArabicText like '%"+textToSearch+"%'"
The result is zero records, How should I search to retrieve the above single record?
Thanks!
The easy answer, is to just use base64_encode on queries going into the database, and decode on information coming out (both while saving and while searching). It's not elegant, but it allows UTF characters into a non-UTF database.
I have an existing database with existing data that I can't change it's structure or values.
In that database there is a nvarchar column that contains values in the twilight unicode zone starting with F800, upward.
When I select those values in SQL or use SQL function, unicode - I get the proper values.
When I select the same values in .Net - I get an error value - all the values in that twilight zone become 65533.
I need those values - how can I presuade .Net to give me those values - something like chaninging the connection encoding to a custom one - or ucs-2 etc...
Here is a sample code that demonstraits the problem:
c.CommandText = "select NCHAR(55297)";
using (var r = c.ExecuteReader())
{
r.Read();
var result = r[0]; //expected 55297 but got 65533
}
55297 is D801 which isn't defined? you probably want f801 which is 63489? But it appears as if that one isn't defined either. Which characters do you want?
If I try doing a "select NCHAR(55297)" in SQL Server Management studio, I get back the diamond question mark, but if I do "select NCHAR(63489)" I get back a dot of some sort:
If what you want is the character values, you can ask for them directly:
select Unicode(NCHAR(63489))
This returns 63489 (as an integer)
If you want them as a byte array, you can ask for that:
select CONVERT(varbinary(MAX), FieldThatIsAnNvarchar) from ThatTable
After much investigations I failed to find any way around this. I couldn't find any two way conversion that would work here.
It seems that some unicode values are intended for some strange unicode scenario that isn't supported by .Net, but is partially supported in a way that breaks what we need here.
I need to change what rows are grabbed from the database based upon a few flags. How can I do this? My current code is like this:
this.tblTenantTableAdapter.Fill(this.rentalEaseDataSet.tblTenant);
Say if I wanted only rows that the id was greater than 50, how would I do that?
Edit:
The code to access the database was autogenerated by the original programmer a long time ago though VisualStudio. I don't know exactly how to get a connection from the autogenerated code. If I could do that I know how to use the SqlDataAdapter
Why wouldn't you use a WHERE clause in your SQL query?
Also, I would hope you don't use your ID field for anything like this. If you just want the first 50 selections, you may want to use a TOP 50 clause or something similar.
For some info on how to do this with your TableAdapter: http://www.shiningstar.net/ASPNet_Articles/DataSet/DataSetProject7.aspx
On the server side you can use plaid old SQL:
SELECT * FROM TBLTENANT WHERE id > 50
On the client side:
rentalEaseDataSet.tblTenant.DefaultView.RowFilter = "id > 50";
I would imagine that the simplest way is to change whatever SQL query is running behind the scenes using a WHERE clause.
Your TableAdapter needs to have the CommandText specified to a SQL query with a WHERE clause.
private void InitCommandCollection() {
this._commandCollection = new global::System.Data.SqlClient.SqlCommand[1];
this._commandCollection[0] = new global::System.Data.SqlClient.SqlCommand();
this._commandCollection[0].Connection = this.Connection;
this._commandCollection[0].CommandText = #"SELECT * FROM MyTable WHERE ID >50;";
this._commandCollection[0].CommandType = global::System.Data.CommandType.Text;
}
NOTE: The above query is just an example, not meant as a solution query. The format of the above code is used to edit the generated Table Adapter designer class's InitCommandCollection() method so you can specify your own SQL. It is possible your class already has some SQL in here, in which case you could just alter it.