Convert varchar to nvarchar - c#

My company use sql server 2000 to store data . There is a table with a column named 'Vattu' .
The problem is that : this column declare as varchar data type , however it's save both unicode and anscii value !
So every time I show this column data on web , it show unreadable characters.
Is there any way to convert data to unicode value using c# ?

A quick google search would have given you the answer. Anyway you use System.Text.Encoding to encode the ascii to unicode. check the sample code at Encoding.Convert Method
Also converting the column datatype to NVarchar will be better in long run. Doing so you will save CPU processing that you will be using due to conversion at c# level.

You need to change the data type of the column to nvarchar in order to store Unicode data.
The Unicode data you've already tried to store in the column is gone - you can't magically get it back, unless your application logged the data or you were running traces of all commands and still have access to those.

Related

String or binary data would be truncated. The statement has been terminated(EF 5.0)

We have a small old app which transfers data from one Server to another server (Using Entity Framework 5.0 and SQl server ) and generate reports issue I am having is that when it is transferring data. I am getting ("InnerException = {"String or binary data would be truncated.\r\nThe the statement has been terminated."}"..
on some data...but if I want to enter that particular data with "id" using the same app it is inserting it with no issue.I read somewhere it is an issue with EF(5.0) but I am not sure so Please let me know.
You have a char / varchar / nchar / nvarchar column with a specified length. You are inserting a row where the value has a length greater than the specified length.
When you read it from the old server, you should use string.Substring to get the first X characters from the source (where X is the database column length). If you do this, your errors will stop.

SQL encryption only returning first character

I have the following query
UPDATE mytable
SET col1 = ENCRYPTBYPASSPHRASE ('Key', col2)
FROM mytable
when I decrypt it using
SELECT CONVERT(VARCHAR(20), DECRYPTBYPASSPHRASE ('Key', col1))
FROM mytable
The result returned is only the first character, for example if the field contains "Computer" the result is only "C".
col2 is probably nvarchar not varchar. Try
SELECT CONVERT(NVARCHAR(20), DECRYPTBYPASSPHRASE ('Key', col1))
FROM mytable
In nvarchar the code points for standard ASCII letters are the same as for ASCII but padded out with a 0x00 byte.
When you cast that to varchar that it is treated as a null character that terminates the string.
After investigation I had come to many issues so I will post what I came across, so anyone can benefit from it.
If you changed to data type of the SQL column to varbinary then make sure that when you decrypt the data, you use the same old data type. That is if you had a column of varchar that contains data and then you changed it to varbinary, you must decrypt it using varchar, if you use nvarchar ,you will get garbage data.
You must encrypt and decrypt using the same way. That is if you are loading the password from a stored procedure and use it in encrypting,and the SAME EXACT password is loaded using a function for decryption, u will also get garbage data (I tested it but I did not know why is this behaviour!)may be internally there is some difference between how data is returned from SP and functions.
Hope this helps anyone out there !
Use CONVERT with data type and size of the value you are encrypting updating.
Looks like EncryptByKey does not recognize the data properly as per column schema.
Try as below
ENCRYPTBYKEY(KEY_GUID('<Key Name>'), CONVERT(varchar(20),col1))

Not able to save more than 43679 char in text datatype column in SQL Server

I have a column in table with text datatype and trying to save some string value to this column from C# code. Issue comes when I use some very large string.
I am not able to save more than 43679 character into text field. I know text size can be 2^31.
I also tried saving same value from SSMS and noticed same scenario.
There is nothing special about code, but still SQL query is given below...
update TableName
set ColumnName = 'some text more than 43679 char'
where id=<some int id>
just to mention... column is declare in table as
[columnname] [text] NULL
Can anyone tell me what could be wrong.
You can try to use varchar(max) to store huge amount of data. See MSDN
We recommend that you store large data by using the varchar(max),
nvarchar(max), or varbinary(max) data types. To control in-row and
out-of-row behavior of these data types, use the large value types out
of row option.
You can also check the same issue here: SSMS - Can not paste more than 43679 characters from a column in Grid Mode

When I insert in the textbox arabic characters are saved as "????" [duplicate]

How can I insert Arabic characters into a SQL Server database? I tried to insert Arabic data into a table and the Arabic characters in the insert script were inserted as '??????' in the table.
I tried to directly paste the data into the table through SQL Server Management Studio and the Arabic characters was successfully and accurately inserted.
I looked around for resolutions for this problems and some threads suggested changing the datatype to nvarchar instead of varchar. I tried this as well but without any luck.
How can we insert Arabic characters into SQL Server database?
For the field to be able to store unicode characters, you have to use the type nvarchar (or other similar like ntext, nchar).
To insert the unicode characters in the database you have to send the text as unicode by using a parameter type like nvarchar / SqlDbType.NVarChar.
(For completeness: if you are creating SQL dynamically (against common advice), you put an N before a string literal to make it unicode. For example: insert into table (name) values (N'Pavan').)
Guess the solation is first turn on the field to ntext then write N with the value. For example
insert into eng(Name) values(N'حسن')
If you are trying to load data directly into the database like me, I found a great way to do so by creating a table using Excel and then export as CSV. Then I used the database browser SQLite to import the data correctly into the SQL database. You can then adjust the table properties if needed. Hope this would help.

Writing to excel and dates

I have written a C# application that reads DataTables from a database (SQL Server) and exports them to Excel files. Unfortunately, in this database, the columns representing dates are sometimes represented as VARCHAR (as opposed to DATETIME).
When the column data type is DATETIME, the Excel file is generated with the value in my desired format, which is dd/mm/yyyy. On the other hand, when the data type is VARCHAR, the result is mm/dd/yyyy. I need to fix this.
The problem is that I do now know a priory if a VARCHAR column represents a date, so I should (I believe) instruct the application to export VARCHAR literally, without doing any conversion (the verbatim value on the database is correct).
How can I do this? If necessary, I can post the code that writes the Excel starting from the DataTable.
Thanks!
Giuseppe
I found a trick that works. Adding a blank space in front of the cell value, the conversion is not performed in case of varchar:
mysheet.Cells[rowCount, i] = " " + drow[i - 1].ToString();

Categories

Resources