简体   繁体   中英

.NET C# mysql SUBSTRING gives me System.Byte[]?

This this code:

SELECT SUBSTRING(posted,1,4) as year FROM styles
reader = cmd1.ExecuteReader(CommandBehavior.CloseConnection);
reader.Read();
Response.Write(reader[0].ToString());

I only get the string "System.Byte[]" printed out. How come?

If I use the software Mysql Query Browser I get the actual string from my database.

I understand that "Byte[]" is an arraylist but how do I convert this to a pure string?

The "posted"-field in my database contains a date like "2010-04-04 13:23:00" and I want to get only the year by using SUBSTRING.

You will need to use the .GetString

ie

reader[0].GetString(0);

Additionally you can use the MySQL YEAR function to extract the year from your date.

ie

SELECT YEAR(date_field) FROM table

The correct query is

SELECT DISTINCT SUBSTRING(CONVERT(varchar, posted, 111),1,4) as year FROM styles

It equals to

SELECT STR(YEAR(posted)) as year FROM styles-- YEAR returns int statement

First argument is converted, than substring extracted. 111 - the convertion format: http://www.mssqltips.com/tip.asp?tip=1145

Also try

reader["year"].ToString();

as far as you use this alias.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM