简体   繁体   English

.NET C#mysql SUBSTRING给了我System.Byte []?

[英].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.

相关问题 从c#查询MySQL返回System.Byte [] - Query to MySQL from c# returns System.Byte[] c#Registry System.Byte [] to string - c# Registry System.Byte[] to string 如何将 System.Byte[] 转换为要显示到 Word C# 中的图像 - How to convert System.Byte[] into an Image to be Displayed into Word C# 在C#中读取pdf文件时出现System.byte错误 - System.byte error while reading a pdf file in C# 如何在 C# 中将 struct System.Byte byte[] 转换为 System.IO.Stream 对象? - How do I convert struct System.Byte byte[] to a System.IO.Stream object in C#? {System.Byte[]} 到 System.Byte[] - {System.Byte[]} to System.Byte[] 无法将类型为“System.String”的 object 转换为类型“System.Byte []”错误 MYSQL NET CORE 3.1 - Unable to cast object of type 'System.String' to type 'System.Byte[]' Error MYSQL NET CORE 3.1 错误在C#调用存储过程中从'System.String'到'System.Byte []'的无效转换 - Error Invalid cast from 'System.String' to 'System.Byte[]' in C# calling stored procedure 如何将System.Byte []转换为Image? (C#窗口形式) - How can I convert System.Byte[] to Image? (C# window forms) 字符串值 system.byte[] 同时将 Base64 值转换为 C# 中的字符串 - String value system.byte[] while coverting Base64 value to string in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM