[英].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. 我只得到打印出来的字符串“ System.Byte []”。 How come?
怎么会?
If I use the software Mysql Query Browser I get the actual string from my database. 如果使用软件Mysql Query Browser,则会从数据库中获取实际的字符串。
I understand that "Byte[]" is an arraylist but how do I convert this to a pure string? 我知道“ Byte []”是一个数组列表,但是如何将其转换为纯字符串?
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. 我数据库中的“已发布”字段包含一个类似于“ 2010-04-04 13:23:00”的日期,我想通过使用SUBSTRING仅获得年份。
You will need to use the .GetString 您将需要使用.GetString
ie 即
reader[0].GetString(0);
Additionally you can use the MySQL YEAR function to extract the year from your date. 另外,您可以使用MySQL YEAR函数从日期中提取年份。
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
111-转换格式: http ://www.mssqltips.com/tip.asp?tip= 1145
Also try 也试试
reader["year"].ToString();
as far as you use this alias. 就您使用此别名而言。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.