Writing to excel and dates - c#

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();

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.

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.

SQL Server Timestamp using with C#

In my database I have used Timestamp in each table to see when data was inserted.
It stores data in byte[] of 8 byte.
Now I want to read that time using C#.
How can I get DateTime object from Timestamp which is byte[]?
SQL Server's TIMESTAMP datatype has nothing to do with a date and time!
It's just a binary representation of a consecutive number - it's only good for making sure a row hasn't change since it's been read.
In never versions of SQL Server, it's being called RowVersion - since that's really what it is. See the MSDN docs on ROWVERSION:
Is a data type that exposes automatically generated, unique binary numbers within a database. rowversion is generally used as a mechanism
for version-stamping table rows. The
rowversion data type is just an incrementing number and does not
preserve a date or a time. To record a date or time, use a datetime2
data type.
So you cannot convert a SQL Server TIMESTAMP to a date/time - it's just not a date/time.
But if you're saying timestamp but really you mean a DATETIME column - then you can use any of those valid date formats described in the CAST and CONVERT topic in the MSDN help. Those are defined and supported "out of the box" by SQL Server. Anything else is not supported, e.g. you have to do a lot of manual casting and concatenating (not recommended).
The format you're looking for looks a bit like the ODBC canonical (style = 121):
DECLARE #today DATETIME = SYSDATETIME()
SELECT CONVERT(VARCHAR(50), #today, 121)
gives:
2011-11-14 10:29:00.470
SQL Server 2012 will finally have a FORMAT function to do custom formatting......

Convert varchar to nvarchar

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.

Categories

Resources