日期:2014-05-17  浏览次数:20783 次

c#.net 将VARCHAR转换为int时出错
点击右边红色标题查看本文完整版:c#.net 将VARCHAR转换为int时出错

mycom.Parameters.Add( "@Companyid ",System.Convert.ToInt32(L_compnay.Text.Trim()));//int型
mycom.Parameters.Add( "@Invest_adress ",T_Invest_adress.Text.ToString().Trim());
mycom.Parameters.Add( "@YcheckDate ",System.Convert.ToDatetime(T_ycheckDate.Text.Trim()));//datetime型
mycom.Parameters.Add( "@ServerDate ",T_ServerDate.Text.Trim());//datetime型
提交后提示:将VARCHAR转换为int时出错
另外下面的时间转换时也出错
要怎样解决?

------解决方法--------------------
类型不声明当然要报错

mycom.Parameters.Add( "@Companyid ", SqlDataType.Int32);
mycom.Parameters[ "@Companyid "].Value = System.Convert.ToInt32(L_compnay.Text.Trim());
mycom.Parameters.Add( "@Invest_adress ", SqlDataType.VarChar);
mycom.Parameters[ "@Invest_adress "].Value = T_Invest_adress.Text.ToString().Trim();
mycom.Parameters.Add( "@YcheckDate ", SqlDataType.DateTime);
mycom.Parameters[ "@YcheckDate "].Value = System.Convert.ToDatetime(T_ycheckDate.Text.Trim());

如果是 2.0,可以使用 AddWithValue() 方法,这样可以不用声明类型。