简体   繁体   English

通过 c# mysql 连接执行存储过程时出现错误“列的数据太长”

[英]Error "Data too long for column" when executing stored procedure via c# mysql connection

I am writing a C# application that will access a MySQL Database using a stored procedure with 5 parameters:我正在编写一个 C# 应用程序,它将使用带有 5 个参数的存储过程访问 MySQL 数据库:

in startTime varchar(20), 
in endTime varchar(20), 
in tagID Int,
in FullDay Int,
in iteration Int

that returns 3 variables all integers.返回 3 个变量都是整数。 When executing the MySQLCommand I get the error: "Data too long for column 'startTime' at row 41".执行 MySQLCommand 时出现错误:“第 41 行‘startTime’列的数据太长”。

Here is my code:这是我的代码:

//create command
MySqlCommand cmd = new MySqlCommand("GetArchiveData", connection);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("startTime", startIn);
cmd.Parameters["startTime"].Direction = ParameterDirection.Input;

cmd.Parameters.AddWithValue("endTime", endIn);
cmd.Parameters["endTime"].Direction = ParameterDirection.Input;

cmd.Parameters.AddWithValue("tagID", tagIn);
cmd.Parameters["tagID"].Direction = ParameterDirection.Input;

cmd.Parameters.AddWithValue("FullDay", fdIn);
cmd.Parameters["FullDay"].Direction = ParameterDirection.Input;

cmd.Parameters.AddWithValue("iteration", iterationIn);
cmd.Parameters["iteration"].Direction = ParameterDirection.Input;

if (cmd.Connection.State == ConnectionState.Closed)
{
    cmd.Connection.Open();
}
MySqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
    TagData tagData = new TagData();
    tagData.TagID = tagIn;
    tagData.Dsttimestamp = Convert.ToInt32(dr["timestamp"]);
    tagData.PvValue = Convert.ToInt32(dr["sp"]);
    tagData.PvValue = Convert.ToInt32(dr["pv"]);

    tagDataList.Add(tagData);
}
dr.Close();

this.CloseConnection();
return tagDataList; 

If anyone knows why this would happen and why the first occurrence would be row 41 it would be greatly appreciated.如果有人知道为什么会发生这种情况以及为什么第一次出现在第 41 行,我们将不胜感激。

UPDATE:更新:

Removing the Single quotations that are required in the MySQL statement in workbench from the strings startIn and endIn has solved that issue.从字符串 startIn 和 endIn 中删除工作台中 MySQL 语句中所需的单引号已解决该问题。 MySQL uses single quotes to define strings where the C# variable is already declared as a string. MySQL 使用单引号定义字符串,其中 C# 变量已声明为字符串。

It looks like your problem is in the sproc. 看来您的问题出在存储过程中。 Have you looked at the vaklue you are passing in to it? 您是否看过传递给它的vaklue? It just seems the value is too long for the column so looking at what is being passed will let you know for sure. 似乎该值对于该列而言太长,因此查看正在传递的内容将使您确定。

I don't have 50 reputation yet. 我还没有50声望。 This is not an answer, this is more of a comment. 这不是答案,更多是评论。 I'll edit if it becomes an answer. 如果它成为答案,我将进行编辑。

Where do you populate 'startTime'? 您在哪里填充“ startTime”? By the looks of things you're giving it a value > 20 characters. 从外观上看,您给它的值> 20个字符。

Inserting value is too long for the column. 列的插入值太长。 Please debug and Check whether u are inserting correct type of data or correct type value for the column. 请调试并检查u是否为列插入正确的数据类型或正确的类型值。
There is an hard limit on how much data can be stored in a single row of a mysql table, regardless of the number of columns or the individual column length. 对于mysql表的单行中可以存储多少数据有硬性限制,无论列数或单个列长如何。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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