C# 无法将参数值从字符串转换为int64
我收到一个错误:无法将参数从字符串转换为Int 64 这是ligne:C# 无法将参数值从字符串转换为int64,c#,sql,sql-server,C#,Sql,Sql Server,我收到一个错误:无法将参数从字符串转换为Int 64 这是ligne: com.Parameters.Add("@NBC", SqlDbType.BigInt).Value = ligneGridBC1.Cells["NBcmd"].ToString(); 我已经试过了 com.Parameters.Add("@NBC", SqlDbType.BigInt).Value = Convert.ToInt64(ligneGridBC1.Cells["NBcmd"].ToString()); co
com.Parameters.Add("@NBC", SqlDbType.BigInt).Value = ligneGridBC1.Cells["NBcmd"].ToString();
我已经试过了
com.Parameters.Add("@NBC", SqlDbType.BigInt).Value = Convert.ToInt64(ligneGridBC1.Cells["NBcmd"].ToString());
com.Parameters.Add("@NBC", SqlDbType.BigInt).Value = Int64.Parse(ligneGridBC1.Cells["NBcmd"].ToString());
我的sql表包含
NBC BigInt
任何帮助都将不胜感激。ligneGridDC1.Cells[“NBcmd”]包含什么类型的对象 默认情况下,对对象调用.ToString()将返回对象的类型,除非已重写ToString方法。您正在尝试将“SomeType.Cell”转换为Int64 试试这个
Int64 a = 0;
if(!Int64.TryParse(ligneGridBC1.Cells["NBcmd"].ToString(), out a))
throw new InvalidCastException(ligneGridBC1.Cells["NBcmd"].ToString());
com.Parameters.Add("@NBC", SqlDbType.BigInt).Value = a;
希望有帮助您是否检查了
ligneGridBC1.Cells[“NBcmd”].ToString()的值
?Convert.ToInt64(ligneGridBC1.Cells[“NBcmd”]);检查ligneGridBC1.Cells[“NBcmd”]是否返回空字符串,如果返回空字符串,则会发生此错误。如果单元格值为null或空,请尝试将默认值保持为0。