I have a SQL Server 2005 database in which I have some tables contain Arabic text. The datatype for those fields is NVARCHAR(n).
The Arabic text inside the table is appearing properly, and when selecting using select clause, they appear properly.
The problem is that searching for Arabic text with where clause results in 0 rows.
select * from table_name
where name=#name
This retrieves no rows, where there is a name with this value.
When we use it like:
select * from table_name
where name=N’Arabic_Text’
Then it works, but how we can pass searching text from front end to back end.
Can you please guide me on how to write the query?
PS
In code behind i wrote:
Dim UserName As String = "N'" & txtLogin.Text & "'"
Dim _dtLogin As DataTable = oUser.UserLogin(UserName)
it returns 0 rows even if that user exist in database.
You need to concatenate your query and you need to use N before value to check because of unicode value
Considering this part: select * from table_name where name=#name
If this is a stored procedure, you need to modify it to be like this:
select * from table_name where name=N#name
give it a try and let us know if it worked.
Related
I'm not really an ASP developer, so a little bit lost.
I have the following data access code:
sSQL = "SELECT answer_id, company_name, old_access_company_name, answer, flag_asker_notified FROM Q01_ask_sheiiba_answer_company2 WHERE question_id="& sQuestion_id &" ORDER BY answer_id"
rs.open sSQL, conn
DO WHILE NOT rs.EOF
Response.Write(rs.Fields("answer"))
rs.MoveNext
LOOP
I have tested that the sql query is built properly by outputting it to the response before it is called. It produces the following query:
SELECT answer_id, company_name, old_access_company_name, answer, flag_asker_notified
FROM Q01_ask_sheiiba_answer_company2
WHERE question_id=988
ORDER BY answer_id
When I copy that exact query to sql management studio and run it I get the expected results of 5 rows and each row containing data in every cell, BUT, when I run it through the above code, I get the same 5 rows with the same cell data, EXCEPT for the answer column, which is empty!
What am I missing?
Thanks in advance
There are two things you might want to try:
Put your text field at the end of the query. For example:
SELECT answer_id, company_name, old_access_company_name, flag_asker_notified, answer
If this doesn't give you the results, you might want to try:
WHILE NOT rs.EOF
theanswer=rs("answer")
Response.Write(theanswer)
rs.movenext
wend
Text and Memo fields can play a little havoc with ASP.
EDIT: Another thing that you can try is:
rs.CursorLocation = adUseClient
or
rs.CursorLocation = 3
The problem is that the ODBC driver can't access large text blobs as strings; you need to access them as chunked BLOB data.
I advise instead to dump the ODBC connection and connect using the OLE-DB driver directly. This will let you access that column as if it was just another varchar column.
I just had a similar problem (I think). I converted a varchar field to text. When I did so, I found that the text field seemed to "disappear" from my selected record set. I my case, I discovered that you can reference the text field ONLY ONCE. After that, it seems to disappear. Accordingly, for the text field, I now simply move it into a string variable and then operate on the string. That solved my problem.
John
Had the same problem and found the solution here https://web.archive.org/web/20170224013842/http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=80
Basically when you open the recordset (not connection.execute) use the options adOpenKeyset (val 1) and adUseClient (val 3), and the text filed should be the last in your field list in strSql
example: rs.Open strSql, dbConn, adOpenKeyset, adUseClient
I am trying to implement an idea where I have two sql tables in a database.
Table Info which has a field Nationality and the other Table Exclusion which has a field Keyword.
`Info.Nationality` `Exclusion.Keyword`
|British| |France|
|resteraunt de France| |Spanish|
|German|
|Flag Italian|
|Spanish rice|
|Italian pasta
|Irish beef|
In my web application I am creating a GridView4 and through a DataTable and SqlDataAdapter I am populating that GridView4 with the SQL command:
SELECT DISTINCT Info.Nationality WHERE Exclusion.Keyword NOT LIKE '%Spanish%'
That SQL statement retrieves all the distinct records in Info.Nationality which do not contain the word spanish.
What I am currently doing is that in the web app which is in vb.net I am adding two different GridViews, each have the data of each table which means that GridView2 has DISTINCT Info.Nationality and GridView3 has Exclusion.Keyword and then adding another GridView4 to display the results of the above SQL command.
The idea is to retrieve all the distinct records from Info.Nationlity which are not suppressed by the keyword constraints in Exclusion.keyword. So from the above mentioned Sql command the GridView4 will retrieve all the records which do not have the word "Spanish".
I am doing all of this in a nested for loop where in the first loop it takes each record (one by one) from Info.Nationality e.g.for each row As DataRow in Me.GridView2.Rows() and compare it with the second for loop which goes till the end of the Exclusion.Keyword which would be like For i=0 To Gridview3 - 1.
The problem is that in that sql statement I have to explicitly specify the word to compare. I tried adding the records of Exclusion.Keyword in a String and then replacing the Spanish Keyword In between the NOT LIKE with the name of the String which is Keywords and then assigning the name a parameter with cmd.parameter.addwithvalue(#String, Keywords). However this is not working, it is only comparing with the last record in the string and ignoring the first.
The idea behind all of this is to display all the records of Info.Nationality in GridView4 which do not contain the keywords in Exclusion.Keyword.
Is there an easier or more effecient way to do this? I was thinking of an Inner Join with a Like command but that is not my problem. My problem is that how can I compare each record one by one of Info.Nationlity with all the records in Exclusion.keyword and then retrieving the ones that do not match and discarding the ones that match.
Then in Gridview4 how can I edit the records without reflecting those changes or affecting in Info.Nationality but rather only Inserting to Exclusion.Keyword the changes.
SOLVED by adding ToString() after Text
In my asp.net web app, I tried this, but didn't work: (SOLVED)
`SELECT DISTINCT Nationality
FROM Info Where NOT EXISTS(SELECT * FROM Exclusion WHERE Info.Nationality LIKE '%' + #GridView +'%')`
`cmd.parameters.AddwithValue("#GridView", GridView3.Rows(i).Cells(0).Text.ToString())`
GridView3 Here has the Exclusion.Keywords data.
Would really appreciate your suggestions and thoughts around this.
You do not need to do this one-by-one, or "Row by agonizing row" as some DBAs are fond of describing this type of approach. There are lots of ways to write a query to only return the records from Info.nationality that do not match the exclusion keywords as a single expression.
My preference is to use the EXISTS clause and a correlated subquery:
SELECT Nationality
FROM Info I
WHERE NOT EXISTS(SELECT * FROM Exclusion WHERE I.Nationality LIKE '%' + Keyword + '%')
You can also express this as a left join.
SELECT I.Nationality
FROM Info I
LEFT OUTER JOIN Exclusion E
ON I.Nationality LIKE '%' + E.Keyword + '%'
WHERE E.Keyword IS NULL
The left join will return all the rows from info and insert nulls in the columns for Exclusion except where the join criteria matches. By filtering for only where those values are null, you can avoid the matches.
I have a form that has the 'data entry' property set to yes. It is bound to a table. When I start filling in the form it automatically saves it. I do not want this to happen. I only want the form to save to the table when I press a button. Any easy way to do this? w/o vba. If i can only do this with vba let me know how to do it that what.
The best way to do this is with an unbound form. When the user clicks save, you can run a query to update your table from the controls.
Using a recordset
Dim rs As Recordset
Set rs=CurrentDB.Openrecordset("MyTable")
rs.AddNew
rs!Field1 = Me.Field1
rs.Update
If you wanted to update a record where you already knew the primary key, you could say:
Dim rs As Recordset
Set rs=CurrentDB.Openrecordset("SELECT * FROM MyTable WHERE ID=" & Me.txtID)
rs.Edit
rs!Field1 = Me.Field1
rs.Update
Using a query that you have created in the query design window
SQL for the query
INSERT INTO MyTable (Field1)
VALUES ( Forms!MyForm!Field1 )
VBA
This will give a warning
DoCmd.OpenQuery "MyQuery"
This will not
CurrentDb.Execute "Query2", dbFailOnError
You could also use dynamic SQL or a query with parameters that you assign in code.
In my programming task I've gone down a dark alley and wished I hadn't, but there is no turning back now.
I'm building up a SQL statement where the table name, column name and id value are retrieved from query string parameters i.e. ("SELECT [{0}] FROM [{1}] WHERE [Id] = {2};", c, t, id)
But it isn't as bad as it looks, I'm protected:
Only authenticated users (i.e. signed in users) can execute the Page_Load
I'm checking that both the table and the column exists beforehand
(using GetSchema etc.)
I'm checking that the Id is an integer beforehand
All my tables have Id columns
The database connection is reasonably secure
The field value is expected to be of type NVARCHAR(4000) or NVARCHAR(MAX) so I'm avoiding ExecuteScalar and I'm trying out LINQ ExecuteQuery because I like LINQ. But I'm a bit out of my depth again.
I've got this far:
Dim db As New MyDataContext
Dim result = db.ExecuteQuery(Of ITable)("SELECT [{0}] FROM [{1}] WHERE [Id] = {2};", c, t, id)
Is this the right way to go?
How do I get first row and first column value?
Is there a better alternative?
P.S. It's a SQL Server 2005 database
Any help appreciated.
Thanks.
SQL Server requires the tables ans columns to be statically known. You can't provide them using command parameters. You can't say
select * from #tableName
because the table name can't be a variable.
You need to build the SQL string with C# ensuring proper escaping of identifiers. Escaping works like this:
var escaped = "[" + rawUntrustedUserInput.Replace("]", "]]") + "]";
This is safe.
I am writing a query like
select * from fact_table where name = 'R&D'
select * from fact_table where name = 'John's'
I am using oracle 11g database. Is there any generic function or query style where in I can handle special characters like '&' and ''' in my query.
I do not want to handle each case seperately, i want a generic mechanism.
If this is a duplicate post please guide me to the original post
The ampersand should work fine. Some tools, like SQLPlus or SQLDeveloper use this character to indicate a bind variable. Disable this "feature" like this:
set define off
re-enable it like this:
set define on
the single quote needs special attention.
Use two single quote characters to tell the database to treat it as a literal, like this:
select * from fact_table where name = 'John''s'
Use Quote Operator
select * from fact_table where name = q'#John's#'
For ampersands, it is an issue for tools like SQL*Plus
set define ~
select * from fact_table where name = 'R&D'
But in a pl/sql block, it seems working fine:
declare
output VARCHAR2(10);
BEGIN
SELECT name
INTO output
FROM fact_table
WHERE name = '&1';
DBMS_OUTPUT.put_line('Output: ' || output);
END;
/
SQL> #test.sql;
Enter value for 1: R&D
old 7: WHERE name = '&1';
new 7: WHERE name = 'R&D';
Output: R&D
PL/SQL procedure successfully completed.